MATLAB Answers

Error when calling fucntion

1 view (last 30 days)
Max Viklund
Max Viklund on 22 Feb 2015
Answered: Image Analyst on 22 Feb 2015
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.

Answers (2)

Sad Grad Student
Sad Grad Student on 22 Feb 2015
Edited: Sad Grad Student on 22 Feb 2015
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 on 22 Feb 2015
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!