Rocket Modeling Matlab Script
Posted: Tue Nov 20, 2012 7:13 am
I thought this might be fun to share, since it has to do with a rocket. This script is from an assignment and models a rocket that is accelerated for 0.15 seconds and has a parachute that opens when the velocity reaches -20 m/s (which we assume keeps the rocket at a constant -20 m/s until it hits the ground). We are also assuming no air resistance (other than the air resistance given by the parachute ) and a constant mass for the rocket. The program asks to input the rocket's mass and the initial force applied on the rocket. The example the assignment gave was a rocket mass of 0.05 kg and an initial force of 16 Newtons, although you can use any numbers you like, so long as the initial force is sufficient enough to give an interesting performance (i.e., fly ). As always, this works in GNU Octave, and octave tested = good .
Code: Select all
clear,clc
% Inputs for Rocket mass and Initial Applied Force
m = input('Enter mass of Rocket in kilograms: ');
Fi = input('Enter intial Force in Newtons: ');
% Constants and intial Acceleration, AO
g = 9.8;
A0 = (Fi - m*g)/m;
% Time the Parachute opens
P = 20/g + 0.15*A0/g +0.15;
% Time the Rocket hits the Ground
G = (0.5*A0*(0.15)^2 + A0*0.15*(P-0.15) - 0.5*g*(P)^2 + 0.15*g*P - 0.01125*g)/20 + P;
% Time matrix
t = 0:0.001:G;
% Empty y and v for piecewise function
y = nan*ones(size(t));
v = nan*ones(size(t));
% y(t) the height of the Rocket versus Time, t.
y(0<t & t<=0.15) = 0.5*A0.*t(0<t & t<=0.15).^2;
y(0.15<t & t<=P) = 0.5*A0*(0.15)^2 + A0*0.15.*(t(0.15<t & t<=P)-0.15) - 0.5*g.*t(0.15<t & t<=P).^2 + 0.15*g.*t(0.15<t & t<=P) - 0.01125*g;
y(P<t & t<=G) = 0.5*A0*(0.15)^2 + A0*0.15*(P-0.15) - 0.5*g*(P)^2 + 0.15*g*P - 0.01125*g - 20.*(t(P<t & t<=G)-P);
% y'(t) = v(t)
v(0<t & t<=0.15) = A0.*t(0<t & t<=0.15);
v(0.15<t & t<=P) = A0*0.15 - g.*t(0.15<t & t<=P) + 0.15*g;
v(P<t & t<=G) = -20 + 0.*t(P<t & t<=G);
% Plotting the results
subplot(2,1,1),plot(t,y);
title('y (height) versus t')
xlabel('time')
ylabel('y (height)')
subplot(2,1,2),plot(t,v);
title('v versus t')
xlabel('time')
ylabel('velocity')
shg