To approximate Ordinary Differential Equations (ODEs) we normally use numerical methods and initial value problems.
A typical IVP is given by:
δtδy=f(y,t);a≤t≤b;y(a)=α
Where y(t) is what I want to approximate, f(y,t) is a known
function, a and b are the limits of the interval, and α
is the initial condition.
There are several methods to solve ODEs numerically:
Euler
Taylor
Runge-Kutta
Runge-Kutta-Fehlberg
Adams-Bashforth
Adams-Moulton
And so on. The method covered in this post is the Euler Method.
The Euler method is a simple numerical method. It is based on the idea of approximating the solution
of an ODE by a sequence of small steps. The method is named after the Swiss mathematician Leonhard
Euler, who first introduced it in the 18th century.
It is based on the Taylor series expansion of the function y(t) around the point ti :
For any ξi in the interval [ti,ti+1] . We can say that (ti+1−ti) is equal
to the step size h that is the element size. So, the Euler Method is:
yi+1=yi+h⋅f(yi,ti)
Where yi is the approximation of y(ti) and yi+1 is the approximation of y(ti+1) .
And both can be written as ωi and ωi+1 respectively. So the formula can be written as:
ω0=y(t0)=αωi+1=ωi+h⋅f(ωi,ti)
The Euler method is a first-order method, which means that the error is proportional to the step size h .
Example: Let’s solve the following ODE:
δtδy=y−t2+1;0≤t≤2;y(0)=0.5;h=0.2
First we have to analyze the value of the step size h . The interval is [0,2]
and the step size is 0.2 , so we have 10 steps. The values of t are:
t0=0.0,t1=0.2,t2=0.4,t3=0.6,t4=0.8,
t5=1.0,t6=1.2,t7=1.4,t8=1.6,t9=1.8
So that means that the number of iterations is equal to 10. What we have to do is, take the initial
function δtδy=y′=y−t2+1=f(ωi,ti) and apply the Euler method
to approximate the values of y in each step.
The initial condition is y(0)=0.5 .
ω0=0.5
ωi+1=ωi+h⋅(ωi−ti2+1)
So ω0+1=ω1 for the first iteration is going to be:
ω1=0.5+0.2⋅(0.5−02+1)=0.8
And the rest of iterations are:
ω2=0.8+0.2⋅(0.8−0.22+1)=1.152
ω3=1.152+0.2⋅(1.152−0.42+1=1.5504
ω4=1.5504+0.2⋅(1.5504−0.62+1)=1.9884
ω5=1.9884+0.2⋅(1.9884−0.82+1)=2.4580
ω6=2.4580+0.2⋅(2.4580−1.02+1)=2.9498
ω7=2.9498+0.2⋅(2.9498−1.22+1)=3.4517
ω8=3.4517+0.2⋅(3.4517−1.42+1)=3.9501
ω9=3.9501+0.2⋅(3.9501−1.62+1)=4.4281
ω10=4.4281+0.2⋅(4.4281−1.82+1)=4.8657
So the approximate value of y(2) is ω10=4.8657 . For comparison, the exact solution of the ODE is:
y(t)=t2+2t+1−0.5et
So the exact value of y(2) is:
y(2)=22+2⋅2+1−0.5e2=5.3054
So the error of the Euler method is:
Error=∣4.8657−5.3054∣=0.4397
The error is 0.4397, which is a good approximation for the Euler method.
Below we can see the plot of the exact solution and the Euler method approximation.
And below, we can see the Python code that implements the Euler method for the ODE:
def euler(edo, a, b, h, y0): t = np.linspace(a, b, int(np.ceil(b-a)/h)+1) n = np.zeros(len(t)) n[0] = y0 for i in range(0, len(t)-1): k = edo(t[i], n[i]) n[i+1] = n[i] + h * k return t, n
So, to sum up the Euler method is considered a simple method to approximate ODEs. The method is easy
to implement and understand, but it is not very accurate. For more accurate methods, we can use the
Runge-Kutta method or the Adams-Bashforth method.
André Albano @onablaerdna
I'm a Geophysicist and Coder. I work with seismic inversion algorithms,
software development, well log analysis and so on.