Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

11 - ODEs

Many times in engineering, the system in question is not at steady state. Or in other words, the system variables are changing. We’ll look at some basic methods to readily solve problems where the system is changing with time.

You should understand at the end of this discussion that if you have an expression for the derivative, you can integrate that numerically to determine the system parameters as a function of time.

One of the most common ways to solve ODEs is to use the Euler method. The Euler method is helpful because it is simple to implement and understand. However, it is not the most accurate method. There are other algorithms that are much better.

Euler’s method is a numerical solution that uses a finite difference approximation to the derivative:

dfdt=lim⁡Δt→0f(t+Δt)−f(t)Δt\frac{df}{dt} = \lim_{\Delta t \rightarrow 0} \frac{f(t+\Delta t) - f(t)}{\Delta t}

Using Euler’s method:

f(t+Δt)=f(t)+dfdtΔt\begin{align} f(t+\Delta t) &= f(t) + \frac{df}{dt} \Delta t \\ \end{align}

So an approximation of the function at the next time step (from the starting point) is the function at the current time step plus the derivative times the time step. This approximation gets more accurate with smaller time steps.

First Example: Frictionless emptying a tank of fliud

A tank of water is draining through a 1/8 inch diameter hole in the bottom of the tank. The water level in the tank is 7 and 3/16 inches above the hole. The tank is 1 and 9/16 inch in diameter. The density of water is 1000 kg/m3. The tank is open to the atmosphere. The flow rate out of the tank is estimated by the following equation:

m˙=ρA2gh\dot{m} = \rho A \sqrt{2 g h}

where m˙\dot{m} is the mass flow rate, ρ\rho is the density, AA is the area of the hole, gg is the gravitational constant, and hh is the height of the water above the hole. This is for a frictionless hole.

Analytical Solution

Start with a mass balance

dmdt=m˙in−m˙outdmdt=0−m˙outdmdt=−ρA2ghm=ρVV=π4D2hm=ρπ4D2hdmdt=ρπ4D2dhdt=−ρA2ghdhdt=−4πD2A2gh\begin{align} \frac{dm}{dt} &= \dot{m}_{in} - \dot{m}_{out} \\ \frac{dm}{dt} &= 0 - \dot{m}_{out} \\ \frac{dm}{dt} &= -\rho A \sqrt{2 g h} \\ m &= \rho V \\ V &= \frac{\pi}{4} D^2 h \\ m &= \rho \frac{\pi}{4} D^2 h \\ \frac{dm}{dt} &= \rho \frac{\pi}{4} D^2 \frac{dh}{dt} = -\rho A \sqrt{2 g h} \\ \frac{dh}{dt} &= -\frac{4}{\pi D^2} A \sqrt{2 g h} \\ \end{align}
dhdt=−4πD2A2ghdhh=−4πD2A2gdt∫dhh=−4πD2A2g∫dt2h=−4πD2A2gt+Ch=(−2πD2A2gt+C)2\begin{align} \frac{dh}{dt} &= -\frac{4}{\pi D^2} A \sqrt{2 g h} \\ \frac{dh}{\sqrt{h}} &= -\frac{4}{\pi D^2} A \sqrt{2 g} dt \\ \int \frac{dh}{\sqrt{h}} &= -\frac{4}{\pi D^2} A \sqrt{2 g} \int dt \\ 2 \sqrt{h} &= -\frac{4}{\pi D^2} A \sqrt{2 g} t + C \\ h &= \left(-\frac{2}{\pi D^2} A \sqrt{2 g} t + C\right)^2 \end{align}

With the initial condition that h(0)=h0h(0) = h0 m, we can solve for CC:

h(0)=(−2πD2A2g(0)+C)2h0=C2C=±h0\begin{align} h(0) &= \left(-\frac{2}{\pi D^2} A \sqrt{2 g} (0) + C\right)^2 \\ h0 &= C^2 \\ C &= \pm \sqrt{h0} \end{align}

Thus the solution is

h(t)=(−2πD2A2gt+h0)2\begin{align} h(t) &= \left(-\frac{2}{\pi D^2} A \sqrt{2 g} t + \sqrt{h0}\right)^2 \\ \end{align}
<Figure size 640x480 with 1 Axes>

Numerical Solution: Euler’s Method

Now we’ll use the above Euler formulation to solve for the fluid height as a function of time. We’ll also compare that solution to the analytical solution.

From the above the problem definition, we can rewrite the differential equation as:

dhdt=f(h)=−4πD2A2gh\begin{align} \frac{dh}{dt} = f(h) = -\frac{4}{\pi D^2} A \sqrt{2 g h} \\ \end{align}

Using Euler’s method:

f(t+Δt)=f(t)+dfdtΔth(t+Δt)=h(t)+dhdtΔth(t+Δt)=h(t)+f(h)Δth(t+Δt)=h(t)+−4πD2A2gh(t)Δt\begin{align} f(t+\Delta t) &= f(t) + \frac{df}{dt} \Delta t \\ h(t+\Delta t) &= h(t) + \frac{dh}{dt} \Delta t \\ h(t+\Delta t) &= h(t) + f(h) \Delta t \\ h(t+\Delta t) &= h(t) + -\frac{4}{\pi D^2} A \sqrt{2 g h(t)} \Delta t \\ \end{align}
<Figure size 640x480 with 1 Axes>

Numerical Solution: ODE Int (SciPy)

Euler’s method isn’t the best algorithm to use. There are better algorithms like 4th order Runge-Kutta. Scipy’s ODEINT is used routinely for systems of differential equations. We’ll use the odeint function from the scipy.integrate module to solve the same above problem with one differential equation.

<Figure size 640x480 with 1 Axes>

Second Example: Draining of Tank with Friction

Now we’ll look at the same problem, but with friction. The flow rate out of the tank is estimated by the following equation:

m˙=ρAv=ρA2ghCdCd=CcCv\begin{align} \dot{m} &= \rho A v = \rho A \sqrt{2 g h} C_d \\ C_d &= C_c C_v \\ \end{align}

where m˙\dot{m} is the mass flow rate, ρ\rho is the density, AA is the area of the hole, gg is the gravitational constant, hh is the height of the water above the hole, and CdC_d is the discharge coefficient. CcC_c is the coefficient of contraction and CvC_v is the coefficient of velocity. The discharge coefficient is a dimensionless number that accounts for the frictional losses in the system. The discharge coefficient can vary depending on the geometry of the hole and the fluid viscosity.

A group from the UK and Nigeria measured nozzle discharge coefficients as published here. There, discharge coefficients are experimentally determined and compared to empirical equations like:

Cd=Re5/617.11⋅l/d+1.65⋅Re0.8Re=ρDvμ\begin{align} C_d &= \frac{Re^{5/6}}{17.11\cdot l/d + 1.65\cdot Re^{0.8}} \\ Re &= \frac{\rho D v}{\mu} \\ \end{align}

where ReRe is the Reynolds number and l/dl/d is the length to diameter ratio of the nozzle. Solving for an analytical solution is not that easy but using the ODEINT function from the scipy.integrate module, we can solve the problem numerically.

<Figure size 640x480 with 1 Axes>

This is a complex problem as the velocity of the fluid depends on the discharge coefficient and the discharge coefficient depends on the fluid velocity. For each call to the derivative function, an fsolve is required to determine the exit velocity.

The equation to solve to determine the reynolds number given the height of fluid in the tank is:

v=m˙ρA=2ghCd(Re)Re=ρDvμ=ρD2ghCd(Re)μ\begin{align} v = \frac{\dot{m}}{\rho A} = \sqrt{2 g h} C_d(Re) \\ Re = \frac{\rho D v}{\mu} = \frac{\rho D \sqrt{2 g h} C_d(Re)}{\mu} \\ \end{align}

Complete an example calculation with fsolve to find the Reynolds number given a height of fluid in the tank.

38157.06745192405 38157.06745192405

Now we can solve for the height of the fluid in the tank as a function of time.

<Figure size 640x480 with 1 Axes>

Video of the above scenario

A video of the above scenario where water drains from graduated cylinder through a small hole. Height of the water in the cylinder as a function of time is approximated from the video.

Loading...
<Figure size 640x480 with 1 Axes>

The agreement between the numerical solution including friction and the experimental values (roughly estimated from the video) is pretty good. What does that mean? What else can you conclude? How else could you use this information?