← Deterministic Models

06

Forward Euler method from scratch

A differential equation gives a rate of change, not all future values directly. The forward Euler method uses the current rate to estimate the next value, then repeats the calculation to construct an approximate trajectory.

Biological scenario

Suppose 100 people are infectious and no new infections occur. Infectious people recover at rate \(\gamma=0.2\) per day.

We want to estimate the remaining infectious population over 10 days using only the differential equation and simple Python calculations.

The recovery differential equation

The recovery-only model is

\[\frac{dI}{dt}=-\gamma I.\]

The minus sign means that the infectious population decreases. At \(I=100\),

\[\frac{dI}{dt}=-0.2\times100=-20\quad\text{people per day}.\]

This is the instantaneous rate at the current state. As \(I\) decreases, the recovery rate also becomes smaller.

From a derivative to an approximate change

Over a short interval \(\Delta t\), Euler’s method approximates

\[\text{change}\approx\text{current rate}\times\Delta t.\]

\[I_{n+1}=I_n+\left(-\gamma I_n\right)\Delta t.\]

The subscript \(n\) identifies the current stored time point. The subscript \(n+1\) identifies the next one.

The four Euler operations

1 CURRENT VALUERead \(I_n\).
2 CURRENT RATECalculate \(-\gamma I_n\).
3 APPROXIMATE CHANGEMultiply the rate by \(\Delta t\).
4 NEXT VALUEAdd the change to \(I_n\).

Calculate the first two steps by hand

Use \(I_0=100\) and \(\Delta t=1\) day.

Current\(I_0=100\)
Rate\(-0.2\times100=-20\)
Change\(-20\times1=-20\)
Next\(I_1=100-20=80\)
Current\(I_1=80\)
Rate\(-0.2\times80=-16\)
Change\(-16\times1=-16\)
Next\(I_2=80-16=64\)

Translate one step into Python

rate = -gamma * I[n]
change = rate * dt
I[n + 1] = I[n] + change
Python expressionMeaning
I[n]The current infectious value
-gamma * I[n]The rate evaluated at the current value
rate * dtThe approximate change during one step
I[n + 1]The storage position for the next value

Repeat the step with a loop

for n in range(number_of_steps):
    rate = -gamma * I[n]
    I[n + 1] = I[n] + rate * dt
  • for begins a repetition.
  • range(number_of_steps) produces the indices \(0,1,2,\ldots\) needed for the updates.
  • The indented statements run once for each index.
  • The next value must be calculated from the current value, I[n].

Run the complete Euler program

The program also calculates the exact solution \(I(t)=I_0e^{-\gamma t}\) so that the numerical approximation can be checked.

Interactive PythonEuler recovery model

Output

Run the code to see the result.

Expected early values

DayEuler valueExact valueAbsolute error
0100.000100.0000.000
180.00081.8731.873
264.00067.0323.032
351.20054.8813.681

Biological and numerical interpretation

The infectious population declines because the model contains recovery but no new infection. Each day, Euler’s method removes 20% of the current infectious value. The amount removed becomes smaller as the infectious population declines.

The Euler values differ from the exact curve because Euler assumes the current rate remains fixed across each whole step. A smaller time step updates the rate more frequently and normally improves the approximation.

Important conditions and common mistakes

  • \(\Delta t>0\) is required.
  • The time grid and storage array must have compatible lengths.
  • The loop stops at the final update; otherwise I[n + 1] would exceed the array.
  • Use the current value on the right-hand side before storing the next value.
  • For this decay equation, \(0\leq\gamma\Delta t\leq1\) prevents a single Euler step from producing a negative population.
  • A time step can satisfy non-negativity and still be too large for good accuracy.

Try a smaller time step

Change dt = 1 to dt = 0.25.

  1. Predict what happens to the number of Euler steps.
  2. Run the program.
  3. Compare the maximum error with the original result.
  4. Inspect how closely the Euler graph follows the exact curve.