Plotting

3 次查看(过去 30 天)
Nishant Nain
Nishant Nain 2011-9-1
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?

回答(2 个)

Walter Roberson
Walter Roberson 2011-9-1
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 个评论
Nishant Nain
Nishant Nain 2011-9-1
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 2011-9-1
^^ gives the plot with different angles but I can't get it to show different colors/forms for seperate values of th

请先登录,再进行评论。


Oleg Komarov
Oleg Komarov 2011-9-1
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 *.

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by