December 23, 2024

Direction Cosine Matrix Tutorial – Correct Mathematical Explanation -Important for Aerospace and Robotics Engineering


In this tutorial, we introduce the concept of a direction cosine matrix. This matrix is important for mathematically describing rigid body motion, rotations, and other topics that appear in aerospace and robotics engineering.

While preparing a series of tutorials on robotics and UAVs kinematics and dynamics, I realized that in a number of books on robotics and aerospace (from the 80s and 90s) direction cosine matrices and other rigid-body kinematics concepts are explained by using non-standard (from today’s viewpoint) linear algebra notation and conventions. Such conventions were maybe popular during the 80s in the aerospace community, however, from today’s viewpoint they are obsolete. Consequently, even for me, it was challenging to properly decipher how the geometrical concepts from these books are translated into linear algebra and matrix operations. It feels like reading an early math manuscript from the 15th century before standard math conventions were established.

To clarify the main concepts, here is a tutorial I made on direction cosine matrices. Direction cosine matrices are important for robotics and aerospace engineering since they are used to represent a series of rotations around a fixed point. That is, they are used as a tool to represent the orientation of rigid bodies in space. They are a generalization of rotation matrices. Usually, they are expressed as a product of several rotation matrices. In the tutorial, I am using the up-to-date linear algebra standards for transforming geometrical objects into matrices and vectors.

The YouTube tutorial accompanying this webpage is given below.

Direction Cosine Matrix Definition

We consider two coordinate systems E and D shown in the figure below. The coordinate systems are also called reference frames in aerospace and robotics engineering.

The coordinate system D is denoted by the blue color, and the coordinate system E is denoted by the red color. The unit vectors that define the axis directions of the coordinate system E are

(1)   \begin{align*}\vec{\mathbf{e}}_{1},\; \vec{\mathbf{e}}_{2}, \; \vec{\mathbf{e}}_{3}\end{align*}

On the other hand, the unit vectors that define the axis directions of the coordinate system D are

(2)   \begin{align*}\vec{\mathbf{d}}_{1},\; \vec{\mathbf{d}}_{2}, \; \vec{\mathbf{d}}_{3}\end{align*}

Here, we assumed that the coordinate system D is rotated with respect to the coordinate system E. Also, it is very important to keep in mind that without any superscript or additional notation, the components of unit vectors of both coordinate systems are represented with respect to a third reference coordinate system. For example, if

(3)   \begin{align*}\vec{\mathbf{e}}_{1}=\begin{bmatrix}\frac{\sqrt{2}}{2} \\ \frac{\sqrt{2}}{2}  \\ 0 \end{bmatrix},\;\; \vec{\mathbf{d}}_{1}=\begin{bmatrix}\frac{\sqrt{3}}{2} \\ \frac{1}{2}  \\ 0 \end{bmatrix}\end{align*}

then all 3 projections of \vec{\mathbf{e}}_{1} and all three projections of \vec{\mathbf{d}}_{1} are represented with respect to the same coordinate system that is independent of both E and D. This coordinate system will be denoted by O. That is, unless there is a superscript notation, all the vectors are represented with respect to the coordinate system O.

Next, consider the figure shown below

The angles \alpha_{11}, \alpha_{12}, and \alpha_{13} be the angles that the unit vector \vec{\mathbf{d}}_{1} makes with the unit vectors \vec{\mathbf{e}}_{1}, \vec{\mathbf{e}}_{2}, and \vec{\mathbf{e}}_{3}, respectively. From the figure above, we have

(4)   \begin{align*}\vec{\mathbf{d}}_{1}=\cos(\alpha_{11}) \vec{\mathbf{e}}_{1}+\cos(\alpha_{12}) \vec{\mathbf{e}}_{2}+\cos(\alpha_{13}) \vec{\mathbf{e}}_{3}\end{align*}

Next, let \alpha_{21}, \alpha_{22}, and \alpha_{23} be the angles that the unit vector \vec{\mathbf{d}}_{2} makes with the unit vectors \vec{\mathbf{e}}_{1}, \vec{\mathbf{e}}_{2}, and \vec{\mathbf{e}}_{3}, respectively. Finally, let \alpha_{31}, \alpha_{32}, and \alpha_{33} be the angles that the unit vector \vec{\mathbf{d}}_{3} makes with the unit vectors \vec{\mathbf{e}}_{1}, \vec{\mathbf{e}}_{2}, and \vec{\mathbf{e}}_{3}, respectively. We have

(5)   \begin{align*}\vec{\mathbf{d}}_{2}=\cos(\alpha_{21}) \vec{\mathbf{e}}_{1}+\cos(\alpha_{22}) \vec{\mathbf{e}}_{2}+\cos(\alpha_{23}) \vec{\mathbf{e}}_{3} \\\vec{\mathbf{d}}_{3}=\cos(\alpha_{31}) \vec{\mathbf{e}}_{1}+\cos(\alpha_{32}) \vec{\mathbf{e}}_{2}+\cos(\alpha_{33}) \vec{\mathbf{e}}_{3}\end{align*}

Now, what is a scalar product of the vectors \vec{\mathbf{d}}_{1} and \vec{\mathbf{e}}_{2} ? We have

(6)   \begin{align*}\vec{\mathbf{d}}_{1}\cdot \vec{\mathbf{e}}_{2}=\left\| \vec{\mathbf{d}}_{1} \right\| \left\| \vec{\mathbf{e}}_{2} \right\|\cos(\angle ( \vec{\mathbf{d}}_{1},\vec{\mathbf{e}}_{2}))\end{align*}

where \left\| \vec{\mathbf{d}}_{1} \right\| and \left\| \vec{\mathbf{e}}_{2} \right\| are the intensities of the vectors \vec{\mathbf{d}}_{1} and \vec{\mathbf{e}}_{2}, respectively, and \cos(\angle (\vec{\mathbf{d}}_{1},\vec{\mathbf{e}}_{2})) is the cosine of the angle between the vectors \vec{\mathbf{d}}_{1} and \vec{\mathbf{e}}_{2}. Since both \vec{\mathbf{d}}_{1} and \vec{\mathbf{e}}_{2} are unit vectors, we have

(7)   \begin{align*}\left\| \vec{\mathbf{d}}_{1} \right\|=1,\;\; \left\| \vec{\mathbf{e}}_{2} \right\|=1\end{align*}

In addition, we have

(8)   \begin{align*}\cos(\angle (\vec{\mathbf{d}}_{1},\vec{\mathbf{e}}_{2}) )=\cos(\alpha_{12})\end{align*}

By substituting the last two equations in (6), we obtain

(9)   \begin{align*}\vec{\mathbf{d}}_{1}\cdot \vec{\mathbf{e}}_{2}=\cos(\alpha_{12})\end{align*}

Consequently, all the cosine terms in the equations (4) and (5) can be interpreted as the scalar products of \vec{\mathbf{d}}_{i} and \vec{\mathbf{e}}_{j}, i,j=1,2,3.

Next, we introduce the direction cosine matrix:

(10)   \begin{align*}\underline{C}=\begin{bmatrix} \cos(\alpha_{11}) & \cos(\alpha_{12}) & \cos(\alpha_{13})  \\ \cos(\alpha_{21}) & \cos(\alpha_{22}) & \cos(\alpha_{23}) \\ \cos(\alpha_{31}) & \cos(\alpha_{32}) & \cos(\alpha_{33}) \end{bmatrix} = \begin{bmatrix} \cos\big(\angle (\vec{\mathbf{d}}_{1},\vec{\mathbf{e}}_{1}) \big)  & \cos\big(\angle (\vec{\mathbf{d}}_{1},\vec{\mathbf{e}}_{2}) \big) & \cos\big(\angle (\vec{\mathbf{d}}_{1},\vec{\mathbf{e}}_{3}) \big) \\ \cos\big(\angle (\vec{\mathbf{d}}_{2},\vec{\mathbf{e}}_{1}) \big)  & \cos\big(\angle (\vec{\mathbf{d}}_{2},\vec{\mathbf{e}}_{2}) \big) & \cos\big(\angle (\vec{\mathbf{d}}_{2},\vec{\mathbf{e}}_{3}) \big) \\ \cos\big(\angle (\vec{\mathbf{d}}_{3},\vec{\mathbf{e}}_{1}) \big)  & \cos\big(\angle (\vec{\mathbf{d}}_{3},\vec{\mathbf{e}}_{2}) \big) & \cos\big(\angle (\vec{\mathbf{d}}_{3},\vec{\mathbf{e}}_{3}) \big)\end{bmatrix}\end{align*}

The direction cosine matrix can also be expressed as follows

(11)   \begin{align*}\underline{C}=\begin{bmatrix} \vec{\mathbf{d}}_{1}\cdot \vec{\mathbf{e}}_{1} & \vec{\mathbf{d}}_{1}\cdot \vec{\mathbf{e}}_{2} & \vec{\mathbf{d}}_{1}\cdot \vec{\mathbf{e}}_{3} \\  \vec{\mathbf{d}}_{2}\cdot \vec{\mathbf{e}}_{1} & \vec{\mathbf{d}}_{2}\cdot \vec{\mathbf{e}}_{2} & \vec{\mathbf{d}}_{2}\cdot \vec{\mathbf{e}}_{3} \\  \vec{\mathbf{d}}_{3}\cdot \vec{\mathbf{e}}_{1} & \vec{\mathbf{d}}_{3}\cdot \vec{\mathbf{e}}_{2} & \vec{\mathbf{d}}_{3}\cdot \vec{\mathbf{e}}_{3}  \end{bmatrix}\end{align*}

where the “dots” are used to denote the scalar product of vectors. Here, it is also important to observe the following. We can also represent the unit vectors \vec{\mathbf{e}}_{1}, \vec{\mathbf{e}}_{2}, and \vec{\mathbf{e}}_{3}, as follows

(12)   \begin{align*}\vec{\mathbf{e}}_{1}=\cos(\alpha_{11}) \vec{\mathbf{d}}_{1}+\cos(\alpha_{21}) \vec{\mathbf{d}}_{2}+\cos(\alpha_{31}) \vec{\mathbf{d}}_{3} \\\vec{\mathbf{e}}_{2}=\cos(\alpha_{12}) \vec{\mathbf{d}}_{1}+\cos(\alpha_{22}) \vec{\mathbf{d}}_{2}+\cos(\alpha_{32}) \vec{\mathbf{d}}_{3} \\\vec{\mathbf{e}}_{3}=\cos(\alpha_{13}) \vec{\mathbf{d}}_{1}+\cos(\alpha_{23}) \vec{\mathbf{d}}_{2}+\cos(\alpha_{33}) \vec{\mathbf{d}}_{3}\end{align*}

Direction Cosine Matrix, Orthogonality, and Determinant

At the beginning of this tutorial, we explained that all the components of unit vectors of both coordinate systems E and D should be represented with respect to some third coordinate system independent from the coordinate systems E and D. The third coordinate system is denoted by O. In this coordinate system, the unit vectors have the following components

(13)   \begin{align*}\vec{\mathbf{e}}_{1}=\begin{bmatrix}e_{11}  \\ e_{12} \\ e_{13}  \end{bmatrix},\; \vec{\mathbf{e}}_{2}=\begin{bmatrix}e_{21}  \\ e_{22} \\ e_{23}  \end{bmatrix},\; \vec{\mathbf{e}}_{3}=\begin{bmatrix}e_{31}  \\ e_{32} \\ e_{33}  \end{bmatrix} \\\vec{\mathbf{d}}_{1}=\begin{bmatrix}d_{11}  \\ d_{12} \\ d_{13}  \end{bmatrix},\; \vec{\mathbf{d}}_{2}=\begin{bmatrix}d_{21}  \\ d_{22} \\ d_{23}  \end{bmatrix},\; \vec{\mathbf{d}}_{3}=\begin{bmatrix}d_{31}  \\ d_{32} \\ d_{33}  \end{bmatrix}\end{align*}

For example e_{11}, e_{12}, e_{13} are the projections of the unit vector \vec{\mathbf{e}}_{1} onto the axes 1, 2, and 3 of the coordinate system O. Similarly, d_{31}, d_{32}, d_{33} are the projections of the unit vector \vec{\mathbf{d}}_{3} onto the axes 1, 2, and 3 of the coordinate system O. That is, the unit vectors of both coordinate systems are represented in O.

Next, let us consider the equations (4) and (5) once more

(14)   \begin{align*}\vec{\mathbf{d}}_{1}&=\cos(\alpha_{11}) \vec{\mathbf{e}}_{1}+\cos(\alpha_{12}) \vec{\mathbf{e}}_{2}+\cos(\alpha_{13}) \vec{\mathbf{e}}_{3} \\\vec{\mathbf{d}}_{2}& =\cos(\alpha_{21}) \vec{\mathbf{e}}_{1}+\cos(\alpha_{22}) \vec{\mathbf{e}}_{2}+\cos(\alpha_{23}) \vec{\mathbf{e}}_{3} \\\vec{\mathbf{d}}_{3}&=\cos(\alpha_{31}) \vec{\mathbf{e}}_{1}+\cos(\alpha_{32}) \vec{\mathbf{e}}_{2}+\cos(\alpha_{33}) \vec{\mathbf{e}}_{3}\end{align*}

How can we write these equations in the matrix format by using the direction cosine matrix?

By using (13), we can represent the first equation in (14) as follows

(15)   \begin{align*}\vec{\mathbf{d}}_{1}& =\begin{bmatrix}d_{11} \\ d_{12} \\ d_{13}  \end{bmatrix}=\cos(\alpha_{11})\begin{bmatrix}e_{11}  \\ e_{12} \\ e_{13}  \end{bmatrix}+\cos(\alpha_{12}) \begin{bmatrix}e_{21}  \\ e_{22} \\ e_{23}  \end{bmatrix}+\cos(\alpha_{13}) \begin{bmatrix}e_{31}  \\ e_{32} \\ e_{33}  \end{bmatrix} \\\vec{\mathbf{d}}_{1}& =\begin{bmatrix}d_{11} \\ d_{12} \\ d_{13}  \end{bmatrix}=\begin{bmatrix} e_{11} & e_{21} & e_{31} \\ e_{12} & e_{22} & e_{33} \\ e_{13} & e_{23} & e_{33}  \end{bmatrix}\begin{bmatrix} \cos(\alpha_{11}) \\ \cos(\alpha_{12})  \\ \cos(\alpha_{13})  \end{bmatrix}\end{align*}

By using the same idea, we can represent two other equations in (14), as follows

(16)   \begin{align*}\vec{\mathbf{d}}_{2}=\begin{bmatrix}d_{21} \\ d_{22} \\ d_{23}  \end{bmatrix}=\begin{bmatrix} e_{11} & e_{21} & e_{31} \\ e_{12} & e_{22} & e_{33} \\ e_{13} & e_{23} & e_{33}  \end{bmatrix}\begin{bmatrix} \cos(\alpha_{21}) \\ \cos(\alpha_{22})  \\ \cos(\alpha_{23})  \end{bmatrix} \\\vec{\mathbf{d}}_{3}=\begin{bmatrix}d_{31} \\ d_{32} \\ d_{33}  \end{bmatrix}=\begin{bmatrix} e_{11} & e_{21} & e_{31} \\ e_{12} & e_{22} & e_{33} \\ e_{13} & e_{23} & e_{33}  \end{bmatrix}\begin{bmatrix} \cos(\alpha_{31}) \\ \cos(\alpha_{32})  \\ \cos(\alpha_{33})  \end{bmatrix}\end{align*}

The last two equations can be written compactly as follows

(17)   \begin{align*}\begin{bmatrix}  d_{11}  & d_{21} & d_{31} \\   d_{12}  & d_{22} & d_{32}  \\  d_{13}  & d_{23} & d_{33} \end{bmatrix} =\begin{bmatrix} e_{11} & e_{21} & e_{31} \\ e_{12} & e_{22} & e_{33} \\ e_{13} & e_{23} & e_{33}  \end{bmatrix}\begin{bmatrix} \cos(\alpha_{11})  & \cos(\alpha_{21}) & \cos(\alpha_{31}) \\ \cos(\alpha_{12})  & \cos(\alpha_{22}) & \cos(\alpha_{32})  \\ \cos(\alpha_{13})  & \cos(\alpha_{23}) & \cos(\alpha_{33}) \end{bmatrix}\end{align*}

The last equation can be written compactly in the matrix form

(18)   \begin{align*}\underline{D}=\underline{E}\underline{C}^{T}\end{align*}

where

(19)   \begin{align*}\underline{D}=\begin{bmatrix}  d_{11}  & d_{21} & d_{31} \\   d_{12}  & d_{22} & d_{32}  \\  d_{13}  & d_{23} & d_{33} \end{bmatrix} \\\underline{E}=\begin{bmatrix} e_{11} & e_{21} & e_{31} \\ e_{12} & e_{22} & e_{33} \\ e_{13} & e_{23} & e_{33}  \end{bmatrix}\end{align*}

and \underline{C} is the direction cosine matrix defined in (10). The matrices \underline{D} and \underline{E} are formed by placing the corresponding unit vectors next to each other. The meaning of the equation (18) is that we can take the unit vectors corresponding to the E coordinate system, and we can transform them into the unit vectors of the coordinate system D. Here, we should keep in mind that \underline{D} and \underline{E} are used to denote the matrices composed of unit vectors, and D and E are used to denote the corresponding coordinate systems.

On the other hand, let us rewrite the equations (12), again

(20)   \begin{align*}\vec{\mathbf{e}}_{1}=\cos(\alpha_{11}) \vec{\mathbf{d}}_{1}+\cos(\alpha_{21}) \vec{\mathbf{d}}_{2}+\cos(\alpha_{31}) \vec{\mathbf{d}}_{3} \\\vec{\mathbf{e}}_{2}=\cos(\alpha_{12}) \vec{\mathbf{d}}_{1}+\cos(\alpha_{22}) \vec{\mathbf{d}}_{2}+\cos(\alpha_{32}) \vec{\mathbf{d}}_{3} \\\vec{\mathbf{e}}_{3}=\cos(\alpha_{13}) \vec{\mathbf{d}}_{1}+\cos(\alpha_{23}) \vec{\mathbf{d}}_{2}+\cos(\alpha_{33}) \vec{\mathbf{d}}_{3}\end{align*}

From these equations, we can use the identical procedure to the procedure used to derive (18) to derive the following matrix equation

(21)   \begin{align*}\underline{E}=\underline{D}\underline{C}\end{align*}

Let us next substitute the equation (21) in the equation (18), as the result, we obtain

(22)   \begin{align*}\underline{D}=\underline{D}\underline{C}\underline{C}^{T}\end{align*}

From the last equation, we obtain the following equation

(23)   \begin{align*}\underline{C}\underline{C}^{T}=I\end{align*}

This means that

  1. The matrix \underline{C} is the orthogonal matrix.
  2. The inverse of the matrix \underline{C} is its transpose.

Furthermore, we can show that the determinant of the matrix C is equal to 1. From (23), we have

(24)   \begin{align*}\text{det}\big( \underline{C}\underline{C}^{T}\big) =\text{det}(I)\end{align*}

From the last equation, we have

(25)   \begin{align*}\text{det}\big(\underline{C}\big) \text{det}\big( \underline{C}^{T}\big) = \text{det}\big(\underline{C}\big) \text{det}\big(\underline{C}\big) =\Big(\text{det}\big(\underline{C}\big)\Big)^{2}=1\end{align*}

From the last equation, we can conclude that the determinant of C can either be +1 or -1. However, it can easily be shown that for the right-hand-sided coordinate system, this determinant should be equal to 1.

Using the Direction Cosine Matrix to Transform Vector from One Coordinate System to Another

Let us consider an arbitrary vector \vec{\mathbf{z}} with the projections in the O coordinate system

(26)   \begin{align*}\vec{\mathbf{z}}=\begin{bmatrix}z_{1} \\ z_{2} \\ z_{3}  \end{bmatrix}\end{align*}

First, let us compute the projections of the vector \vec{\mathbf{z}} onto the coordinate system E. We want to represent the vector \vec{\mathbf{z}} as follows

(27)   \begin{align*}\vec{\mathbf{z}}=z_{e1}\vec{\mathbf{e}}_{1}+z_{e2}\vec{\mathbf{e}}_{2}+z_{e3}\vec{\mathbf{e}}_{3}\end{align*}

where z_{e1}, z_{e2}, and z_{e3} are the projections of the vector \vec{\mathbf{z}} onto the axes of the coordinate system E. These projections can easily be computed as follows

(28)   \begin{align*}z_{e1}=\vec{\mathbf{z}}\cdot  \vec{\mathbf{e}}_{1} \\z_{e2}=\vec{\mathbf{z}}\cdot  \vec{\mathbf{e}}_{2}  \\z_{e3}=\vec{\mathbf{z}}\cdot  \vec{\mathbf{e}}_{3}\end{align*}

Now, let us introduce the following notation

(29)   \begin{align*}\vec{\mathbf{z}}^{e}=\begin{bmatrix} z_{e1} \\ z_{e2} \\ z_{e3}\end{bmatrix}\end{align*}

This notation represents the vector \vec{\mathbf{z}} in the coordinate system E. By using this notation, we can write the equation (27), as follows

(30)   \begin{align*}\vec{\mathbf{z}}=\begin{bmatrix} e_{11} & e_{21} & e_{31} \\ e_{12} & e_{22} & e_{33} \\ e_{13} & e_{23} & e_{33}  \end{bmatrix}\begin{bmatrix} z_{e1} \\ z_{e2} \\ z_{e3}\end{bmatrix}=\underline{E}\vec{\mathbf{z}}^{e}\end{align*}

On the other hand, we can represent the vector \vec{\mathbf{z}} in the coordinate system D:

(31)   \begin{align*}\vec{\mathbf{z}}=z_{d1}\vec{\mathbf{d}}_{1}+z_{d2}\vec{\mathbf{d}}_{2}+z_{d3}\vec{\mathbf{d}}_{3}\end{align*}

where

(32)   \begin{align*}z_{d1}=\vec{\mathbf{z}}\cdot  \vec{\mathbf{d}}_{1} \\z_{d2}=\vec{\mathbf{z}}\cdot  \vec{\mathbf{d}}_{2}  \\z_{d3}=\vec{\mathbf{z}}\cdot  \vec{\mathbf{d}}_{3}\end{align*}

and we can introduce the following notation

(33)   \begin{align*}\vec{\mathbf{z}}^{d}=\begin{bmatrix} z_{d1} \\ z_{d2} \\ z_{d3}\end{bmatrix}\end{align*}

Similarly to (30), we have

(34)   \begin{align*}\vec{\mathbf{z}}=\begin{bmatrix} d_{11} & d_{21} & d_{31} \\ d_{12} & d_{22} & d_{33} \\ d_{13} & d_{23} & d_{33}  \end{bmatrix}\begin{bmatrix} z_{d1} \\ z_{d2} \\ z_{d3}\end{bmatrix}=\underline{D}\vec{\mathbf{z}}^{d}\end{align*}

Our goal is to find an equation that will transform \vec{\mathbf{z}}^{e} directly to \vec{\mathbf{z}}^{d}. Let us start from the equation (30)

(35)   \begin{align*}\vec{\mathbf{z}}=\underline{E}\vec{\mathbf{z}}^{e}\end{align*}

By substituting \underline{E} from the equation (21) into this equation, we obtain

(36)   \begin{align*}\vec{\mathbf{z}}=\underline{D}\underline{C}\vec{\mathbf{z}}^{e}\end{align*}

On the other hand, the equation (34) gives us the following relationship

(37)   \begin{align*}\vec{\mathbf{z}}=\underline{D}\vec{\mathbf{z}}^{d}\end{align*}

From (36) and (37), we have

(38)   \begin{align*}\underline{D}\vec{\mathbf{z}}^{d}=\underline{D}\underline{C}\vec{\mathbf{z}}^{e}\end{align*}

Since the matrix \underline{D} is invertible (non-singular) from the last equation we obtain

(39)   \begin{align*}\vec{\mathbf{z}}^{d}=\underline{C}\vec{\mathbf{z}}^{e}\end{align*}

The last equation is very important. It tells us how to transform the projections of a vector from one coordinate system to another. If we know the projections of the vector \vec{\mathbf{z}} in the coordinate system E, we can multiply the vector \vec{\mathbf{z}} represented in the coordinate system E by the direction cosine matrix to compute the representation of this vector in the coordinate system D.