Matlab – Angle between two vectors in Matlab

geometrymathMATLAB

I have two points, a and b. I need to calculate the angle between them, so I treat them like vectors. However vector a will always be defined as [0 0 0]. Reading over the MATLAB Newsreader, "Angle between two vectors", three solutions are provided:

x1 = 0;
y1 = 0;
z1 = 0;
x2 = 0;
y2 = 1;
z2 = 0;
a = [x1,y1,z1]; b= [x2,y2,z2];

theta = rad2deg(atan2(norm(cross(a,b)),dot(a,b)))
theta = rad2deg(acos(dot(a,b)))
theta = rad2deg(atan2(x1*y2-x2*y1,x1*x2+y1*y2)) 

However as acos has accuracy problems as theta nears zero, yet out of the three equations, only acos provides the correct solution.

Should I continue to use acos or is there a better solution?

Best Answer

A vector has magnitude and direction, while a and b are just coordinate points in space. When you treat a and b as vectors, you are implicitly defining [0 0 0] as the origin point for the two vectors. However, since point a is at [0 0 0], then it will be a vector with zero length.

If a vector has zero length, which direction does it point in? The answer is nowhere. It doesn't point in any direction, and thus you can't find the angle between it and another vector.

I think maybe you've defined your problem poorly. Does your coordinate system have an origin other than [0 0 0]? Are you actually trying to find the angle between the line formed by a and b and the x-y plane?