How do I plot both for loop graphs?

1 Ansicht (letzte 30 Tage)
Manuel
Manuel am 18 Sep. 2012
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!

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 18 Sep. 2012
Bearbeitet: Azzi Abdelmalek am 18 Sep. 2012
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')

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by