How do I graph more than one line in the same plot using a function?

3 ビュー (過去 30 日間)
Hunter Nau
Hunter Nau 2022 年 4 月 16 日
I am graphong a function based on the coordinates from theta. I am having difficulty plotting 4 different lines on the same plot. I keep having them all turn into 1 line.
^what the graph should look like
^my graph and code
%Test Inputs for theta in degrees
% P1=angle(10)
% P2=angle(30)
% P3=angle(60)
% P4=angle(95)
% for th=1:180
th=[10 30 60 95];
i=1:length(th);
x=cos(th(i));
y=sin(th(i));
plot(x,y)
figure(1)
% plot([P1-x P2-x P3-x P4-x],[P1-y P2-y P3-y P4-y])
xlabel('X Position (m)');
ylabel('Y Position (m)');
xlim([-0.2 1])
ylim([0 1])
% end
% plot([0 x],[0 y],'-o r','linewidth',2)
% function th=angle(th)
% theta=th
% end
  2 件のコメント
Matt J
Matt J 2022 年 4 月 16 日
Please paste in your code as text, not as an image (so we can copy/paste it more easily).
Hunter Nau
Hunter Nau 2022 年 4 月 17 日
Posted

サインインしてコメントする。

回答 (1 件)

Soujanya Shimoga Raveendra
Soujanya Shimoga Raveendra 2022 年 4 月 25 日
Hello,
I understand that you are trying to plot individual lines in the same plot. For that you need to take care of the following points in your code:
1. Converting theta from degree to radians using 'deg2rad' before passing to 'cos' and 'sin' functions.
2. Using 'hold on' to retain the previous plot.
Please check the following code for the same:
%Test Inputs for theta in degrees
th_d=[10 30 60 95];
%--------------Convert the angle in degree to radian
th = deg2rad(th_d);
i=1:length(th);
x=cos(th(i));
y=sin(th(i));
%--------------Use hold on to retain the previous plot
hold on;
grid on;
figure(1)
for i=1:length(th)
%-------------plot individual point and connect with the origin
plot([x(i) 0],[y(i) 0], '-o', 'LineWidth',2)
end
hold off;
%-------------- To add legends
legend('10 deg','30 deg','60 deg','95 deg')
xlabel('X Position (m)');
ylabel('Y Position (m)');
xlim([-0.2 1])
ylim([0 1])
Hope this helps.

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by