# rotation angles from rotation matrix

Discussion in 'Mathematica' started by will, Aug 11, 2007.

1. ### willGuest

Dear Math forum,

I found some useful code for retrieving the Euler angles of a rotation from a rotation matrix; see

http://forums.wolfram.com/mathgroup/archive/2001/Aug/msg00286.html

which goes as follows:

In[1]:=
Needs["Geometry`Rotations`"]

In[2]:=
B=RotationMatrix3D[Pi/3,Pi/4,Pi/6];

In[3]:=
Solve[RotationMatrix3D[Ï•,Î¸,Ïˆ]\[Equal]B,{Ï•,Î¸,Ïˆ}]

From In[3]:=
Solve::ifun: Inverse functions are being used by Solve, so some
solutions may \
not be found.

Out[3]=
{{\[Psi] -> Pi/6, \[Theta] -> Pi/4, \[Phi] -> Pi/3}}

However, I noticed that if instead of exact rotations being entered, numerical approximations are entered:

In[4]:=
A=RotationMatrix3D[Pi/3.,Pi/4.,Pi/6.];

then solve no longer finds the solution...

In[5]:=
Solve[RotationMatrix3D[Ï•,Î¸,Ïˆ]\[Equal]A,{Ï•,Î¸,Ïˆ}]

Out[5]=
{}

my problem is that i have a rotation matrix

{{0.977431, 0.158491, 0.139673}, {-0.0822761,
0.894559, -0.439311}, {-0.194572, 0.417905, 0.88741}}

and i really want to find the rotations that are described by this matrix, either in Euler angles (for mathematica vs 5.2), or (preferably) in xyz (pitch-roll-yaw) convention (for mathematica vs 6). I am sorry if this is a very simple problem, but i don't know much about matrix maths...

Will

will, Aug 11, 2007

2. ### sashapGuest

Hi Will,

the reason, I think, the solve was failing for inexact input is that
inexact matrix fails to satisfy the
same algebraic constraints that the symbolic does, hence Solve
concludes that there are no solutions:

In[64]:= m = {{0.977431, 0.158491, 0.139673}, {-0.0822761,
0.894559, -0.439311}, {-0.194572, 0.417905, 0.88741}};

In[66]:= Transpose[m].m - IdentityMatrix[3] // Norm

Out[66]= 1.38282*10^-6

In[10]:= rm[\[Phi]_, \[Theta]_, \[Psi]_] :=
RotationMatrix[\[Psi], {0, 1, 0}].RotationMatrix[\[Theta], {0, 0,
1}].RotationMatrix[\[Phi], {0, 1, 0}]

In[54]:= sm = rm[\[Phi], \[Theta], \[Psi]];

In[55]:= Transpose[sm].sm // Simplify

Out[55]= {{1,0,0},{0,1,0},{0,0,1}}

Now to how to find angles:

Observe that

In[59]:= sm[[2, 2]]

Out[59]= Cos[\[Theta]]

Thus

In[43]:= \[Theta]aprox = ArcCos[m[[2, 2]]]

Out[43]= 0.463353

Also

In[62]:= sm[[2, {1, 3}]]/Sin[\[Theta]]

Out[62]= {Cos[\[Phi]],Sin[\[Phi]]}

and thus we find another angle:

In[44]:= \[Phi]aprox =
Mod[ArcTan @@ (m[[2, {1, 3}]]/Sin[\[Theta]aprox]), 2 Pi]

Out[44]= 4.52725

And, finally,

In[63]:= sm[[{1, 3}, 2]]/Sin[\[Theta]]

Out[63]= {-Cos[\[Psi]],Sin[\[Psi]]}

In[48]:= \[Psi]aprox =
Mod[ArcTan @@ ({-1, 1}*m[[{1, 3}, 2]]/Sin[\[Theta]aprox]), 2 Pi]

Out[48]= 1.93329

Verify that the solution indeed gives your matrix back as much as it
is possible with machine numbers:

In[51]:= rm[\[Phi]aprox, \[Theta]aprox, \[Psi]aprox] - m // Norm

Out[51]= 1.35436*10^-6

Hope it helps,
Oleksandr Pavlyk
Wolfram Research

rical approximations are entered:
, either in Euler angles (for mathematica vs 5.2), or (preferably) in xyz (=
pitch-roll-yaw) convention (for mathematica vs 6). I am sorry if this is a =
very simple problem, but i don't know much about matrix maths...

sashap, Aug 12, 2007

3. ### robGuest

I gather all this is for V6; there is no RotationMatrix
function available on my 5.1 system. (RotationMatrix3D is
there though).

Either way it appears that using RotationMatrix3D to work
with Euler angles doesn't work in the real world.

rob, Aug 13, 2007
4. ### sashapGuest

Yes, RotationMatrix is new in version 6.
Rather, it does work in the real world, one just needs to properly
apply it.

Oleksandr Pavlyk
Wolfram Research

sashap, Aug 14, 2007
5. ### robGuest

OK, sure.

rob, Aug 16, 2007