MATLAB: Angle between 2 3D straight lines

3danglelinesMATLABvectors

Hi everybody,
i have two 3D straight lines as follow:
the line "S1" passing through the points A=[3,7,9] and B=[4,5,8]
and the line "S2" passing through the points A=[3,7,9] and C=[4,5,0].
How can i calculate the angle between S1 and S2.
Thank you very much!
Riccardo

Best Answer

A = [3,7,9];
B = [4,5,8];
C = [4,5,0];
S1 = B - A;
S2 = C - A;
Theta = atan2(norm(cross(S1, S2)), dot(S1, S2));
W. Kahan suggested in his paper "Mindless.pdf":
2 * atan(norm(S1 * norm(S2) - norm(S1) * S2) / ...
norm(S1 * norm(S2) + norm(S1) * S2))
Both methods are more stable than appraoches using ACOS(DOT) or ASIN(CROSS).