0
$\begingroup$

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));
end

I am using this algorithm Rotation Matrix

  • 0
    There are is a minor mistake: In R(1,3), the y should be a z. Otherwise it looks fine.2012-10-24

2 Answers 2

0

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.

1

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.