Rotation Matrix Function Issues

조회 수: 1 (최근 30일)
William Reichling
William Reichling 2021년 3월 12일
답변: Matt J 2021년 3월 12일
I have been trying to create a function that outputs a rotation matrix when you input a certain axis and angle. My issue is that I cannot get the axis input to link to the function. The axis input continues to be highlighted on the first line and says "input argument 'axis' might be unused". When I set values for the inputs and run my function in the command window, it only displays the last matrix no matter what value I put for the axis. Here is the code I have.
function R = AxisAngle_to_Rot(axis, angle)
for axis = [1; 0; 0]
R = [1 0 0; 0 cos(angle) -sin(angle); 0 sin(angle) cos(angle)];
end
for axis = [0; 1; 0]
R = [cos(angle) 0 sin(angle); 0 1 0; -sin(angle) 0 cos(angle)];
end
for axis = [0; 0; 1]
R = [cos(angle) -sin(angle) 0; sin(angle) cos(angle) 0; 0 0 1];
end
end
I have been struggling with linking inputs a lot recently, so any guidance would be appreciated.

답변 (2개)

Matt J
Matt J 2021년 3월 12일
Change for to if:
function R = AxisAngle_to_Rot(axis, angle)
if isequal(axis, [1; 0; 0])
R = [1 0 0; 0 cos(angle) -sin(angle); 0 sin(angle) cos(angle)];
end
if isequal(axis, [0; 1; 0])
R = [cos(angle) 0 sin(angle); 0 1 0; -sin(angle) 0 cos(angle)];
end
if isequal(axis,[0; 0; 1])
R = [cos(angle) -sin(angle) 0; sin(angle) cos(angle) 0; 0 0 1];
end
end

Russel Burgess
Russel Burgess 2021년 3월 12일
It seems you might be misunderstanding what "for axis = [1; 0; 0]" does. A for loop like:
for axis = [1; 0; 0]
disp(axis);
end
Will exceute the body of the loop with axis = 1, axis = 0, and axis = 0. You can run the code above to see that happening.
Therefore, in your code the input axis is being ignored, as all three for loops are being excuted (assigning new values for axis each time), leaving only the last execution of the last loop to set R.
Something closer to what you're aiming for might be (without any checking for input formatting):
function R = AxisAngle_to_Rot(axis, angle)
switch find(axis, 1)
case 1
R = [1 0 0; 0 cos(angle) -sin(angle); 0 sin(angle) cos(angle)];
case 2
R = [cos(angle) 0 sin(angle); 0 1 0; -sin(angle) 0 cos(angle)];
case 3
R = [cos(angle) -sin(angle) 0; sin(angle) cos(angle) 0; 0 0 1];
otherwise
error('Invalid axis input');
end
end
find() gets the index of the axis being specified, and the switch statement picks which expression for R to use based on this.
I'd recommend reading the documentation for for, switch, and other basic elements of matlab to get a good grasp on how they work first.
  댓글 수: 1
Paul
Paul 2021년 3월 12일
Actually, these lines:
for axis = [1;0;0]
disp(axis)
end
only runs the loop one time, with value of axis being the column vector [1 ; -0 ; 0], as opposed to
for axis = [1 0 0]
disp(axis)
end
which will execute the loop three times as you describe.
Also, probably shouldn't use a variable named axis so as not conflict with the Matlab function axis().

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by