Rotation Matrix Function Issues
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
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
0 个评论
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
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 Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!