0% found this document useful (0 votes)
7 views4 pages

Euler Method for ODEs Explained

Uploaded by

yunitahariani6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Euler Method for ODEs Explained

Uploaded by

yunitahariani6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

21/12/25, 12.

09 The Euler Method — Python Numerical Methods

This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide
Print at
for Engineers and Scientists, the content is also available to Berkeley
PDF Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better
learning experience. The code is released under the MIT license. If you find this content useful, please
consider supporting the work on Elsevier or Amazon!

< 22.2 Reduction of Order | Contents | 22.4 Numerical Error and Instability >

The Euler Method


dS(t)
Let dt
= F (t, S (t)) be an explicitly defined first order ODE. That is, F is a function that returns the
derivative, or change, of a state given a time and state value. Also, let t be a numerical grid of the interval [t0 , tf ]
with spacing h. Without loss of generality, we assume that t0 = 0 , and that tf = Nh for some positive integer,
N .

The linear approximation of S (t) around tj at tj+1 is

dS (tj )
S (tj+1 ) = S (tj ) + (tj+1 − tj ) ,
dt

which can also be written

S (tj+1 ) = S (tj ) + hF (tj , S (tj )).

This formula is called the Explicit Euler Formula, and it allows us to compute an approximation for the state at
S (tj+1 ) given the state at S (tj ) . Starting from a given initial value of S0 = S (t0 ) , we can use this formula to
integrate the states up to S (tf ) ; these S (t) values are then an approximation for the solution of the differential
equation. The Explicit Euler formula is the simplest and most intuitive method for solving initial value problems. At
any state (tj , S (tj )) it uses F at that state to “point” toward the next state and then moves in that direction a
distance of h. Although there are more sophisticated and accurate methods for solving these problems, they all
have the same fundamental structure. As such, we enumerate explicitly the steps for solving an initial value
problem using the Explicit Euler formula.

dS(t)
WHAT IS HAPPENING? Assume we are given a function F (t, S (t)) that computes , a numerical grid, t, of
dt

the interval, [t0 , tf ], and an initial state value S0 = S (t0 ) . We can compute S (tj ) for every tj in t using the
following steps.

1. Store S0 = S (t0 ) in an array, S .


2. Compute S (t1 ) = S 0 + hF (t0 , S 0 ) .
3. Store S1 = S (t1 ) in S .
4. Compute S (t2 ) = S 1 + hF (t1 , S 1 ) .
[Link] 1/4
21/12/25, 12.09 The Euler Method — Python Numerical Methods

5. Store S2 = S (t1 ) in S .
6. ⋯
7. Compute S (tf ) = S f −1 + hF (tf −1 , S f −1 ) .
8. Store Sf = S (tf ) in S .
9. S is an approximation of the solution to the initial value problem.

When using a method with this structure, we say the method integrates the solution of the ODE.

df (t)
TRY IT! The differential equation dt
= e
−t
with initial condition f 0 = −1 has the exact solution
f (t) = −e
−t
. Approximate the solution to this initial value problem between 0 and 1 in increments of 0.1 using
the Explicity Euler Formula. Plot the difference between the approximated solution and the exact solution.

import numpy as np
import [Link] as plt

[Link]('seaborn-poster')
%matplotlib inline

# Define parameters
f = lambda t, s: [Link](-t) # ODE
h = 0.1 # Step size
t = [Link](0, 1 + h, h) # Numerical grid
s0 = -1 # Initial Condition

# Explicit Euler Method


s = [Link](len(t))
s[0] = s0

for i in range(0, len(t) - 1):


s[i + 1] = s[i] + h*f(t[i], s[i])

[Link](figsize = (12, 8))


[Link](t, s, 'bo--', label='Approximate')
[Link](t, -[Link](-t), 'g', label='Exact')
[Link]('Approximate and Exact Solution \
for Simple ODE')
[Link]('t')
[Link]('f(t)')
[Link]()
[Link](loc='lower right')
[Link]()

In the above figure, we can see each dot is one approximation based on the previous dot in a linear fashion. From
the initial value, we can eventually get an approximation of the solution on the numerical grid. If we repeat the
process for h = 0.01 , we get a better approximation for the solution:

[Link] 2/4
21/12/25, 12.09 The Euler Method — Python Numerical Methods

h = 0.01 # Step size


t = [Link](0, 1 + h, h) # Numerical grid
s0 = -1 # Initial Condition

# Explicit Euler Method


s = [Link](len(t))
s[0] = s0

for i in range(0, len(t) - 1):


s[i + 1] = s[i] + h*f(t[i], s[i])

[Link](figsize = (12, 8))


[Link](t, s, 'b--', label='Approximate')
[Link](t, -[Link](-t), 'g', label='Exact')
[Link]('Approximate and Exact Solution \
for Simple ODE')
[Link]('t')
[Link]('f(t)')
[Link]()
[Link](loc='lower right')
[Link]()

The Explicit Euler Formula is called “explicit” because it only requires information at tj to compute the state at
tj+1 . That is, S (tj+1 ) can be written explicitly in terms of values we have (i.e., tj and S (tj ) ). The Implicit Euler
Formula can be derived by taking the linear approximation of S (t) around tj+1 and computing it at tj :

S (tj+1 ) = S (tj ) + hF (tj+1 , S (tj+1 )).

This formula is peculiar because it requires that we know S (tj+1 ) to compute S (tj+1 ) ! However, it happens that
sometimes we can use this formula to approximate the solution to initial value problems. Before we give details on
how to solve these problems using the Implicit Euler Formula, we give another implicit formula called the
Trapezoidal Formula, which is the average of the Explicit and Implicit Euler Formulas:

h
S (tj+1 ) = S (tj ) + (F (tj , S (tj )) + F (tj+1 , S (tj+1 ))).
2

To illustrate how to solve these implicit schemes, consider again the pendulum equation, which has been reduced
to first order.

dS (t) 0 1
= [ g ] S (t)
dt − 0
l

For this equation,

0 1
F (tj , S (tj )) = [ g ] S (tj ).
− 0
l

If we plug this expression into the Explicit Euler Formula, we get the following equation:

0 1 0 1 1 h
1 0
S (tj+1 ) = S (tj ) + h [ g ] S (tj ) = [ ] S (tj ) + h [ g ] S (tj ) = [ gh ] S (tj )
− 0 0 1 − 0 − 1
l l l

[Link] 3/4
21/12/25, 12.09 The Euler Method — Python Numerical Methods

Similarly, we can plug the same expression into the Implicit Euler to get

1 −h
[ gh
] S (tj+1 ) = S (tj ),
1
l

and into the Trapezoidal Formula to get

h h
1 − 1
2 2
[ ] S (tj+1 ) = [ ] S (tj ).
gh gh
1 − 1
2l 2l

With some rearrangement, these equations become, respectively,

−1
1 −h
S (tj+1 ) = [ gh ] S (tj ),
1
l

−1
h h
1 − 1
2 2
S (tj+1 ) = [ ] [ ] S (tj ).
gh gh
1 − 1
2l 2l

These equations allow us to solve the initial value problem, since at each state, S (tj ) , we can compute the next
state at S (tj+1 ) . In general, this is possible to do when an ODE is linear.

< 22.2 Reduction of Order | Contents | 22.4 Numerical Error and Instability >

© Copyright 2020.

[Link] 4/4

You might also like