Help with a rotation matrix
Show older comments
I have a code which requires 4 different rotation matrices, and since all my variables are in the form of arrays I'm trying to make these matrices also in the form of arrays. So far, the code I have below is what I believe should work. The parameters at the top are parameters for each of the 4 orbits, laid out in 1x4 array. R1, R2 and R3 are not giving me the answers I expect in all the cells (R1,2 and 3 will be combined to make R the final matrix). The matrices should have more zeros in them but I am getting a very very small number (10^-17) instead, I don't know where the maths is going wrong? Thanks! (.png attached at top).
clear
close
i = [90 90 90 90]; %[deg] Inclinations for each orbit
o = [0 0 0 0]; %[deg] Small omega of each orbit
O = [0 45 90 135]; %[deg] RAANs for each orbit
R1 = [cos(o).*cos(O)-sin(o).*cos(i).*sin(O); % Column 1 of rotation matrix for the 4 orbits
cos(o).*sin(O)+sin(o).*cos(i).*cos(O);
sin(o).*sin(i)];
R2 = [-sin(o).*cos(O)-cos(o).*cos(i).*sin(O); % Column 2 of rotation matrix for the 4 orbits
-sin(o).*sin(O)+cos(o).*cos(i).*cos(O);
cos(o).*sin(i)];
R3 = [sin(i).*sin(O); % Column 3 of rotation matrix for the 4 orbits
-sin(i).*cos(O);
cos(i)];
15 Comments
Image Analyst
on 26 Oct 2018
I'm not quite sure why your rotation matrices (R1, R2, and R4) have so many terms.
Plus I don't know how the i, o, and O are related to points in the plane that might be rotated through some angle. Why are they not Nx2 arrays of (x,y) coordinates?
Scott Hunter
on 26 Oct 2018
Edited: Scott Hunter
on 26 Oct 2018
Bruno Luong
on 26 Oct 2018
Edited: Bruno Luong
on 26 Oct 2018
you seem to apply sin/cos to degree angles which is wrong, since argument must be in radian.
Scott Hunter
on 26 Oct 2018
Scott Hunter
on 26 Oct 2018
Bruno Luong
on 26 Oct 2018
"I then read somewhere MATLAB uses degrees for trig. functions, is that incorrect?"
Yes incorrect.
"Could it possibly be to do with the fact I have element-wise multiplication?"
No, element wise is correct.
But take a step back : Why you think R2 and R3 are wrong?
Scott Hunter
on 26 Oct 2018
Edited: Scott Hunter
on 26 Oct 2018
Bruno Luong
on 26 Oct 2018
You should expect such small error when working with finite precision numbers
>> cos(pi/2)
ans =
6.1232e-17
>>
Scott Hunter
on 26 Oct 2018
James Tursa
on 26 Oct 2018
@IA: "... I'm not quite sure why your rotation matrices (R1, R2, and R4) have so many terms ..."
Because the formulas he is using are the result of three 3D rotation matrices multiplied together, the result of which is still a rotation matrix.
James Tursa
on 26 Oct 2018
Edited: James Tursa
on 26 Oct 2018
@Scott: For your downstream code, I would strongly advise arranging all of your columns into a 3D matrix that is size 3x3xN, where N is the number of rotation matrices that you are working with (in this case 4) and the 3x3 pages are the individual rotation matrices. That way the first two dimensions form pages of your rotation matrices, which makes it compatible with a lot of code (e.g. in the FEX) that has been written to deal with pages that are stored this way.
Scott Hunter
on 26 Oct 2018
Edited: Scott Hunter
on 26 Oct 2018
James Tursa
on 26 Oct 2018
R1 contains four 1st columns, R2 contains four 2nd columns, and R3 contains four 3rd columns. I am suggesting that the first page of your 3D R array would be:
[R1(:,1) R2(:,1) R3(:,1)]
the second page of R would be
[R1(:,2) R2(:,2) R3(:,2)]
etc.
Rather that constructing R manually this way, one could pre-allocate R and then just fill it in appropriately. E.g.,
N = numel(i); % the number of rotation matrices
R = zeros(3,3,N); % pre-allocate the array of rotation matrices
R(:,1,:) = R1; % fill in the 1st columns of R
R(:,2,:) = R2; % fill in the 2nd columns of R
R(:,3,:) = R3; % fill in the 3rd columns of R
Then R(:,:,1) will be the first rotation matrix, R(:,:,2) will be the second rotation matrix, etc. In general, R(:,:,k) will be the k'th rotation matrix. This arrangement will pay dividends in your downstream code.
Scott Hunter
on 26 Oct 2018
James Tursa
on 26 Oct 2018
Edited: James Tursa
on 26 Oct 2018
Many FEX submissions assume this layout so you could use them with your R array. E.g.,
And several MATLAB functions also assume this layout. E.g.,
Accepted Answer
More Answers (1)
sanjay
on 10 Jul 2025
0 votes
cos(pi/2)
ans =
6.1232e-17
2 Comments
Or use degrees:
cosd(90)
sind(180)
Categories
Find more on Lengths and Angles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!