title: Decomposing rotation matrix (x,y',z'') - Cartesian angles
body: <h2>Decomposing rotation matrix (x,y',z'') - Cartesian angles</h2>
  
  <p>Im currently working with <strong>rotation matrices</strong> and I have the following problem:
  Given three coordinate systems (<code>O0,x0,y0,z0; O1,x1,y1,z1; O2,x2,y2,z2</code>) which coincide. We rotate first the frame #1 with the respect to frame #0, then the frame #2 with respect to frame #1.</p>
  
  <p>The order of the rotations: <code>R = Rx_alpha * Ry_beta * Rz_gamma</code>, so first about <strong><em>x</em></strong>, then <strong><em>y'</em></strong>, then <strong><em>z''</em></strong>, which are also known as the <strong>Cartesian angles</strong>. 
  If R1 stands for the 1st and R2 for the 2nd rotation, we are looking for the angles of the 2nd frame with respect to initial frame (#0) after both of the rotations. This can be done by <strong>decomposing the rotation matrix</strong> R (where:<code>R = R1*R2</code> ). There are many literature available, how it can be done by Euler- and RPY-angles, but I don't find any, how to solve this problem in case of Cartesian angles.</p>
  
  <p>I have a <strong>matlab function</strong> which works only by simple rotations. If all the angles have values different than 0 (example below), then the result becomes really unstable.</p>
  
  <p>Orientation of the 1st frame with respect to the frame #0:</p>
  
  <pre><code>    alpha1 = 30*pi/180;
      beta1 = 10*pi/180;
      gamma1 = 0*pi/180;
  </code></pre>
  
  <p>Orientation of the 2nd frame with respect to the frame #1</p>
  
  <pre><code>    alpha2 = 10*pi/180;
      beta2 = 10*pi/180;
      gamma2 = 0*pi/180;
  </code></pre>
  
  <p>The matlab function I was using for solving the problem:</p>
  
  <pre><code>function [q] = cartesian_angles(R)
  
  beta = asin(R(1,3));
  
  *% Catching the numerical singularty*
  if abs(abs(beta)-pi/2) &gt; eps;
      *% singulartiy of acos*
      gamma1 = acos(R(1,1) / cos(beta));
      gamma2 = asin(-R(1,2) / cos(beta));
      if gamma2&lt;0
          gamma=2*pi-gamma1;
      else
          gamma=gamma1;
      end
      alpha1 = acos(R(3,3) / cos(beta));
      alpha2 = asin(-R(2,3) / cos(beta));
      if alpha2&lt;0
          alpha = 2*pi-alpha1;
      else
          alpha = alpha1;
      end
  else
      fprintf('beta=pi/2 \n')
      gamma = 0;
      alpha = 0;
      beta  = 0;
  end;
  
  alpha = alpha*180/pi;
  beta = beta*180/pi;
  gamma = gamma*180/pi;
  
  q = [alpha; beta; gamma];
  </code></pre>
  
  <p>Thank you for any help! If you have some questions don't hesitate to ask!</p>
  
  <p>Marci</p>