Physics Library
 An open source physics library
Encyclopedia | Forums | Docs | Random | Template Test |  
Login
create new user
Username:
Password:
forget your password?
Main Menu
Sections

Meta

Talkback

Downloads

Information
[parent] example of quaternions and direction cosine matrices (Example)

Quaternions and Direction Cosine Matrices: Examples, Exercises, and Solutions

This companion article belongs to the PhysicsLibrary entry quaternions and direction cosine matrices. All exercises are stated first. Complete solutions appear only after the exercise section so that the article is self study friendly.

Convention declaration

We use right-handed frames, Hamilton multiplication, scalar-first display notation, and the active rotation rule

$\displaystyle \mathbf v' = q\mathbf v q^* = R(q)\mathbf v$ (1)
for unit quaternions. Passive coordinate changes are represented by the associated DCM relation
$\displaystyle {}^{A}\mathbf v = C^A_B\,{}^{B}\mathbf v, \qquad C^A_B = R({}^{A}q_B).$ (2)

Exercises

Exercise 1

Starting from the vector rotation formula
$\displaystyle \mathbf v'= (q_w^2-\mathbf q\cdot\mathbf q)\mathbf v +2\mathbf q(\mathbf q\cdot\mathbf v) +2q_w(\mathbf q\times\mathbf v),$ (3)
derive the compact matrix formula for $R(q)$.

Exercise 2

Expand the compact formula to obtain the component matrix
$\displaystyle R(q)= \begin{bmatrix} 1-2(q_y^2+q_z^2) & 2(q_xq_y-q_wq_z) & 2(q_x... ..._wq_x)\ 2(q_xq_z-q_wq_y) & 2(q_yq_z+q_wq_x) & 1-2(q_x^2+q_y^2) \end{bmatrix}.$ (4)

Exercise 3

For
$\displaystyle q=\frac{1+\mathbf k}{\sqrt2},$ (5)
compute $R(q)$.

Exercise 4

Using the matrix from Exercise 3, compute the images of $\mathbf e_1$, $\mathbf e_2$, and $\mathbf e_3$.

Exercise 5

Show directly from the compact matrix formula that $R(-q)=R(q)$.

Exercise 6

Show that for a unit quaternion,
$\displaystyle R(q^*)=R(q)^T.$ (6)

Exercise 7

Let
$\displaystyle q=\cos\frac{\pi}{3}+\mathbf i\sin\frac{\pi}{3} =\frac{1}{2}+\frac{\sqrt3}{2}\mathbf i.$ (7)
Find $R(q)$.

Exercise 8

Let
$\displaystyle q=\cos\frac{\pi}{3}+\frac{\mathbf i+\mathbf j+\mathbf k}{\sqrt3}\sin\frac{\pi}{3}.$ (8)
Compute $R(q)$ and interpret the result geometrically.

Exercise 9

Show that the columns of $R(q)$ are the images of $\mathbf e_1$, $\mathbf e_2$, and $\mathbf e_3$.

Exercise 10

Write the passive DCM relation for the frame chain $B \to A \to N$ and derive
$\displaystyle C^N_B = C^N_A C^A_B.$ (9)

Exercise 11

Suppose
$\displaystyle R= \begin{bmatrix} 0 & -1 & 0\ 1 & 0 & 0\ 0 & 0 & 1 \end{bmatrix}.$ (10)
Recover a quaternion representing this rotation.

Exercise 12

Given the proper rotation matrix
$\displaystyle R= \begin{bmatrix} 1 & 0 & 0\ 0 & -1 & 0\ 0 & 0 & -1 \end{bmatrix},$ (11)
find a quaternion representing it and explain why the trace formula using $q_w$ is numerically delicate here.

Exercise 13

Show that if $q$ is unit, then $R(q)$ is orthogonal and has determinant $+1$.

Exercise 14

Explain the difference between the active matrix relation $\mathbf v'=R(q)\mathbf v$ and the passive DCM relation ${}^{A}\mathbf v=C^A_B\,{}^{B}\mathbf v$.

Exercise 15

A software package stores quaternions in scalar-last order. A student copies the scalar-first matrix formula without changing the component mapping. Describe the error and how to fix it.

Solutions

Solution 1

Write the three terms separately:
$\displaystyle (q_w^2-\mathbf q\cdot\mathbf q)\mathbf v = (q_w^2-\mathbf q^T\mathbf q)I_3\mathbf v,$ (12)
$\displaystyle 2\mathbf q(\mathbf q\cdot\mathbf v)=2\mathbf q\mathbf q^T\mathbf v,$ (13)
and
$\displaystyle 2q_w(\mathbf q\times\mathbf v)=2q_w[\mathbf q]_\times\mathbf v.$ (14)
Adding the three contributions gives
$\displaystyle R(q)= (q_w^2-\mathbf q^T\mathbf q)I_3 + 2\mathbf q\mathbf q^T + 2q_w[\mathbf q]_\times.$ (15)

Solution 2

Insert
$\displaystyle \mathbf q= \begin{bmatrix}q_x\\ q_y\\ q_z\end{bmatrix}, \qquad [\... ...egin{bmatrix} 0 & -q_z & q_y\ q_z & 0 & -q_x\ -q_y & q_x & 0 \end{bmatrix},$ (16)
into the compact matrix formula and expand entry by entry. Using $q_w^2+q_x^2+q_y^2+q_z^2=1$ simplifies the diagonal terms, yielding the stated matrix.

Solution 3

For $q_w=q_z=1/\sqrt2$ and $q_x=q_y=0$, so
$\displaystyle R(q)= \begin{bmatrix} 0 & -1 & 0\ 1 & 0 & 0\ 0 & 0 & 1 \end{bmatrix}.$ (17)

Solution 4

The columns are the images of the basis vectors, so
$\displaystyle R(q)\mathbf e_1= \begin{bmatrix}0\\ 1\\ 0\end{bmatrix}, \qquad R(... ... 0\end{bmatrix}, \qquad R(q)\mathbf e_3= \begin{bmatrix}0\\ 0\\ 1\end{bmatrix}.$ (18)
Thus the $x$ axis rotates into $y$, the $y$ axis rotates into $-x$, and the $z$ axis is unchanged.

Solution 5

Replacing $q$ by $-q$ changes $q_w$ to $-q_w$ and $\mathbf q$ to $-\mathbf q$. The terms in the matrix become
$\displaystyle (-q_w)^2 - (-\mathbf q)^T(-\mathbf q) = q_w^2-\mathbf q^T\mathbf q,$ (19)
$\displaystyle 2(-\mathbf q)(-\mathbf q)^T = 2\mathbf q\mathbf q^T,$ (20)
and
$\displaystyle 2(-q_w)[-\mathbf q]_\times = 2q_w[\mathbf q]_\times.$ (21)
Hence $R(-q)=R(q)$.

Solution 6

A unit quaternion satisfies $q^{-1}=q^*$. The inverse active rotation matrix is therefore the transpose of the forward matrix. More directly, quaternion conjugation corresponds to reversing the rotation, so
$\displaystyle R(q^*)=R(q)^{-1}=R(q)^T.$ (22)

Solution 7

Here $q_w=1/2$, $q_x=\sqrt3/2$, and $q_y=q_z=0$. Therefore,
$\displaystyle R(q)= \begin{bmatrix} 1 & 0 & 0\ 0 & -\frac12 & -\frac{\sqrt3}{2}\ 0 & \frac{\sqrt3}{2} & -\frac12 \end{bmatrix}.$ (23)
This is the matrix for a $120^\circ$ active rotation about the $x$ axis.

Solution 8

Here
$\displaystyle q_w=\frac12, \qquad q_x=q_y=q_z=\frac12.$ (24)
Substituting into the component formula gives
$\displaystyle R(q)= \begin{bmatrix} 0 & 0 & 1\ 1 & 0 & 0\ 0 & 1 & 0 \end{bmatrix}.$ (25)
This cyclically permutes the coordinate axes: $\mathbf e_1\mapsto\mathbf e_2$, $\mathbf e_2\mapsto\mathbf e_3$, and $\mathbf e_3\mapsto\mathbf e_1$.

Solution 9

For any matrix $A$,
$\displaystyle A= \begin{bmatrix} A\mathbf e_1 & A\mathbf e_2 & A\mathbf e_3 \end{bmatrix}.$ (26)
Applying this to $A=R(q)$ shows that its columns are exactly the rotated basis vectors.

Solution 10

The passive chain is
$\displaystyle {}^{N}\mathbf v = C^N_A\,{}^{A}\mathbf v, \qquad {}^{A}\mathbf v = C^A_B\,{}^{B}\mathbf v.$ (27)
Substituting the second relation into the first gives
$\displaystyle {}^{N}\mathbf v = C^N_A C^A_B\,{}^{B}\mathbf v,$ (28)
so
$\displaystyle C^N_B = C^N_A C^A_B.$ (29)

Solution 11

The matrix is the $+90^\circ$ active rotation about the $z$ axis, so one choice is
$\displaystyle q=\frac{1+\mathbf k}{\sqrt2}.$ (30)
The equally valid choice $-q$ represents the same matrix.

Solution 12

The matrix is a $180^\circ$ rotation about the $x$ axis, so one choice is
$\displaystyle q=\mathbf i.$ (31)
The trace is $1-1-1=-1$, so the formula
$\displaystyle q_w = \frac12\sqrt{1+\operatorname{tr}(R)}$ (32)
returns $q_w=0$. Dividing by $4q_w$ is therefore numerically dangerous. One must instead use a branch based on the large value of $q_x$.

Solution 13

For a unit quaternion the corresponding map is a proper rotation, so it preserves dot products and orientation. Hence the matrix is orthogonal and has determinant $+1$. Algebraically one can verify
$\displaystyle R(q)^T R(q)=I_3$ (33)
from the compact formula or from the fact that the inverse rotation is given by $q^*$.

Solution 14

The active formula $\mathbf v'=R(q)\mathbf v$ rotates the vector itself in a fixed basis. The passive formula ${}^{A}\mathbf v=C^A_B\,{}^{B}\mathbf v$ changes the coordinates used to describe the same geometric vector. The same orientation change may be described either way, but the physical interpretation is different.

Solution 15

The student has confused storage layout with component meaning. In the scalar-first formula, the first symbol is the scalar component $q_w$. If a library stores $[q_x,q_y,q_z,q_w]^T$, the entries must be remapped before the formula is used. Otherwise the resulting matrix corresponds to a different quaternion.

License

This article is an original synthesis prepared for PhysicsLibrary and intended for release under CC BY-SA 4.0.



"example of quaternions and direction cosine matrices" is owned by bloftin.
(view preamble)
View style:

This object's parent.

Cross-references: scalar, dot products, conjugation, determinant, trace, matrix, formula, vector, relation, quaternions, section, quaternions and direction cosine matrices

This is version 1 of example of quaternions and direction cosine matrices, born on 2026-08-24.
Object id is 1102, canonical name is ExampleOfQuaternionsAndDirectionCosineMatrices.
Accessed 5 times total.

Classification:
Physics Classification02.40.Yy (Geometric mechanics )
 02.10.Hh (Rings and algebras)
 45.40.-f (Dynamics and kinematics of rigid bodies)
Pending Errata and Addenda
None.
Discussion
Style: Expand: Order:

No messages.

Interact
rate | post | correct | update request | add example | add (any)