How can I get an array of velocity if I have an array of position and another array of time

38 views (last 30 days)
So I have two arrays, one with position and other with time but now I want to get a vector with the velocity over that time, ive seen it being done many ways but I cant get none to work, this is an example of my code:
pos=[0 2 4 8 16 32 64];
t=[0 0.1 0.2 0.3 0.4 0.5 0.6];
V= diff(pos)/diff(t);
plot(t,V)
but this is not working as intended, how can I do it?
Thanks!

Accepted Answer

Fabio Freschi
Fabio Freschi on 4 Dec 2019
First, you need a element-wise division
pos = [0 2 4 8 16 32 64];
t = [0 0.1 0.2 0.3 0.4 0.5 0.6];
V = diff(pos)./diff(t);
Then you must realize that the size of V equals the size of t and pos minus one. So you need to reduce the t vector. I usually set a new time vector at the mid-points of the original vector
t_new = (t(1:end-1)+t(2:end))/2; % [0.05 0.15 0.25 ...]
Now you can plot
figure, plot(t_new,V);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!