Physics Library
 An open source physics library
Encyclopedia | Forums | Docs | Random  

[parent] example of quaternions and Euler angles

(Example)

Quaternions and Euler Angles: Examples, Exercises, and Solutions

This entry is the self study companion to the PhysicsLibrary article quaternions and Euler angles. All exercises are stated first. Complete worked solutions follow afterward.

The article uses the PhysicsLibrary passive frame convention. For the intrinsic moving axis 3-2-1 yaw, pitch, roll sequence,

ψ  = yaw,     𝜃 = pitch,     ϕ = roll,

and

BC   =  C (ϕ)C  (𝜃)C (ψ).
   A     1     2    3
(1)

The matching quaternion is

BqA = qP (ϕ)qP(𝜃)qP (ψ ),
       1     2    3
(2)

where

qP (α) = cos α-− e sin α.
 i           2    i    2
(3)

1 Formula summary

Define the half angle abbreviations

 h       ϕ-      h       ϕ-
cϕ = cos 2 ,    sϕ = sin 2,

 h      𝜃-      h       𝜃-
c𝜃 = cos2 ,    s𝜃 = sin 2,

ch = cos ψ-,    sh = sin ψ-.
 ψ       2       ψ       2

Then

qw = chϕch𝜃chψ + shϕsh𝜃shψ,
(4)

qx = chϕsh𝜃shψ − shϕch𝜃chψ,
(5)

qy = − chϕsh𝜃chψ − shϕch𝜃shψ,
(6)

qz = shϕsh𝜃chψ − chϕch𝜃shψ.
(7)

The passive 3-2-1 DCM is

       ⌊                                   ⌋
             c𝜃cψ           c𝜃sψ       − s𝜃
BCA  = ⌈ sϕs𝜃cψ − cϕsψ  sϕs𝜃sψ + cϕcψ  sϕc𝜃⌉ .
         cϕs𝜃cψ + s ϕsψ cϕs𝜃sψ − sϕcψ  cϕc𝜃
(8)

On the principal nonsingular branch,

          (                      2    2)
ϕ = atan2  2(qyqz − qwqx),1 − 2(qx + qy)  ,
(9)

𝜃 = arcsin (− 2(qxqz + qwqy)),
(10)

ψ = atan2 (2(q q  − q q ),1 − 2(q2+  q2)) .
              x y    w z         y    z
(11)

2 Exercises

  1. Pure yaw.

    Show that a positive passive frame yaw of angle ψ is represented by

           ψ         ψ
q = cos-- − k sin --.
        2        2
  2. Pure pitch.

    Show that a positive passive frame pitch of angle 𝜃 is represented by

            𝜃-       𝜃-
q =  cos2 − jsin 2.
  3. Pure roll.

    Show that a positive passive frame roll of angle ϕ is represented by

    q = cos ϕ-− isin ϕ.
        2        2
  4. Derive the passive 3  -2  -1  quaternion.

    Starting from

    B      P     P    P
 qA = q1 (ϕ)q2 (𝜃)q3 (ψ ),

    multiply the three elementary quaternions and derive qw,qx,qy,qz.

  5. Pure positive 90∘ yaw.

    For

                               π
ϕ =  0,    𝜃 = 0,     ψ =  -,
                           2

    compute Bq A.

  6. Pure positive 90∘ roll.

    For

         π
ϕ =  -,     𝜃 = 0,     ψ = 0,
     2

    compute Bq A.

  7. Pure positive 90∘ pitch.

    For

                    π
ϕ =  0,    𝜃 =  -,     ψ = 0,
                2

    compute Bq A.

  8. Derive the passive 3  -2  -1  DCM.

    Multiply

    BCA  = C1(ϕ )C2 (𝜃)C3(ψ )

    and derive the explicit matrix.

  9. Quaternion and DCM cross check.

    Use the quaternion from Exercise 5 to generate a DCM and verify that it agrees with the 3-2-1 matrix from Exercise 8 when

                               π
ϕ =  0,    𝜃 = 0,     ψ =  -.
                           2
  10. Recover a positive    ∘
60 yaw.

    Given the passive unit quaternion

        √ --
      3   1
q = ----− --k,
     2    2
    (12)

    recover the principal 3-2-1 yaw, pitch, and roll angles.

  11. Recover a positive    ∘
90 roll.

    Given

        √ --   √ --
      2      2
q = ----−  ---i,
     2      2
    (13)

    recover the principal yaw, pitch, and roll angles.

  12. Gimbal lock.

    Explain why the intrinsic 3-2-1 Euler Angle description becomes singular at

    𝜃 =  ± π.
       2

    State what happens to yaw and roll at the singularity.

  13. scalar last array error.

    A student receives a quaternion stored as

                T
[qx,qy,qz,qw ]

    but substitutes the four numerical entries directly into a formula that expects

    [qw,qx,qy,qz]T.

    Explain the error and how to correct it.

  14. Euler angle nonuniqueness.

    Explain why two different triples

    (ϕ,𝜃,ψ )

    can describe the same physical orientation.

    Give at least one source of nonuniqueness.

  15. Engineering representation choice.

    Why is it often advantageous to propagate attitude internally with a quaternion while displaying yaw, pitch, and roll to an operator?

    Include both numerical and geometric reasons.

3 Solutions

Solution 1: pure yaw

A positive frame yaw is a positive frame rotation about the 3 axis.

Under the PhysicsLibrary passive axis rule,

               α         α
qP (^u,α) = cos --− u^sin -.
               2         2

Set

^u = k,     α = ψ.

Therefore

       ψ-        ψ-
q = cos 2 − k sin 2 .
(14)

Solution 2: pure pitch

Pitch is the positive frame rotation about the 2 axis, so

        𝜃        𝜃
q =  cos--− jsin -.
        2        2
(15)

Solution 3: pure roll

Roll is the positive frame rotation about the 1 axis, so

        ϕ        ϕ
q = cos --− isin -.
        2        2
(16)

Solution 4: derive the passive 3  -2  -1  quaternion

Write

qP = ch − ish,
 1    ϕ     ϕ

 P    h     h
q2 = c𝜃 − js𝜃,

and

qP3 =  chψ − kshψ.

First multiply the pitch and yaw factors:

qP2 qP3 = (ch𝜃 − jsh𝜃)(chψ − kshψ)
        h h    h   h     h h      h h
     = c𝜃cψ − c𝜃ks ψ − js𝜃cψ + jks 𝜃sψ.

Since

jk =  i,

we have

qP qP = chch + ishsh − jshch−  kchsh.
 2  3    𝜃 ψ     𝜃 ψ     𝜃 ψ     𝜃 ψ

Now left multiply by

qP = ch − ish.
 1    ϕ     ϕ

Collecting scalar and vector terms gives

      h h h    h h h
qw = cϕc𝜃cψ + sϕs𝜃sψ,
(17)

      h h h    h h h
qx = cϕs𝜃sψ − sϕc𝜃cψ,
(18)

        h h h    h h h
qy = − cϕs𝜃cψ − sϕc𝜃sψ,
(19)

      h h h    h h h
qz = sϕs𝜃cψ − cϕc𝜃sψ.
(20)

These are exactly the passive formulas summarized at the beginning of the article.

Solution 5: pure positive 90∘ yaw

With

                           π-
ϕ =  0,    𝜃 = 0,     ψ =  2,

only the yaw quaternion remains:

Bq  = cos π-− k sin π-.
  A       4         4

Therefore

Bq  =  1√−-k-.
  A       2
(21)

Solution 6: pure positive 90∘ roll

Only the roll quaternion remains:

B         π        π
 qA =  cos--−  isin --.
          4        4

Thus

Bq   = 1√−-i.
   A      2
(22)

Solution 7: pure positive 90∘ pitch

Only the pitch quaternion remains:

B         π        π
 qA =  cos--−  jsin --.
          4        4

Therefore

Bq   = 1√−-j.
   A      2
(23)

Solution 8: derive the passive 3  -2  -1  DCM

The elementary passive matrices are

        ⌊            ⌋
          1   0    0
C1(ϕ ) = ⌈ 0  cϕ   sϕ⌉ ,
          0  − sϕ  cϕ

        ⌊            ⌋
          c𝜃  0  − s𝜃
C  (𝜃 ) = ⌈ 0  1   0  ⌉,
  2
          s𝜃  0   c𝜃

and

        ⌊            ⌋
           cψ   sψ  0
C3(ψ) = ⌈ − sψ  cψ  0⌉ .
           0    0   1

Multiply

C (ϕ )C (𝜃)C (ψ ).
  1    2    3

The result is

       ⌊                                   ⌋
             c𝜃cψ           c𝜃sψ       − s𝜃
BCA  = ⌈ sϕs𝜃cψ − cϕsψ  sϕs𝜃sψ + cϕcψ  sϕc𝜃⌉ .
         c s c + s  s   c s s  − s c   c c
          ϕ 𝜃 ψ    ϕ ψ   ϕ 𝜃 ψ    ϕ ψ   ϕ 𝜃
(24)

Solution 9: quaternion and DCM cross check

From Exercise 5,

q =  1 −√-k.
        2

The quaternion to DCM relation from Q09 gives

        ⌊         ⌋
          0   1  0
C (q) = ⌈− 1  0  0⌉ .
          0   0  1
(25)

Now set

                          π-
ϕ = 0,     𝜃 = 0,    ψ =  2

in the matrix from Exercise 8.

Then

c  = c  = 1,     s  = s  = 0,
 ϕ    𝜃           ϕ    𝜃

cψ = 0,     sψ = 1.

This gives

        ⌊         ⌋
B         0   1  0
  CA =  ⌈− 1  0  0⌉ ,
          0   0  1
(26)

which agrees exactly with C(q).

Solution 10: recover a positive 60 ∘ yaw

The quaternion is

    √3--  1
q = ----− --k.
     2    2

Thus

     √ --
qw = --3-,    qx = qy = 0,     qz = − 1-.
      2                              2

The roll formula gives

ϕ = atan2 (0,1) = 0.

The pitch formula gives

𝜃 = arcsin (0) = 0.

For yaw,

          (                  2)
ψ = atan2 (2(0 − qw)qz),1 − 2qz
            √3-- 1
  = atan2   ----,--
             2   2
     π
  =  3.

Therefore

ϕ =  0,    𝜃 = 0,     ψ =  π.
                           3
(27)

The orientation is a positive passive frame yaw of 60∘.

Solution 11: recover a positive 90 ∘ roll

The quaternion is

    √2--   √2--
q = ----−  ---i.
     2      2

Thus

     √ --            √ --
     --2-            --2-
qw =  2  ,    qx = −  2 ,     qy = qz = 0.

The roll angle is

          (               )
ϕ = atan2  − 2qwqx,1 − 2q2x

  = atan2 (1,0)
  = π-.
     2

The pitch and yaw expressions both give zero:

𝜃 = 0,     ψ = 0.

Therefore

     π
ϕ =  -,     𝜃 = 0,     ψ = 0.
     2
(28)

Solution 12: gimbal lock

For the passive 3-2-1 matrix,

C13 = − sin 𝜃.

At

𝜃 =  ± π,
       2

we have

cos𝜃 = 0.

The matrix terms used to recover yaw and roll lose independent information. The first and third rotation axes become aligned in the Euler construction.

At

𝜃 =  + π,
       2

the orientation depends on the combination

ϕ −  ψ.

At

𝜃 =  − π,
       2

it depends on

ϕ +  ψ.

Thus yaw and roll cannot be recovered independently. The physical attitude remains valid; only the Euler coordinate description becomes singular.

Solution 13: scalar last array error

The Euler extraction formulas assign specific meanings to

qw,qx,qy,qz.

If software stores

            T
[qx,qy,qz,qw ]

and those four numerical values are interpreted directly as

             T
[qw,qx,qy,qz] ,

every semantic component is assigned to the wrong symbol.

The array must first be remapped:

[qx,qy,qz,qw]T − → [qw, qx,qy,qz]T.

Changing memory layout does not change Hamilton multiplication, frame direction, or active versus passive interpretation.

Solution 14: Euler angle nonuniqueness

Euler Angles are not a globally unique orientation parameterization.

The simplest source of nonuniqueness is angular periodicity. For example,

(ϕ,𝜃,ψ )

and

(ϕ + 2π, 𝜃,ψ)

represent the same orientation.

Inverse trigonometric branch choices also produce equivalent descriptions.

At the gimbal lock singularity, the nonuniqueness becomes stronger because yaw and roll cannot be determined independently.

Therefore equality of two orientations should not be tested merely by checking whether their Euler triples are numerically identical.

Solution 15: engineering representation choice

Quaternions are useful internal attitude states because they:

  1. avoid the 3-2-1 gimbal lock singularity;
  2. compose efficiently with Hamilton products;
  3. are convenient for integrating angular velocity;
  4. require only four stored components rather than nine DCM components;
  5. can be renormalized easily to control numerical drift;
  6. work naturally with estimation, interpolation, and attitude control.

Euler angles remain useful because yaw, pitch, and roll are intuitive for operators, plots, displays, and many engineering requirements.

A common architecture is therefore

gyro data −→  quaternion attitude state − → Euler angles for display.

The internal state remains numerically robust while the external presentation remains physically intuitive.

4 Compact passive 3  -2  -1  checks



Case PhysicsLibrary passive result


Pure positive yaw ψ  q = cos(ψ∕2) − k sin(ψ∕2)


Pure positive pitch 𝜃  q = cos(𝜃∕2) − j sin(𝜃∕2)


Pure positive roll ϕ  q = cos(ϕ∕2) − i sin(ϕ∕2)


Intrinsic 3  -2  -1  q = q1P (ϕ)q 2P (𝜃)q 3P (ψ)


Matching DCM C = C1(ϕ)C2(𝜃)C3(ψ)


Positive 90∘ yaw q = (1 − k)∕√ --
  2


Gimbal lock 𝜃 = ±π∕2


5 Sources and exercise provenance

The exercises and solutions in this companion are rewritten for PhysicsLibrary under the passive intrinsic 3-2-1 convention.

Henderson provides an important aerospace reference for Euler angle, quaternion, and transformation matrix relationships. Moore provides an openly licensed modern treatment of reference frame orientation. Sommer and coauthors provide a modern analysis of quaternion convention choices.

References

[1]   D. M. Henderson, Euler Angles, Quaternions, and Transformation Matrices: Working Relationships, JSC-12960, NASA Johnson Space Center, Mission Planning and Analysis Division, 1977. Engineering reference. NASA Technical Reports Server search

[2]   J. K. Moore, Learn Multibody Dynamics, chapter “Orientation of Reference Frames.” Distributed under CC BY 4.0. Learn Multibody Dynamics

[3]   H. Sommer, I. Gilitschenski, M. Bloesch, S. Weiss, R. Siegwart, and J. Nieto, “Why and How to Avoid the Flipped Quaternion Multiplication,” Aerospace, vol. 5, no. 3, article 72, 2018. Published under CC BY 4.0. Publisher article

[4]   W. R. Hamilton, Elements of Quaternions, 2nd ed., edited by C. J. Joly, Longmans, Green, and Co., 1899. Public domain historical source. Internet Archive scan

License

Unless otherwise noted, this PhysicsLibrary entry is intended for release under the Creative Commons Attribution ShareAlike 4.0 International license.


"example of quaternions and Euler angles" is owned by bloftin.
(view preamble)
View style:
See Also: quaternion series overview and article guide, Quaternions for Physics and Engineering: Orientation, Notation, and Conventions, quaternion definition and basic algebra, example of quaternion definition and basic algebra, quaternion product, example of quaternion product, quaternion conjugate, example of quaternion conjugate, quaternion norm, example of quaternion norm, quaternion inverse, example of quaternion inverse

Keywords:  quaternion, Euler angles, yaw pitch roll, 321 sequence, gimbal lock, Tait-Bryan angles, exercises, worked solutions

This object's parent.

Cross-references: operators, work, angular velocity, Euler Angles, relation, vector, operator, representation, formula, scalar, Euler Angle, unit, matrix, quaternion, quaternions and Euler angles
There is 1 reference to this object.

This is version 2 of example of quaternions and Euler angles, born on 2026-08-24, modified 2026-08-27.
Object id is 1104, canonical name is ExampleOfQuaternionsAndEulerAngles.
Accessed 264 times total.

Classification:
Physics Classification: 02.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

No messages.

Interact