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?
The mistake is setting
a = [0 0 0]. The point of interest is centered at the origin, and to calculate the angle with respect to vectorb, you need to specify the direction the point is traveling. This can by done by settingais a unit vector.If the point is traveling in the “x” direction, then
x1=1Problem Solved, forget to use the unit vector 😛