How do I graph more than one line in the same plot using a function?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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.
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.
For more details on plot please refer to : https://www.mathworks.com/help/matlab/ref/plot.html?searchHighlight=plot&s_tid=srchtitle_plot_1
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!