# How to derive rotation matrix by Euler angles

We rotate an object around the axis of a coordinate system rotating together with the object. First we rotate it by $\phi$ angle around the $z$ axis, then by $\theta$ around the $y$ axis and finally by $\psi$ around the $z$ axis. The rotation matrix representing the position transformation for this operation is
. The three angles, $\phi$, $\theta$, and $\psi$ are called Euler angle.

### Proof

Let a coordinate system $C$ be the Cartesian coordinate system, whose axes are represented as $x, y, z$. And $\mathbf{e}_{x}$, $\mathbf{e}_{y}$, and $\mathbf{e}_{z}$ be unit vectors whose direction is $x$, $y$, and $z$ axis, respectively. These form the orthonormal basis of $C$.
Let $\mathbf{r}$ the position of an object. By the orthonormal basis, it can be represented as
. We rotate this object in the following order.
$(1)$   Rotation around the $z$ axis:
We rotate the object by angle $\phi$ about the $z$ axis of $C$. We rotate not only the object but also the coordinate system $C$ in the same way, and define the rotated coordinate system as the new coordinate system $C'$. ($C'$ is the coordinate system obtained by rotating $C$ by angle $\phi$ about the $z$ axis of $C$.)
coordinate $C$ (dotted line) and $C'$ (orange line)
Let $x'$, $y'$, and $z'$ be the axes of $C'$ , and $\mathbf{e}_{x}'$, $\mathbf{e}_{y}'$, and $\mathbf{e}_{z}'$ be unit vectors whose direction is $x'$, $y'$, and $z'$ axis, respectively. These form the orthonormal basis of $C'$.
Let $\mathbf{r}'$ be the position of the object after the rotation. Since the object and the coordinate system rotate in the same way, the relative position of $C'$ and $\mathbf{r}'$ after rotation is the same as that of $C$ and $\mathbf{r}$ before rotation (see the figure below).
Rotation of the coordinate and the position viewed from above of the $z$ axis. The relative position of them is invariant under rotation.
Therefore, the coordinate value of $\mathbf{r}'$ with respect to the basis of $C'$ is the same as that of $\mathbf{r}$ with respect to the basis of $C$ (the above expression of $\mathbf{r}$). That is, $\mathbf{r}'$ can be represented as
By the basis $\{ \mathbf{e}_{x}, \mathbf{e}_{y}, \mathbf{e}_{z} \}$, the basis $\{ \mathbf{e}_{x}', \mathbf{e}_{y}', \mathbf{e}_{z}' \}$ is represented as
.
$(2)$   Rotation around the $y'$ axis:
Next, we rotate the object by angle $\theta$ about the $y'$ axis of $C'$. We rotate not only the object but also the coordinate system $C'$ in the same way, and define the rotated coordinate system as the new coordinate system $C''$. ($C''$ is the coordinate system obtained by rotating $C'$ by angle $\theta$ about the $y'$ axis of $C'$.)
Coordinate systems $C$, $C'$ (dotted line) and $C''$ (oragne line)
Let $x''$, $y''$, and $z''$ be the axes of $C''$ , and $\mathbf{e}_{x}''$, $\mathbf{e}_{y}''$, and $\mathbf{e}_{z}''$ be unit vectors whose direction is $x''$, $y''$, and $z''$ axis, respectively. These form the orthonormal basis of $C''$.
Let $\mathbf{r}''$ be the position of the object after the rotation. Since the object and the coordinate system rotate in the same way, the relative position of $C''$ and $\mathbf{r}''$ after rotation is the same as that of $C'$ and $\mathbf{r}'$ before rotation (see the figure below).
Rotation of the coordinate and the position viewed from above of the $y'$ axis. The relative position of them is invariant under rotation.
Therefore, the coordinate value of $\mathbf{r}''$ with respect to the basis of $C''$ is the same as that of $\mathbf{r}'$ with respect to the basis of $C'$ (the above expression of $\mathbf{r}'$). That is, $\mathbf{r}''$ can be represented as
. By the basis $\{ \mathbf{e}_{x}', \mathbf{e}_{y}', \mathbf{e}_{z}' \}$, the basis $\{ \mathbf{e}_{x}'', \mathbf{e}_{y}'', \mathbf{e}_{z}'' \}$ is represented as
.
$(3)$   Rotation around the $z''$ axis:
Finally, we rotate the object by angle $\psi$ about the $z''$ axis of $C''$. We rotate not only the object but also the coordinate system $C''$ in the same way, and define the rotated coordinate system as the new coordinate system $C'''$. ($C'''$ is the coordinate system obtained by rotating $C''$ by angle $\psi$ about the $z''$ axis of $C''$.)
Coordinate systems $C$, $C'$, $C''$ (dotted line) and $C'''$ (orange line)

Let $x'''$, $y'''$, and $z'''$ be the axes of $C'''$ , and $\mathbf{e}_{x}'''$, $\mathbf{e}_{y}'''$, and $\mathbf{e}_{z}'''$ be unit vectors whose direction is $x'''$, $y'''$, and $z'''$ axis, respectively. These form the orthonormal basis of $C'''$.
Let $\mathbf{r}'''$ be the position of the object after the rotation. Since the object and the coordinate system rotate in the same way, the relative position of $C'''$ and $\mathbf{r}'''$ after rotation is the same as that of $C''$ and $\mathbf{r}''$ before rotation (see the figure below).
Rotation of the coordinate and the position viewed from above of the $z''$ axis. The relative position of them is invariant under rotation.
Therefore, the coordinate value of $\mathbf{r}'''$ with respect to the basis of $C'''$ is the same as that of $\mathbf{r}''$ with respect to the basis of $C''$ (the above expression of $\mathbf{r}''$). That is, $\mathbf{r}'''$ can be represented as
. By the basis $\{ \mathbf{e}_{x}'', \mathbf{e}_{y}'', \mathbf{e}_{z}'' \}$, the basis $\{ \mathbf{e}_{x}''', \mathbf{e}_{y}''', \mathbf{e}_{z}''' \}$ is represented as
.
From $(*)$, $(**)$, and $(***)$, the orthonormal basis $\{ \mathbf{e}_{x}''', \mathbf{e}_{y}''', \mathbf{e}_{z}''' \}$, which appeared as a result of the above three rotations, can be represented by the basis $\{ \mathbf{e}_{x}, \mathbf{e}_{y}, \mathbf{e}_{z} \}$:
. From these, the position of the object after the three rotation can be represented by the basis $\{ \mathbf{e}_{x}, \mathbf{e}_{y}, \mathbf{e}_{z} \}$:
.
Let $r_{x}'''$, $r_{y}'''$, and $r_{z}'''$ be coordinates values of $\mathbf{r}'''$ represented by the basis $\{ \mathbf{e}_{x}, \mathbf{e}_{y}, \mathbf{e}_{z} \}$:
. Since this is equal to $(*4)$, we see that
. By using matrix representaion, this relation can be written as
.
Here, $r_{x}''',r_{y}''',r_{z}'''$ is the position of the object after the three rotations, and $r_{x},r_{y},r_{z}$ is the initial position. Therefore, the above transformation formula shows that the position of the object after the three rotations is obtained by operationg the rotation matrix
to the initial position.
The three angles $\phi,\theta,\psi$ are caled Euler angles.