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

조회 수: 1 (최근 30일)
Hunter Nau
Hunter Nau 2022년 4월 16일
답변: Soujanya Shimoga Raveendra 2022년 4월 25일
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).

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

답변 (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 CenterFile Exchange에서 2-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