MATLAB: How to find the angle between two quaternions

4 dimensionsanglequaternions

If quaternions represent an orientation in space, there is an axis between any two orientations and an angle between them. I'm looking for the procedure to find that angle.

Best Answer

For example purposes I am using the coordinate frames as ECI and BODY
Q1 = quaternion from ECI->BODY1
Q2 = quaternion from ECI->BODY2
Then perform the following calculation
Q12 = conj(Q1) * Q2 % <- quaternion conjugate and quaternion multiply
Q12 = quaternion from BODY1->BODY2
There may be MATLAB functions to do the conjugate and multiply, but I don't know at the moment. The conjugate of Q1 is simply [Q(1),-Q(2:4)] of course assuming the scalar is the first element.
If we assume the scalar is the first element of the quaternion, matching the MATLAB quaternion functions convention, then you have
Q12(1) = cos(angle/2)
and
Q12(2:4) = sin(angle/2) * e
where e is the unit axis of rotation
From these you can solve for the angle
angle = 2 * atan2(norm(Q12(2:4)),Q12(1))
See also this post:
and this post: