Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook Rocket DiffEq.ipynb

41 views
Kernel: Python 3 (Ubuntu Linux)

Here we'll solve for the motion of a rocket.

The shell of the rocket has a mass mrm_r and it contains fluid with mass mfm_f.

The mass of the fluid in the rocket as a function of time follows the functional form:

mf=m0,f−m˙0,ft+k12t2m_f = m_{0,f} - \dot{m}_{0,f} t + \frac{k_1}{2} t^2

The mass flow rate of fuel from the rocket due to combustion, m˙f\dot{m}_f, is

m˙f=−dmfdt=m˙0,f−k1t\dot{m}_f = -\frac{d m_f}{dt} = \dot{m}_{0,f} - k_1 t

The velocity of the rocket exhaust, vev_e, follows the form

ve=v0−k2tv_e = v_0 - k_2 t

We want to find the velocity and altitude of the rocket in the instant when the fuel is exhausted given the following constants:

ConstantValue
m0,fm_{0,f}5 kg5~kg
mrm_r1 kg1~kg
m˙0,f\dot{m}_{0,f}10 kgs10~\frac{kg}{s}
k1k_15.7 kgs25.7~\frac{kg}{s^2}
v0v_040 ms40~\frac{m}{s}
k2k_24.3 ms24.3~\frac{m}{s^2}

From a momentum balance, we know that

∑F=mf˙ve−mg=ma=mdvdt=md2xdt2\sum F = \dot{m_f}v_e - m g = m a = m \frac{d v}{d t} = m \frac{d^2 x}{d t^2}

where aa, vv, and xx are the instantaneous acceleration, velocity, and position of the rocket and mm is the total mass of the rocket (mr+mfm_r + m_f).

Note that mfË™\dot{m_f}, vev_e, and mm are functions of time; to solve this numerically, we will use standard ordinary differential equation methods. Most commonly, these methods take some set of equations dfdt\frac{d\mathbf{f}}{dt} and initial values f0\mathbf{f}_0 and integrate them forward over time.

First, we input our constants and equations governing the system:

m_0f = 5 m_r = 1 mdot_0f = 10 k1 = 5.7 v0 = 40 k2 = 4.3 g = 9.81 mdot_f = lambda t: mdot_0f - k1*t v_e = lambda t: v0 - k2*t m = lambda t: m_r + mdot_f(t) sumOfForces = lambda t: mdot_f(t)*v_e(t) - m(t)*g

To put these into a form usable by ODE solvers, we will let our vector of values

f=[dxdt,x]\mathbf{f}=\left[\frac{dx}{dt}, x\right]

then our derivative vector

dfdt=[d2xdt2,dxdt]\frac{df}{dt}=\left[\frac{d^2 x}{dt^2}, \frac{dx}{dt}\right]

To pass these into an equation for the integration method we use, we supply dfdt(f,t)\frac{df}{dt}(f, t). Note that in matlab, the ode45 function takes dfdt(t,f)\frac{df}{dt}(t, f).

dfdt = lambda f, t: [sumOfForces(t)/m(t), # dv/dt = sumOfForces/m f[0]] # dx/dt = v = f[0] f0 = [0, 0] # initial velocity is 0 and initial position is 0

We should also find the time it takes for the tank to be emptied by solving the second-order polynomial for when mf=0m_f=0

from math import sqrt tmax = (mdot_0f - sqrt(mdot_0f**2 - 4*k1/2*m_0f))/2/k1*2 print('tmax = {}'.format(tmax))
tmax = 0.603958153631228

Now we can integrate this using odeint to get f(t)\mathbf{f}(t) as follows:

import scipy as sp, scipy.integrate t = sp.linspace(0, tmax, 1024) # 1024 time values linearly spaced from t=0 to t=20 s f = sp.integrate.odeint(dfdt, f0, t) print('Final v: {} m/s'.format(f[-1, 0])) print('Final x: {} m'.format(f[-1, 1]))
Final v: 14.905972849135207 m/s Final x: 4.619581521862367 m

Finally, we can plot the time evolution of vv and xx as follows:

%matplotlib inline import matplotlib, matplotlib.pyplot as pp pp.plot(t, f[:, 1]) pp.xlabel('$t$'); pp.ylabel('$x(t)$')
<matplotlib.text.Text at 0x7f9796646438>
Image in a Jupyter notebook
%matplotlib inline import matplotlib, matplotlib.pyplot as pp pp.plot(t, f[:, 0]) pp.xlabel('$t$'); pp.ylabel('$v(t)$')
<matplotlib.text.Text at 0x7f97965e53c8>
Image in a Jupyter notebook