Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is this the correct way to calculate a rotation matrix for a given angle around a unit vector, i am having problems verifying it.

function R = rot(k,fi)
% This is just to make it easier to read!
x = k(1);
y = k(2);
z = k(3);

% Create a 3x3 zero matrix
R = zeros(3,3);
% We use the formula for rotating a matrix about a unit vector k

R(1,1) = cos(fi)+x^2*(1-cos(fi));
R(1,2) = x*y*(1-cos(fi))-z*sin(fi);
R(1,3) = x*y*(1-cos(fi))+y*sin(fi);

R(2,1) = y*x*(1-cos(fi))+z*sin(fi);
R(2,2) = cos(fi)+y^2*(1-cos(fi));
R(2,3) = y*z*(1-cos(fi))-x*sin(fi);

R(3,1) = z*x*(1-cos(fi))-y*sin(fi);
R(3,2) = z*y*(1-cos(fi))+x*sin(fi);
R(3,3) = cos(fi)+z^2*(1-cos(fi));

I am using this algorithm Rotation Matrix

share|cite|improve this question
There are is a minor mistake: In R(1,3), the y should be a z. Otherwise it looks fine. – utdiscant Oct 24 '12 at 7:00
up vote 0 down vote accepted

If you change the R(1,3) line to

R(1,3) = x*z*(1-cos(fi))+y*sin(fi);

and plug the above code into Matlab, then one way to see that the code is behaving as it should be, is to run

rot([1 0 0],pi)

which gives the matrix

ans =

    1.0000         0         0
         0   -1.0000   -0.0000
         0    0.0000   -1.0000

and this is what one would expect, since rotating 180 degrees around the $x$-axis corresponds to negating the $y$ and $z$-coordinates.

share|cite|improve this answer

You can write this in a more compact way using Rodrigues' Rotation Formula, see formulas 2 and 4.

An example implementation of this in Matlab is found here: link.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.