How to have a function return a coordinate point?

31 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by