I am trying to calculate the velocity of an object based on vectors of its X and Y coordinates. Originally, I used both component velocities then used the Pythagorean theorem to add them together. mdcx
and mdcy
are vectors of the x and y coordinates, respectively.
for i=2:length(mdcx)
xdif(i)=mdcx(i-1)-mdcx(i);
end
xvel=(xdif/(1/60));
for i=2:length(mdcy)
ydif(i)=mdcy(i-1)-mdcy(i);
end
yvel=(ydif/(1/60));
v=hypot(xvel,yvel);
A friend mentioned how stupid this was, and I realized that there was a much nicer way of doing it:
d = hypot(mdcx,mdcy);
for i = 2:length(d)
v(i,1) = d(i)-d(i-1);
end
v = v/(1/60);
This is all well and good, except for the two methods get different answers and I cannot figure out why. An example of the results from method no. 1 are:
- 3.39676316513232
- 1.69387130561921
- 1.21490740387897
- 1.40071410359145
- 0.702281994643187
- 1.02703456611744
- 0.933380951166206
and the equivalent section from method no. 2:
- 3.00324976888577
- 1.41904819171419
- 0.473028796076438
- 0.772429851826608
- 0.126083801997687
- 1.02574816428026
- 0.541889676174012
My Question
What am I doing wrong here? Why aren't these coming up with the same results? It's probably a stupid mistake, but I can't seem to figure out where it's coming from. Am I using hypot
correctly?
Thanks in advance!