Filter löschen
Filter löschen

Plotting

3 Ansichten (letzte 30 Tage)
Nishant Nain
Nishant Nain am 1 Sep. 2011
How to plot trajectory of an object when the angle of launch varies from 15-75 using 15 degree increments. I have the code to plot it. However, all the 4 plots are blue in color. How do I make it display different colored plots for different angles?

Antworten (2)

Walter Roberson
Walter Roberson am 1 Sep. 2011
plot(t,h15,'r');
hold on
plot(t,h30,'g');
plot(t,h45,'b');
plot(t,h60,'c');
plot(t,h75,'m');
Or
plot(t,h15,'r',t,h30,'g',t,h45,'b',t,h60,'c',t,h75,'m')
  2 Kommentare
Nishant Nain
Nishant Nain am 1 Sep. 2011
here's the code I have-
y0= 0; % m/s
v0=28; % m/s
g=9.8; % acceleration due to gravity in m/s^2
x=0:5:80; %distance from 0-100 in increments of 5
for th= 15:15:75;
y= (tand(th) .*x ) - (g.*x)/((2.*v0.^2).*(cosd(th)).^2) + y0;
plot (x,y);
legend (x,y);
xlabel('distance 0-80');
ylabel('vertical displacement y');
axis square;
hold on;
end
What should I change in this to make the plots look different.
Nishant Nain
Nishant Nain am 1 Sep. 2011
^^ gives the plot with different angles but I can't get it to show different colors/forms for seperate values of th

Melden Sie sich an, um zu kommentieren.


Oleg Komarov
Oleg Komarov am 1 Sep. 2011
Vectorized version, you can see here that the color is automatically selected for each column of y
y0 = 0;
v0 = 28;
g = 9.8;
x = 0:5:80;
th = 15:15:75;
y = bsxfun(@times,tand(th), x.') - bsxfun(@rdivide, g*x.',2*v0^2*cosd(th).^2) + y0;
plot (repmat(x.',1,5),y);
xlabel('distance 0-80');
ylabel('vertical displacement y');
axis square
Note that you legend accepts strings not doubles.
Otherwise use the syntax that Walter suggested:
y0 = 0; % m/s
v0 = 28; % m/s
g = 9.8; % acceleration due to gravity in m/s^2
x = 0:5:80; %distance from 0-100 in increments of 5
th = 15:15:75;
clr = {'r','g','b','c','m'}; % Colors
h = axes;
axis(h,'square');
hold on
for t = 1:numel(th)
y = tand(th(t))*x - g*x/(2*v0^2*cosd(th(t))^2) + y0;
plot(x,y,clr{t});
end
xlabel('distance 0-80');
ylabel('vertical displacement y');
Some suggestions, don't abuse the parenthesis and try to understand when you really need .* instead of *.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by