Rotation Matrix Function Issues
4 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Matt J
on 12 Mar 2021
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 Comments
Russel Burgess
on 12 Mar 2021
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 Comment
Paul
on 12 Mar 2021
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().
See Also
Categories
Find more on Matrix Indexing 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!