필터 지우기
필터 지우기

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