How do I plot both for loop graphs?

조회 수: 1 (최근 30일)
Manuel
Manuel 2012년 9월 18일
At the moment I am trying to create two graphs one containing the values (t,v) and the other (t,h), and both need to be displayed at the same axis. I sincerely do not know how to create the graph as using plot does not work. here is my program so far:
% Position and velocity of a ball % Objective: To find the relationship between velocity and position of ball as a function of time when being released at an initial height. % Variable Notation: % g= gravity % t= time % V0= initial velocity of the ball % h0= the hight of the tallest building in Puerto Rico in meters % V= velocity as a function of time % h= height as a function of time
clc;clear
v0=0; g=-9.81; h0=49;
t=0;
t1=0;
for x= 1:100
v=g*t+v0;
h=((1/2)*g*(t1^2))+v0*t1+h0; plot(t,v);
fprintf('%f %f %f \n',t,h);
t1=t1+1;
t=t+1;
if h<=0
break
end
end
I really need help with this!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 18일
편집: Azzi Abdelmalek 2012년 9월 18일
clc;clear
v0=0; g=-9.81;
x=1;
h0=49;
t(1)=0;
v(1)= g*t(1)+v0
h(1)=(1/2)*g*t(1)^2+v0*t(1)+h0
while h(x)>0 & x<100
x=x+1
t(x)=x-1
v(x)= g*t(x)+v0
h(x)=(1/2)*g*t(x)^2+v0*t(x)+h0
end
plot(t,h)
hold on;
plot(t,v,'r')
%or
clc;clear
v0=0;h0=49;g=-9.81;
t=0:99; % time vector
v= g.*t+v0
h=(1/2)*g*t.^2+v0*t+h0
idx=find(h<0,1)
t=t(1:idx);v=v(1:idx);h=h(1:idx);
plot(t,h)
hold on;
plot(t,v(1:idx),'r')
  댓글 수: 1
Manuel
Manuel 2012년 9월 18일
Thank you very, very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by