How do I calculate the acceleration using only 3D distances?

Hi,
I have a list of distances in each cell of acc_dist . The first three columns in each cell are xyz coordinates. So column one are all x, column two are all y, and column three are all z. The rows are the coordinate points measured at a speed of 72 Hz.
I am trying to compute the acceleration of these coordinate points by first calculating the 3D euclidean distance between each point and the adjacent point like so:
sqrt((x2-x1).^2+(y2-y1).^2+(z2-z1).^2)
And then I am trying to use the acceleration formula like so:
acceleration = dv/dt
dv = change in velocity
dt = change in time
How would I go about calculating the acceleration for the entire length of each list?
Thank you!

댓글 수: 4

Do you want the linear acceleration along the path? That is what you seem to be describing.
Yes, that is what I need
Use gradient() twice.
However... acceleration is a signed vector quantity, derived from velocity which is a signed vector quantity. This is a problem for you because sqrt((x2-x1).^2+(y2-y1).^2+(z2-z1).^2) is not signed and not a vector. You are calculating speed, not velocity.
lil brain
lil brain 2022년 4월 15일
편집: lil brain 2022년 4월 15일
@Walter Roberson I dont think I follow could you elaborate on that?

댓글을 달려면 로그인하십시오.

 채택된 답변

Les Beckham
Les Beckham 2022년 4월 15일
편집: Les Beckham 2022년 4월 15일
load acc_dist.mat
xyz = cell_of_double_pre_ballsCopy{1};
dxyz = diff(xyz); % difference between adjacent points in xyz coordinates
v = sqrt(dxyz(:,1).^2 + dxyz(:,2).^2 + dxyz(:,3).^2) * 72;
a = gradient(v, 1/72);
t = 0:1/72:(length(a)-1)/72;
plot(t,v)
plot(t,a)

댓글 수: 13

lil brain
lil brain 2022년 4월 15일
편집: lil brain 2022년 4월 15일
Thank you! This looks helpful.
Previously I have been using the following formulas which seem very different. Could you kindly tell me something about how they differ?
distance = sqrt( dx.^2 + dy.^2 + dz.^2 );
acceleration = diff(distance,2) .* SR^2;
Looks like you are calculating all three components.
What is SR? What is dist?
lil brain
lil brain 2022년 4월 15일
편집: lil brain 2022년 4월 15일
SR ist sample rate and dist is distance (i edited it)
Les Beckham
Les Beckham 2022년 4월 15일
편집: Les Beckham 2022년 4월 15일
I forgot to ask what dx, dy, and dz are. I assumed they were already diff(x), diff(y), and diff(z).
If so, I don't think you want diff(distance, 2) as you have already diff'ed the data once. Have you plotted your results to compare against mine. I think your "distance" is really distance per sample which would be 1/SR times my v.
Sorry I have one more question. If I use your code for the and create a for loop to calculate the velocity I get an error saying:
Undefined function 'power' for input arguments of type 'cell'.
This is my code:
v_pre_h{v} = sqrt(d_xyz_pre{1,v}(:,1).^2 + d_xyz_pre{1,v}(:,2).^2 + d_xyz_pre{1,v}(:,3).^2);
v_pre_h = v
d_xyz_pre = d
Any ideas why that is?
You definitely can't square a cell.
I don't know what you are doing with this. You are operating on variables that weren't in your mat file and doing some pretty weird indexing. What are all of these variables; what class and size?
Try whos to see.
It looks like you are making this harder than it should be.
If you are trying to save the results for both cells in your mat file, try this
load acc_dist.mat
for i = 1:numel(cell_of_double_pre_ballsCopy)
xyz{i} = cell_of_double_pre_ballsCopy{i};
dxyz{i} = diff(xyz{i}); % difference between adjacent points in xyz coordinates
v{i} = sqrt(dxyz{i}(:,1).^2 + dxyz{i}(:,2).^2 + dxyz{i}(:,3).^2) * 72;
a{i} = gradient(v{i}, 1/72);
t = 0:1/72:(length(a{i})-1)/72;
figure
plot(t,v{i})
figure
plot(t,a{i})
end
That message suggests that d_xyz_pre is a cell array containing cell arrays, instead of being a cell array that contains numeric matrices.
Sorry you are right this wasnt very clear of me.
Essentially I am trying to apply your code above to a larger file cell array with 19 cells (the file I uploaded only has 2 cells). I am trying to build for loops that perform all of your steps on each of the cells in my cell array. But I am having trouble making it work.
Here is what I have so far:
% differences
n = numel(cell_of_double_pre_ballsCopy);
dxyz = {};
for d = 1:n
dxyz{d} = cellfun(@diff,cell_of_double_pre_ballsCopy,'UniformOutput',false); % unsure if this is the correct way of applying the code to every cell in the array
end
dxyz_doubles = cellfun(@cell2mat, dxyz, 'uni',false ); % need to convert the cell array into a cell array of doubles
% velocity
n = numel(dxyz_doubles);
vel = {}; %velocity
for v = 1:n
vel{v} = sqrt(dxyz_doubles{v}(:,1).^2 + dxyz_doubles{v}(:,2).^2 + dxyz_doubles{v}(:,3).^2);
end
% acceleration
n = numel(vel);
acc = {}; %acceleration
for v = 1:n
acc{a} = gradient(vel{a},1/72);
end
Can you help me with this?
dxyz_doubles = cellfun(@diff, cell_of_double_pre_ballsCopy,'UniformOutput', false);
There is no reason why my previous response couldn't handle additional dimensions of the input data. It worked for two, why not 19?
Personally, I'm not a big fan of cellfun, arrayfun, etc., as I feel like they just obscure what is really going on, which is a loop. Why not just code the loop so that everyone that looks at it knows that is a loop. If someone can convince me otherwise, please do so (with a good reason).
@Les Beckham my bad, I think I posted my last comment and code without already realizing. Your previous code works well for what I am trying to do.
"Apply this function to every element of this array" is one of the fundamental array operations in theory.
Consider for example,
A = [1 3 5]
A = 1×3
1 3 5
B = A.^2
B = 1×3
1 9 25
From a theoretical perspective, this is not "squaring the vector": from a theoretical perspective, it is "Apply the function x->x^2 to each element of A, returning an array of the results.
This is frequenty called the "map" operation. And having a brief call that says that you are mapping a function over all of the elements of an array, is often significantly clearer than using a for loop.
Point taken. Thanks for clarifying.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2022년 4월 15일

댓글:

2022년 4월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by