MATLAB: Modeling speed and height of a toy rocket using while loops

MATLABpracticewhile loop

.

Best Answer

A little more like this perhaps:
g = 9.81 ;
m = 0.05 ;
f = 16 ;
%find velocity and height of the rocket in the first 15s
%this loop starts when time in seconds is 0 and repeats the commands until
%15 secods
t1 = 0; dt = 0.01;
t = 0; v = 0; h = 0; % to store values for plotting
i = 1; % counter
while t1<0.15
a = f/m - g;
v1 = a*t1;
h1 = .5*a*t1^2;
t1 = t1 +dt;
i = i+1;
t(i) = t1; v(i) = v1; h(i) = h1;
end
%this while loop models the velocity and height after 15 seconds until the
%parachute opens
v2 = v1;
t2 = t1;
while v2 > -20
h2 = h1 + v1*(t2-t1)-.5*g*((t2-t1)^2);
v2 = v1 - g*(t2-t1);
t2 = t2 + dt;
i = i+1;
t(i) = t2; v(i) = v2; h(i) = h2;
end
%this while loop models the velocity and height from when the parachut
%opens till the rocket hits the ground.
h3 = h2;
t3 = t2;
v3 = v2;
while h3>0
h3 = h2 + v3*(t3-t2);
t3 = t3+dt;
i = i+1;
t(i) = t3; v(i) = v3; h(i) = h3;
end
subplot(2,1,1)
plot(t, h),grid
xlabel('t'),ylabel('h')
subplot(2,1,2)
plot(t, v),grid
xlabel('t'),ylabel('v')
Note that the question specifies the powered flight for 0.15s not 15s.
Related Question