I am calculating the angle between vectors $$cos(\theta{_a}{_b}) = \frac{a^T b}{||a|| ||b||}$$
In mathematica I am coding
ArcCos[N[Transpose[{{10},{12},{4}}].{{2.5},{3},{1}}/(Norm[{{10},{12},{4}}] * Norm[{{2.5},{3},{1}}])][[1]][[1]]]
The dot product on top and and product of the norms on the bottom both equal 65. which means I should be calculating the arccos of one. The angle should be zero.
The answer I get is $$0 + 2.10734 x 10 ^{-8} i $$
If I use a fraction instead of decimal ...
ArcCos[Transpose[{{10},{12},{4}}].{{5/2},{3},{1}}/(Norm[{{10},{12},{4}}]*Norm[{{5/2},{3},{1}}])]
I get a zero.
What do I need to do to get a clean zero with decimals in my vectors?
Chop:ArcCos[N[Transpose[{{10}, {12}, {4}}] . {{2.5}, {3}, \ {1}}/(Norm[{{10}, {12}, {4}}]*Norm[{{2.5}, {3}, {1}}])][[1]][[1]]] // Chop[#, 10^-7] &$\endgroup$VectorAngle? Alternatively, use a numerically stable formula such asWith[{a = Flatten@N@{{10}, {12}, {4}}, b = Flatten@N@{{2.5}, {3}, {1}}}, With[{n1 = Norm[a], n2 = Norm[b]}, (*vector angle formula by Velvel Kahan*) 2 ArcTan[Norm[a n2 + n1 b], Norm[a n2 - n1 b]] ]]from here. $\endgroup$