Plotting
6 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Walter Roberson
on 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')
Oleg Komarov
on 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 *.
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!