1

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!

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147

2 Answers2

5

The correct method is the first. Velocity is a vector, so you have to compute its x, y components and then obtain the magnitude of that vector.

With the second method you are subtracting magnitudes of distances, and that's not correct. For example, in a circular movement around the origin of coordinates that would give you zero velocity, which is wrong.

To sum up: you are dealing with vectors. Do the vector subtraction, and only at the end you take the magnitude. Magnitude of difference is not the same as difference of magnitudes.

By the way, you can vectorize the first method using diff (note this will not give an initial zero in the result as your method does):

v = hypot(diff(mdcx), diff(mdcy))*60;
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Thanks for the fast and clear answer! I was under the impression from a highschool physics class that velocity = displacement/time, which I can now see would give problems in this case. – user3845377 Jul 28 '14 at 15:58
  • That's correct in a _vector_ sense. Just remember you are dealing with vectors, so displacement has to be computed by a _vector subtraction_ of positions – Luis Mendo Jul 28 '14 at 16:31
1

In the first case, you're computing the lengths of the difference of position segments. In the second, you're computing the differences of the "lengths" of the positions.

In other words, if you were given 3 points A, B, and C, the first method computes the difference of the length of AB and the length of BC. The second method, however, computes the differences of the lengths of A, B, and C.

The first method is correct; the second method is using the "length" of position, which isn't really a valid/useful figure.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57