Info

此问题已关闭。 请重新打开它进行编辑或回答。

Error when calling fucntion

1 次查看(过去 30 天)
Max Viklund
Max Viklund 2015-2-22
关闭: MATLAB Answer Bot 2021-8-20
Hello,
I'm quite new to Matlab and I'm doing a university-course. My assignment is to create a function that for a value x rotates the base vectors of the room in positive direction around the z-axis.
This is what I came up with.
function f=rotate(x)
f=[cos (x), -sin (x), 0; sin (x), cos (x), 0; 0, 0, 1];
But when calling the function rotate for a certain value, let's say rotate(1), it gives me the following errors:
Error using cos
Not enough input arguments.
Error in rotate (line 2)
f=[cos (x), -sin (x), 0; sin (x), cos (x), 0; 0, 0, 1];
I have no idea what to do.
Thanks.

回答(2 个)

Sad Grad Student
Sad Grad Student 2015-2-22
编辑:Sad Grad Student 2015-2-22
rotate is already an inbuilt matlab function. Change your function name to rotate_val or something else (so it doesn't overwrite the inbuilt function if you need that function in future) and change your saved file name correspondingly. And more important: REMOVE the spaces after cos , sin .. It should be: cos(x) and not cos (x)
This works for me:
function f = rotate_val(x)
f = [cos(x), -sin(x), 0; sin(x), cos(x), 0; 0, 0, 1];
end
>> f = rotate_val(1)
f =
0.54030230586814 -0.841470984807897 0
0.841470984807897 0.54030230586814 0
0 0 1

Image Analyst
Image Analyst 2015-2-22
Your "x" is really the angle, not the locations. So you should probably call it theta or something. Next you need to pass in an array with the locations like xy which is a 2 by N array of x,y locations. Then you multiply your rotation matrix f by your locations array xy to get "rotated_xy", which you then pass out of the function
function rotated_xy = RotatePoints(xy, theta)
rotationMatrix = .... something involving sind() and cosd() and theta ....
rotated_xy = ... something involving rotationMatrix and xy ......
See if you can complete it.

Community Treasure Hunt

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

Start Hunting!

Translated by