Euler Method with Theorems Applied to Non-Linear Population Equations#
The more general form of a first order Ordinary Differential Equation is:
This can be solved analytically by integrating both sides but this is not straight forward for most problems. Numerical methods can be used to approximate the solution at discrete points. In this notebook we will work through the Euler method for two initial value problems:
A non-linear sigmoidal population equation
A non-linear sigmoidal population differential equation with a wiggle,
Euler method#
The simplest one step numerical method is the Euler Method named after the most prolific of mathematicians Leonhard Euler (15 April 1707 – 18 September 1783) .
The general Euler formula for to the first order differential equation
approximates the derivative at time point
where
Assuming uniform stepsize
This can be read as the future
## Library
import numpy as np
import math
%matplotlib inline
import matplotlib.pyplot as plt # side-stepping mpl backend
import matplotlib.gridspec as gridspec # subplots
import warnings
import pandas as pd
warnings.filterwarnings("ignore")
Non-linear population equation#
The general form of the non-linear sigmoidal population growth differential equation is:
where
Specific non-linear population equation#
Given the growth rate $
The initial population at time
we are interested in the time period
Initial Condition#
To get a specify solution to a first order initial value problem, an initial condition is required. For our population problem the initial population is 6 billion people:
General Discrete Interval#
The continuous time
Specific Discrete Interval#
Here the interval is
this gives the 201 discrete points with stepsize h=0.1:
which is generalised to
The plot below illustrates the discrete time steps from 2000 to 2002.
### Setting up time
t_end=2020.0
t_start=2000.0
N=200
h=(t_end-t_start)/(N)
time=np.arange(t_start,t_end+0.01,h)
fig = plt.figure(figsize=(10,4))
plt.plot(time,0*time,'o:',color='red')
plt.title('Illustration of discrete time points for h=%s'%(h))
plt.xlim((2000,2002))
plt.plot();

Numerical approximation of Population growth#
The differential equation is transformed using the Euler method into a difference equation of the form
This approximates a series of of values
where
w=np.zeros(N+1)
w[0]=6
for i in range (0,N):
w[i+1]=w[i]+h*(0.2*w[i]-0.01*w[i]*w[i])
The plot below shows the Euler approximation
fig = plt.figure(figsize=(10,4))
plt.plot(time,w,'s:',color='blue',label='Euler')
plt.xlim((min(time),max(time)))
plt.xlabel('time')
plt.legend(loc='best')
plt.title('Euler solution')
plt.plot();

Table#
The table below shows the iteration
d = {'time t[i]': time[0:10], 'Euler (w_i) ':w[0:10]}
df = pd.DataFrame(data=d)
df
time t[i] | Euler (w_i) | |
---|---|---|
0 | 2000.0 | 6.000000 |
1 | 2000.1 | 6.084000 |
2 | 2000.2 | 6.168665 |
3 | 2000.3 | 6.253986 |
4 | 2000.4 | 6.339953 |
5 | 2000.5 | 6.426557 |
6 | 2000.6 | 6.513788 |
7 | 2000.7 | 6.601634 |
8 | 2000.8 | 6.690085 |
9 | 2000.9 | 6.779130 |
Numerical Error#
With a numerical solution there are two types of error:
local truncation error at one time step;
global error which is the propagation of local error.
Derivation of Euler Local truncation error#
The left hand side of a initial value problem
Rearranging and letting
From this the local truncation error is
where
Derivation of Euler Local truncation error for the Population Growth#
As the exact solution
differentiate with respect to
subbing the original equation gives
which expresses the second derivative as a function of the exact solution
this gives a local trucation for
M=0.8
fig = plt.figure(figsize=(10,4))
plt.plot(time[0:2],0.1*M/2*np.ones(2),'v:'
,color='black',label='Upper Local Truncation')
plt.xlabel('time')
plt.ylim([0,0.1])
plt.legend(loc='best')
plt.title('Local Truncation Error')
plt.plot();

Global truncation error for the population equation#
For the population equation specific values
In this case
on
Specific Theorem Global Error
Let
and
Non-linear population equation with a temporal oscilation#
Given the specific population differential equation with a wiggle,
with the initial population at time
For the specific example of the population equation the difference equation is
for
w=np.zeros(N+1)
w[0]=6
for i in range (0,N):
w[i+1]=w[i]+h*(0.2*w[i]-0.01*w[i]*w[i]+np.sin(2*np.pi*time[i]))
fig = plt.figure(figsize=(10,4))
plt.plot(time,w,'s:',color='blue',label='Euler')
plt.xlim((min(time),max(time)))
plt.xlabel('time')
plt.legend(loc='best')
plt.title('Euler solution')
plt.plot();

Table#
The table below shows the iteration
d = {'time t_i': time[0:10], 'Euler (w_i) ':w[0:10]}
df = pd.DataFrame(data=d)
df
time t_i | Euler (w_i) | |
---|---|---|
0 | 2000.0 | 6.000000 |
1 | 2000.1 | 6.084000 |
2 | 2000.2 | 6.227443 |
3 | 2000.3 | 6.408317 |
4 | 2000.4 | 6.590522 |
5 | 2000.5 | 6.737676 |
6 | 2000.6 | 6.827034 |
7 | 2000.7 | 6.858187 |
8 | 2000.8 | 6.853211 |
9 | 2000.9 | 6.848203 |