How to have a function return a coordinate point?
조회 수: 27 (최근 30일)
이전 댓글 표시
I am trying to get a function to return (x,y) coordinates for a given angle theta. I am struggling to figure out how to get the output of the function to be coordinate points and not two seperate variables x and y.
This is what I have for the function:
function [x,y] = unitCircle(theta)
[x,y] = [cos(theta),sin(theta)]
end
And when I call the function using:
[x,y] = unitCircle(pi/4)
I get an error:
Too many output arguments.
Error in unitCircle (line 2)
[x,y] = [cos(theta),sin(theta)]
댓글 수: 1
채택된 답변
Torsten
2023년 2월 12일
[x,y] = unitCircle(pi/4)
function [x y] = unitCircle(theta)
z = [cos(theta),sin(theta)];
x = z(1);
y = z(2);
end
댓글 수: 4
Sulaymon Eshkabilov
2023년 2월 12일
This function file syntax has to be:
theta = -pi:.1:pi;
[x,y]=unitCircle(theta)
function [x y] = unitCircle(theta)
z = [cos(theta);sin(theta)];
x = z(1,:);
y = z(2,:);
end
추가 답변 (1개)
Sulaymon Eshkabilov
2023년 2월 12일
Here is the corrected code:
% Ver 1
xy = unitCircle(pi/4)
function xy = unitCircle(theta)
xy=[cos(theta); sin(theta)];
end
댓글 수: 1
Sulaymon Eshkabilov
2023년 2월 12일
Alt. version:
% Ver 2
theta = linspace(-2*pi, 2*pi);
xy=unitCircle(theta);
plot(xy(1,:), xy(2,:)), axis equal;
xlabel("x"), ylabel("y"), grid on
function xy = unitCircle(theta)
xy(1,:)=[cos(theta)];
xy(2,:)=[sin(theta)];
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
