필터 지우기
필터 지우기

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