How do I graph more than one line in the same plot using a function?
4 views (last 30 days)
Show older comments
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 Comments
Matt J
on 16 Apr 2022
Please paste in your code as text, not as an image (so we can copy/paste it more easily).
Answers (1)
Soujanya Shimoga Raveendra
on 25 Apr 2022
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 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!