Modeling speed and height of a toy rocket using while loops
5 views (last 30 days)
Show older comments
Gabrielle Bartolome
on 28 Sep 2020
.
2 Comments
Stephen23
on 26 Oct 2020
Original question by Gabrielle Bartolome retrieved from Google Cache:
Modeling speed and height of a toy rocket using while loops
The flight of a model rocket of mass 0.05 kg can be modelled as follows. During the first 0.15 s the rocket is propelled up by the rocket engine with a force of 16 N. The rocket then flies up slowing down under the force of gravity. After it reaches the apex, the rocket starts to fall back down. When its down velocity reaches 20 m/s a parachute opens (assumed to open instantly) and the rocket continues to move down at a constant speed of 20 m/s until it hits the ground. Write a script that calculates and plots the speed and altitude of the rocket as a function of time during the flight.
Original Comment by Gabrielle Bartolome retrieved from Google Cache:
% This script calculates and plots the speed and altitude of the rocket as a function of time during the flight
%Declare known variables
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;
while t1<15
a = f/m - g;
v1 = a*t1;
h1 = .5*a*t1^2;
t1 = t1 +1;
end
%this while loop models the velocity and height after 15 seconds until the
%parachute opens
v2 = v1;
t1 = 15;
t2 = 16;
while v2 < -20
h2 = h1 + v1*(t2 - t1)-.5*9.81*((t2-t1)^2);
v2 = v1 - g*(t2-t1);
t2 = t2 + 1;
end
%this while loop models the velocity and height from when the parachut
%opens till the rocket hits the ground.
h3 = h2;
while h3>0
t3 = t2;
v3 = -20;
h3 = h2 + v2*(t3 - t2);
t3 = t3+1;
end
grid on
plot(t1, v1,'r', t2, v2, 'b', t3, v3, 'g')
hold on
Accepted Answer
Alan Stevens
on 28 Sep 2020
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!