Find angle of rotation matrix in MATLAB

30 views (last 30 days)
Kevin Maresca
Kevin Maresca on 22 Nov 2020
Hey everyone,
I'll preface by letting you know that I am a novice at MATLAB so I'm sure the code will need plenty of help.
I'd like to find the angle at which a matrix needs to be rotated about the y-axis to reach given coordinates. 'I' is the given matrix, I'd like to rotate it by 't' to get 'Iwant'. I'd also like to print out which value of 't' got the desired matrix.
As far as I can tell, the while loop runs forever. What am I doing wrong? Is this a completely wrong approach?
Thank you.
I = [11755 0 4821; 0 15000 0; 4821 0 23245]
Iwant = [10000 0 0; 0 15000 0; 0 0 25000]
t = 0;
while t < 3.14
R = [cos(t) 0 sin(t); 0 1 0; -sin(t) 0 cos(t)];
I1 = R*I*R';
if I1 == Iwant
fprintf(t)
break
t = t+0.2;
end
end

Answers (2)

Bruno Luong
Bruno Luong on 22 Nov 2020
Edited: Bruno Luong on 22 Nov 2020
Yes
  • the approach is wrong
  • your code has plenty of bugs*
  • you need to learn how to debug
  • you need to pratice MATLAB onramp
On (*) is fixed
I = [11755 0 4821; 0 15000 0; 4821 0 23245]
Iwant = [10000 0 0; 0 15000 0; 0 0 25000]
t = 0;
while t < 2*pi
R = [cos(t) 0 sin(t); 0 1 0; -sin(t) 0 cos(t)];
I1 = R*I*R';
if norm(I1-Iwant,'Inf') < 10
fprintf('t=%g\n', t)
break
end
t = t+1e-3;
end

Asad (Mehrzad)  Khoddam
Asad (Mehrzad) Khoddam on 22 Nov 2020
I = [11755 0 4821; 0 15000 0; 4821 0 23245];
Iwant = [10000 0 0; 0 15000 0; 0 0 25000];
t = 0;
i=1;
tList = 0:.02:3.14;
dList = NaN(size(tList));
for t = tList
R = [cos(t) 0 sin(t); 0 1 0; -sin(t) 0 cos(t)];
I1 = R*I*R';
d = I1-Iwant;
dList(i)= sum(d(:).^2);
i = i+1;
end
[~, index] = min(abs(dList));
disp(tList(index))

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!