# Angle between 2 quaternions

161 views (last 30 days)
Patrícia Falcão on 23 Aug 2018
Edited: James Tursa on 19 Aug 2019
How do we calculate angle between 2 quaternions? For example, what is the angle between x = ( 0.968, 0.008, -0.008, 0.252) and y = (0.382, 0.605, 0.413, 0.563)? How can I do it in matlab?

Show 1 older comment
Patrícia Falcão on 23 Aug 2018
This is about quaternions, not lines. It doesn't work, but thanks anyway!
Patrícia Falcão on 23 Aug 2018
I don't have matlab 2018
Rik on 23 Aug 2018
What is the angle between two points? You need to define that first, or explain the mathematical definition, as I couldn't find the definition with a quick search.

James Tursa on 23 Aug 2018
Edited: James Tursa on 23 Aug 2018
Assuming these represent attitude rotations from one coordinate frame to another, if you are simply asking what is the minimum rotation to take you from one quaternion to the other, you simply multiply one quaternion by the conjugate of the other and then pick off the rotation angle of the resulting quaternion.
But we really need to know what these quaternions represent, and what angle you are trying to recover, before we know what you want.
E.g., suppose x and y represent ECI->BODY rotation quaternions, and you want to know the minimum rotation angle that would take you from the x BODY position to the y BODY position. Then you could do this:
>> x = [ 0.968, 0.008, -0.008, 0.252]; x = x/norm(x); % ECI->BODY1
>> y = [ 0.382, 0.605, 0.413, 0.563]; y = y/norm(y); % ECI->BODY2
>> z = quatmultiply(quatconj(x),y) % BODY1->BODY2
z =
0.5132 0.6911 0.2549 0.4405
>> a = 2*acosd(z(4)) % min angle rotation from BODY1 to BODY2
a =
127.7227
But, again, these calculations are dependent on how I have the quaternions defined. Your specific case may be different.

Silas Waxter on 17 Aug 2019
The accepted answer here seems to work better.
James Tursa on 19 Aug 2019
Agreed for matching MATLAB. I should have noted this above. The Answer above assumes the scalar part is in the 4th element (does not match MATLAB toolboxes). The Answer in the other post assumes the scalar part is in the 1st element (which matches MATLAB toolboxes). They are both valid conventions and both are widely used in industry. Which is the correct method to use for a given application will depend on how the user has his/her quaternions defined.