How do I plot both for loop graphs?

1 visualización (últimos 30 días)
Manuel
Manuel el 18 de Sept. de 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!

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 18 de Sept. de 2012
Editada: Azzi Abdelmalek el 18 de Sept. de 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')

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by