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

3 vues (au cours des 30 derniers jours)
Hunter Nau
Hunter Nau le 16 Avr 2022
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];
% 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 commentaires
Matt J
Matt J le 16 Avr 2022
Please paste in your code as text, not as an image (so we can copy/paste it more easily).

Connectez-vous pour commenter.

Réponses (1)

Soujanya Shimoga Raveendra
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);
%--------------Use hold on to retain the previous plot
hold on;
grid on;
for i=1:length(th)
%-------------plot individual point and connect with the origin
plot([x(i) 0],[y(i) 0], '-o', 'LineWidth',2)
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.


En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by