# Compute Angle Between Quaternions (in Matlab)

I am working on a project where I have many quaternion attitude vectors, and I want to find the 'precision' of these quaternions with respect to each-other.

Without being an expert in this type of thing, my first thought is to find the angle between each (normalized) quaternion, and then find the RMS of that angle. That will give a measure of the precision of our attitude measurements. I was going to use a simple dot product to get this angle.

I don't know that this is a good solution though. Don't know if the dot product will necessarily work between quaternions though.

Note that just want a scalar representation of the pointing precision, and that the differences will on the order of arcseconds. I had also thought to convert to Euler Angles and then just use the vector part of that the Euler Angles.

Doing the math in MATLAB, but really I'm just looking for the theory behind it.

Thanks for the help!

-

## migrated from stackoverflow.comJul 7 '12 at 12:56

This question came from our site for professional and enthusiast programmers.

I had better success with the logic here: math.stackexchange.com/questions/90081/quaternion-distance – user111777 Nov 27 '13 at 5:41

For small differences the Euclidian distance between the two vectors is sufficient. To get an exact answer you would have to use the following process. Assume your quaternions $x$ and $y$ are represented as $x = [x_0, x_1, x_2, x_3]$ and $y = [y_0, y_1, y_2, y_3]$ and that they are unit quaternions. Then let $\operatorname{inv}()$ denote the inverse of a quaternion which for unit quaternions is equivalent to the conjugate (i.e. $\operatorname{inv}(x)=\operatorname{conj}(x) = [x_0, -x_1, -x_2, -x_3]$). Then the quantity that captures the true difference is $z = x*\operatorname{conj}(y)$. We can then recover the angle using $\theta = 2\arccos(z_0)$. Then $\theta$ gives you an angle by which the two quaternions differ.