Math

Other ways to construct this sort of rotation between between 3D vectors $\vec f$ to $\vec t$? Maybe another matrix?

Step-by-step mathematics solution: Other ways to construct this sort of rotation between between 3D vectors $\vec f$ to $\vec t$? Maybe another matrix?

As an Amazon Associate, I earn from qualifying purchases. For more practice problems like this, see Schaum’s Outline of Calculus, 7th Edition.


1. What the problem is asking (in plain language)

You have

  • a reference (model) unit vector (\hat f) and
  • a measured unit vector (\hat t) that is the same physical direction after the platform has been rotated by an (unknown) 3‑D attitude.

You want a rotation matrix (R) that carries (\hat f) onto (\hat t) (the minimal rotation, i.e. the one with the smallest angle).

Then you want to express that same rotation in the usual Tait–Bryan (yaw‑pitch‑roll) angles ((\alpha,\beta,\gamma)) that the GPS device reports.

When you try to set the Rodrigues‑derived matrix equal to the product of the three elementary yaw‑pitch‑roll matrices, you obtain angles that do not match the GPS values.
The task is to show the correct way to

  1. build the rotation matrix from (\hat f) and (\hat t) (using Rodrigues or an equivalent closed‑form), and
  2. extract the yaw, pitch and roll from that matrix, explaining why a naïve “equate the two matrices and read off the entries” gives the wrong answer.

2. Detailed derivation

2.1 Minimal rotation that maps (\hat f) to (\hat t)

Let

  • (\hat f) and (\hat t) be unit vectors ((|\hat f|=|\hat t|=1)).
  • (\mathbf{v}= \hat f\times\hat t) (the axis of rotation).
  • (\sin\theta = |\mathbf{v}|) and (\cos\theta = \hat f\cdot\hat t).

If (\hat f\neq\pm\hat t) the axis is non‑zero and we can normalise it:

[ \mathbf{k}= \frac{\hat f\times\hat t}{|\hat f\times\hat t|}, \qquad \theta = \operatorname{atan2}\bigl(|\hat f\times\hat t|,\;\hat f\cdot\hat t\bigr). ]

The Rodrigues formula gives the rotation matrix that rotates any vector through angle (\theta) about (\mathbf{k}):

[ R = I + \sin\theta\;[\,\mathbf{k}\,]\times + (1-\cos\theta)\;[\,\mathbf{k}\,]\times^{2}, \tag{R1} ]

where ([\,\mathbf{k}\,]_\times) is the skew‑symmetric cross‑product matrix

[ [\,\mathbf{k}\,]_\times= \begin{bmatrix} 0 & -k_z & k_y
k_z & 0 & -k_x
-k_y & k_x & 0 \end{bmatrix}. ]


2.1.1 A closed‑form without the explicit angle

Using the identities

[ \sin\theta = |\hat f\times\hat t|,\qquad \cos\theta = \hat f\cdot\hat t, ]

and the fact that ([\,\mathbf{k}\,]_\times) can be written as

[ [\,\mathbf{k}\,]\times = \frac{[\hat f\times\hat t]\times}{|\hat f\times\hat t|}, ]

the matrix (R1) can be rearranged to a form that depends only on (\hat f) and (\hat t).
A convenient expression (the one you quoted) is

[ \boxed{ R = I + 2\,\hat t\,\hat f^{!!T} \;-\;2\, \frac{(\hat t+\hat f)(\hat t+\hat f)^{!T}} {(\hat t+\hat f)^{!T}(\hat t+\hat f)} } \tag{R2} ]

provided (\hat t\neq -\hat f).
If (\hat t = -\hat f) the rotation is a 180° turn about any axis orthogonal to (\hat f); one may choose, for instance,

[ R = I - 2\,\hat f\,\hat f^{!!T}. ]


2.2 Converting a rotation matrix to yaw‑pitch‑roll (ZYX Tait‑Bryan)

The GPS attitude convention is

[ R(\alpha,\beta,\gamma)=R_z(\alpha)\,R_y(\beta)\,R_x(\gamma), \tag{RzRyRx} ]

with

[ R_z(\alpha)=\begin{bmatrix} c_\alpha &-s_\alpha &0
s_\alpha & c_\alpha &0
0&0&1 \end{bmatrix},\qquad R_y(\beta)=\begin{bmatrix} c_\beta &0& s_\beta
0&1&0
-s_\beta&0&c_\beta \end{bmatrix}, \qquad R_x(\gamma)=\begin{bmatrix} 1&0&0
0&c_\gamma&-s_\gamma
0&s_\gamma& c_\gamma \end{bmatrix}, ]

where (c_*=\cos(*)) and (s_*=\sin(*)).

Multiplying the three matrices gives the explicit form

[ R(\alpha,\beta,\gamma)= \begin{bmatrix} c_\alpha c_\beta & c_\alpha s_\beta s_\gamma - s_\alpha c_\gamma & c_\alpha s_\beta c_\gamma + s_\alpha s_\gamma\[2mm] s_\alpha c_\beta & s_\alpha s_\beta s_\gamma + c_\alpha c_\gamma & s_\alpha s_\beta c_\gamma - c_\alpha s_\gamma\[2mm]

  • s_\beta & c_\beta s_\gamma & c_\beta c_\gamma \end{bmatrix}. \tag{Rzyx} ]

Given a known rotation matrix (R) (for us, the matrix computed by (R2)), the yaw‑pitch‑roll angles are obtained by solving the above system for (\alpha,\beta,\gamma).
The standard, numerically stable formulas are

[ \boxed{ \begin{aligned} \beta &= \operatorname{atan2}(-R_{31},\;\sqrt{R_{11}^{2}+R_{21}^{2}}) ,\[4pt] \alpha &= \operatorname{atan2}(R_{21},\;R_{11}),\[4pt] \gamma &= \operatorname{atan2}(R_{32},\;R_{33}), \end{aligned}} \tag{Angles} ]

where (R_{ij}) denotes the entry in row (i), column (j) of the matrix (R).
(If (|\beta|) is close to (90^\circ) one falls into a gimbal‑lock case; then (\alpha) and (\gamma) are not uniquely defined and a different extraction formula must be used – see the “Common Mistakes” section.)


2.3 Putting it all together

  1. Normalise the two vectors

    [ \hat f = \frac{\vec f}{|\vec f|},\qquad \hat t = \frac{\vec t}{|\vec t|}. ]

  2. Build the rotation matrix

    If (\hat t\neq -\hat f): use (R2) directly

    [ R = I + 2\,\hat t\,\hat f^{!!T} -2\,\frac{(\hat t+\hat f)(\hat t+\hat f)^{!T}} {(\hat t+\hat f)^{!T}(\hat t+\hat f)} . ]

    If (\hat t = -\hat f): pick any unit vector (\mathbf{u}) orthogonal to (\hat f) and set

    [ R = I - 2\,\mathbf{u}\,\mathbf{u}^{!!T} \quad\text{(or the simpler $R = I-2\hat f\hat f^{!T}$, a 180° turn about any axis perpendicular to $\hat f$).} ]

  3. Extract yaw‑pitch‑roll with (Angles).
    Concretely:

    beta  = atan2( -R[2,0] , sqrt( R[0,0]^2 + R[1,0]^2 ) )
    alpha = atan2(  R[1,0] , R[0,0] )
    gamma = atan2(  R[2,1] , R[2,2] )
    

    (Indices start at 0 in most programming languages; replace accordingly.)

  4. Compare the obtained ((\alpha,\beta,\gamma)) with the GPS‑reported values.
    If the vectors are truly the same physical direction, the two triplets should match up to the usual ambiguities of Euler angles (e.g., adding (2\pi) to any angle, or the sign change that occurs when the rotation is expressed as a 180° flip).


2.4 Why directly equating the two matrix expressions fails

The matrix (R2) already is the unique orthogonal matrix that rotates (\hat f) onto (\hat t) by the smallest possible angle.

When you write

[ R(\alpha,\beta,\gamma)=R, ]

you are solving for three unknown angles in nine equations (the nine entries of the matrices).
Because the three elementary rotations do not commute, the nine scalar equations are not independent; many different ((\alpha,\beta,\gamma)) triples produce the same rotation matrix (they differ by multiples of (2\pi) or by the singularities at (\beta=\pm 90^\circ)).

If you simply compare individual entries (e.g., set the ((1,3)) entry of (R2) equal to the ((1,3)) entry of the product matrix and solve), you are imposing extra constraints that are not satisfied by the true Euler‑angle solution; the resulting angles are therefore off.

The correct approach is to first compute the matrix, then apply the standard inverse mapping (the atan2 formulas above) which is derived precisely to handle the non‑linear coupling between the three rotations.


3. Final answer

The minimal‑angle rotation matrix that carries the unit vector (\hat f) onto (\hat t) is

[ \boxed{ R= \begin{cases} I + 2\,\hat t\,\hat f^{!!T} -2\displaystyle\frac{(\hat t+\hat f)(\hat t+\hat f)^{!T}} {(\hat t+\hat f)^{!T}(\hat t+\hat f)}, & \hat t\neq -\hat f,\[12pt] I-2\,\hat f\,\hat f^{!!T}, & \hat t = -\hat f . \end{cases}} ]

From this matrix the yaw ((\alpha)), pitch ((\beta)) and roll ((\gamma)) in the GPS Z‑Y‑X (yaw‑pitch‑roll) convention are obtained by

[ \boxed{ \begin{aligned} \beta &= \operatorname{atan2}!\bigl(-R_{31},\;\sqrt{R_{11}^{2}+R_{21}^{2}}\bigr),
\alpha &= \operatorname{atan2}!\bigl(R_{21},\;R_{11}\bigr),
\gamma &= \operatorname{atan2}!\bigl(R_{32},\;R_{33}\bigr). \end{aligned}} ]

These angles, possibly adjusted by integer multiples of (2\pi), are the ones that should agree with the GPS‑provided attitude values.


4. Common mistakes

Mistake Why it’s wrong How to avoid it
Using (\theta = \arccos(\hat f\cdot\hat t)) alone (ignoring the sign of the cross product) (\arccos) returns only the principal angle in ([0,\pi]); the direction of rotation (sign of (\theta)) is lost. Compute (\theta) with atan2(‖f×t‖, f·t) which keeps the sign information.
Equating individual matrix entries of (R2) and the product (R_zR_yR_x) The nine scalar equations are not independent; they over‑constrain the three unknown angles, leading to spurious solutions. First compute the full matrix (R); then use the dedicated inverse‑Euler‑angle

Original question: Other ways to construct this sort of rotation between between 3D vectors $\vec f$ to $\vec t$? Maybe another matrix? on Mathematics Stack Exchange, licensed CC BY-SA.