How to have a function return a coordinate point?

조회 수: 27 (최근 30일)
Connor
Connor 2023년 2월 12일
편집: Torsten 2023년 2월 12일
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
Voss
Voss 2023년 2월 12일
function [x,y] = unitCircle(theta)
x = cos(theta);
y = sin(theta);
end

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2023년 2월 12일
[x,y] = unitCircle(pi/4)
x = 0.7071
y = 0.7071
function [x y] = unitCircle(theta)
z = [cos(theta),sin(theta)];
x = z(1);
y = z(2);
end
  댓글 수: 4
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 12일
This function file syntax has to be:
theta = -pi:.1:pi;
[x,y]=unitCircle(theta)
x = 1×63
-1.0000 -0.9950 -0.9801 -0.9553 -0.9211 -0.8776 -0.8253 -0.7648 -0.6967 -0.6216 -0.5403 -0.4536 -0.3624 -0.2675 -0.1700 -0.0707 0.0292 0.1288 0.2272 0.3233 0.4161 0.5048 0.5885 0.6663 0.7374 0.8011 0.8569 0.9041 0.9422 0.9710
y = 1×63
-0.0000 -0.0998 -0.1987 -0.2955 -0.3894 -0.4794 -0.5646 -0.6442 -0.7174 -0.7833 -0.8415 -0.8912 -0.9320 -0.9636 -0.9854 -0.9975 -0.9996 -0.9917 -0.9738 -0.9463 -0.9093 -0.8632 -0.8085 -0.7457 -0.6755 -0.5985 -0.5155 -0.4274 -0.3350 -0.2392
function [x y] = unitCircle(theta)
z = [cos(theta);sin(theta)];
x = z(1,:);
y = z(2,:);
end
Torsten
Torsten 2023년 2월 12일
편집: Torsten 2023년 2월 12일
Doesn't work :-)
theta = (-pi:.1:pi).';
[x,y]=unitCircle(theta)
x = -1
y = -0.9950
function [x y] = unitCircle(theta)
z = [cos(theta);sin(theta)];
x = z(1,:);
y = z(2,:);
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 12일
Here is the corrected code:
% Ver 1
xy = unitCircle(pi/4)
xy = 2×1
0.7071 0.7071
function xy = unitCircle(theta)
xy=[cos(theta); sin(theta)];
end
  댓글 수: 1
Sulaymon Eshkabilov
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by