How to find the average velocity across positions in coordinates using a for loop

2 views (last 30 days)
I have two variables that both consist of column vectors with 3000 rows. One are X coordinates and the other are Y coordinates. I am trying to find the average velocity over a given time that these coordinates were collected. I figured a for loop would work best given the amount of data that is here. I am struggling to figure out how i can find the distance from position 2 to 1, 3 to 2, 4 to 3......3000 to 2999, and have this run in the for loop. P is currently just a place holder I have used to say position. Any help would be great thank you!
for i = 1:2999
P(i+1)-P(i);
end

Accepted Answer

Image Analyst
Image Analyst on 8 Nov 2018
Use sqrt()
distance = 0;
for k = 2 : length(x)
distance = distance + sqrt((x(k) - x(k-1))^2 + (y(k) - y(k-1))^2);
end
% Divide total distance by the number of time points to get the average velocity.
meanVelocity = distance / length(x);
  2 Comments
Andy Jung
Andy Jung on 8 Nov 2018
Thank You I believe this make sense but As this is right now though gives me the total distance correct? I may have worded my question incorrectly. Don't I need to find each instantaneous velocity between each time point then find the average of those
Image Analyst
Image Analyst on 8 Nov 2018
You can (just put an index on distance), but no, you don't. If you put some marks along the floor and measure the distance along the floor and the total distance across the room, do you need to know the speed or time between each of those points as you walk along? No. All that counts is how far you walked and how long it took. It doesn't matter if you went faster between some marks than others.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!