0% found this document useful (0 votes)
206 views

ECE 3040 Lecture 22: Numerical Solution of Differential Equations

Uploaded by

Aqe Kitam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views

ECE 3040 Lecture 22: Numerical Solution of Differential Equations

Uploaded by

Aqe Kitam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

ECE 3040

Lecture 22: Numerical Solution of Differential Equations


© Prof. Mohamad Hassoun

This lecture covers the following topics:

 Introduction
 Euler’s method for solving first-order ODEs
 Euler’s method for higher-order ODEs
 Error analysis of Euler’s method
 Improving Euler’s method: Heun’s & Runge-Kutta methods
 Matlab’s function ode45
 Case study: Bungee jumper
 Appendix: Symbolic solution of ODE’s using Matlab’s dsolve
Introduction

Differential equations are common place in engineering. They arise from the
analysis of engineering systems as mathematical models that describe the behavior
of a variable physical quantity, 𝑦(𝑥), such as electric current, voltage, displacement,
velocity, acceleration, fluid flow, heat transfer, etc. In such models, the unknown
𝑑𝑦 𝑑2 𝑦
function 𝑦(𝑥) appears in differentiated form ( = 𝑦 = 𝑦̇ , 2 = 𝑦 ′′ = 𝑦̈ , …). The

𝑑𝑥 𝑑𝑥
solution of a differential equation involves obtaining an analytical expression for
the function, 𝑦(𝑥). However, in some cases, the differential equation can involve
nonlinearities and might have no analytic solution. Also, some engineering systems
are described by a large set of dependent differential equations (linear or nonlinear).
In such cases, numerical solutions are the only feasible solutions.

The following is an example of a simple differential equation,

𝑑𝑦(𝑥)
= 𝑥2 − 1
𝑑𝑥

This differential equation is classified as an ordinary differential equation (or ODE)


because it involves one independent variable, 𝑥. It is also a first-order differential
equation because the unknown function appears in first derivative form. This
particular differential equation can be solved analytically by integrating both sides
of the equation with respect to 𝑥, to obtain

𝑑𝑦(𝑥)
∫ 𝑑𝑥 = ∫(𝑥 2 − 1)𝑑𝑥
𝑑𝑥
or
∫ 𝑑𝑦(𝑥) = ∫(𝑥 2 − 1)𝑑𝑥
or
1
𝑦(𝑥 ) = 𝑥 3 − 𝑥 + 𝐶
3

where 𝐶 is a constant of integration that can be obtained if we know one value of


1 2
𝑦(𝑥). For example if 𝑦(1) = 0, then 𝐶 is the solution of 0 = − 1 + 𝐶, or 𝐶 = .
3 3
The following is another example of a linear, first-order ODE with a specified
initial condition
𝑑𝑦(𝑡)
+ 2𝑦(𝑡) = 4, 𝑦(0) = 3
𝑑𝑡

In your differential equations course you have learned how to systematically solve
this equation to obtain the solution, 𝑦(𝑡) = 𝑒 −2𝑡 + 2. This solution can be verified
by confirming that it satisfies the above differential equation:

𝑑 −2𝑡
(𝑒 + 2) + 2(𝑒 −2𝑡 + 2) = −2𝑒 −2𝑡 + 2𝑒 −2𝑡 + 4 = 4
𝑑𝑡

The order of the differential equation is determined by the physical nature of the
system that generates it. For example, a linear electric circuit that contain 𝑛
independent energy storing elements (capacitors and/or inductors) is governed by
the 𝑛th-order linear differential equation [𝑔(𝑡) represents the source that drives the
circuit. In some cases, 𝑔(𝑡) = 0]

𝑑𝑛 𝑦 𝑑𝑛−1 𝑦 𝑑𝑦
𝑎𝑛 𝑛 + 𝑎𝑛−1 𝑛−1 + ⋯ + 𝑎1 + 𝑎0 𝑦 = 𝑔(𝑡)
𝑑𝑡 𝑑𝑡 𝑑𝑡

This differential equation requires 𝑛 initial conditions 𝑦(𝑡0 ), 𝑦 ′ (𝑡0 ), 𝑦 ′′ (𝑡0 ),


𝑦 (3) (𝑡0 ), … , 𝑦 (𝑛−1) (𝑡0 ); typically, 𝑡0 = 0. For example, the following linear circuit
has one capacitor and one inductor. Therefore, it is a second-order circuit.

Circuit analysis techniques allow us to solve for the differential equation describing,
say, the current 𝑖(𝑡) that flows through the capacitor in the above circuit.
Application of such techniques leads to the differential equation

𝑑2 𝑖(𝑡) 𝑑𝑖 (𝑡)
3 + 2 + 𝑖(𝑡) = 0
𝑑𝑡 2 𝑑𝑡
𝑑𝑖(0)
The initial conditions 𝑖(0) and 𝑖 ′ (0) = can be determined from the circuit if
𝑑𝑡
the capacitor voltage and inductor current are known at 𝑡 = 0. It can be shown that
when the capacitor’s initial voltage is 1Volt and the inductor’s initial current is
√2
zero, then 𝑖(0) = 0 and 𝑖 ′ (0) = − . The solution to the differential equation
6
would then be
1 −1 𝑡 √2
( )
𝑖 𝑡 = − 𝑒 3 sin ( 𝑡)
2 3

As mentioned earlier, some differential equations have no analytical solution and,


therefore, numerical methods must be used. The following are two specific
examples.

A swinging pendulum (with damping coefficient, 𝛾)

The sum of the tangential forces must be zero (by Newton’s second law of motion),

𝑚𝑎𝑡 − ∑ 𝐹⃗𝑡 = 0

𝑑 2 𝜃(𝑡) 𝑑𝜃(𝑡)
𝑚𝐿 + 𝛾𝐿 + 𝑚𝑔 sin[𝜃(𝑡)] = 0
𝑑𝑡 2 𝑑𝑡

Where 𝑎𝑡 = 𝐿𝜃̈ is tangential acceleration and 𝑚 is the mass. For small swings we may write
sin(𝜃) ≅ 𝜃 (think Taylor series) and the system becomes linear.

Predator-prey model (Lotka-Volterra system of two coupled nonlinear differential


equations):
𝑑𝑥 (𝑡)
= 1.2𝑥 − 0.6𝑥𝑦
𝑑𝑡
𝑑𝑦(𝑡)
= −0.8𝑦 + 0.3𝑥𝑦
𝑑𝑡
Euler’s Method for Solving First-Order ODEs

In this section, numerical solutions are derived for first-order ODEs of the form

𝑑𝑦(𝑡)
𝑦 ′ (𝑡) = = 𝑓(𝑡, 𝑦(𝑡))
𝑑𝑡

Substituting the simple forward finite-difference approximation (with step size ℎ,


0 < ℎ < 1) for the first-derivative at the point 𝑡𝑖 , namely,

𝑑𝑦(𝑡𝑖 ) 𝑦(𝑡𝑖+1 ) − 𝑦(𝑡𝑖 ) 1


≅ ≜ [𝑦(𝑡𝑖+1 ) − 𝑦(𝑡𝑖 )]
𝑑𝑡 𝑡𝑖+1 − 𝑡𝑖 ℎ

in the above ODE expression and solving for 𝑦(𝑡𝑖+1 ) lead to

𝑦(𝑡𝑖+1 ) ≅ 𝑦(𝑡𝑖 ) + ℎ𝑓(𝑡𝑖 , 𝑦(𝑡𝑖 )) (1)

This result can be discretized and expressed as the iterative formula (known as
Euler’s method) for estimating 𝑦(𝑘 + 1) from its current value 𝑦(𝑘),

𝑦(𝑘 + 1) = 𝑦(𝑘) + ℎ𝑓(𝑡0 + 𝑘ℎ, 𝑦(𝑘))

Starting from the (given) initialization 𝑦(0) = 𝑦(𝑡0 ) = 𝑦0 (typically, 𝑡0 is zero).

Example. Use Euler’s method to solve 𝑦 ′ + 0.5𝑦 = 4𝑒 0.8𝑡 from 𝑡 = 0 to 4, with a


step size ℎ = 1, 𝑡0 = 0 and 𝑦(0) = 2. The exact solution for this linear differential
40
equation can be shown to be 𝑦(𝑡) = (𝑒 0.8𝑡 − 𝑒 −0.5𝑡 ) + 2𝑒 −0.5𝑡 .
13
Solution. We first express the differential equation as
𝑦 ′ = 𝑓(𝑡, 𝑦) = 4𝑒 0.8𝑡 − 0.5𝑦
and then express it as an Euler’s iterative formula,
𝑦(𝑘 + 1) = 𝑦(𝑘) + ℎ(4𝑒 0.8 (𝑡0 +𝑘ℎ) − 0.5𝑦(𝑘))
With 𝑡0 = 0 and ℎ = 1, we obtain
𝑦(𝑘 + 1) = 𝑦(𝑘) + 4𝑒 0.8𝑘 − 0.5𝑦(𝑘) = 0.5𝑦(𝑘) + 4𝑒 0.8𝑘
Initialization: 𝑦(0) = 2.
First iteration (𝑘 = 0):
𝑦(1) = 0.5𝑦(0) + 4𝑒 0.8 (0) = (0.5)2 + 4(1) = 5
Second iteration (𝑘 = 1):
𝑦(2) = 0.5𝑦(1) + 4𝑒 0.8 (1) = 0.5(5) + 4𝑒 0.8 = 11.40216
and so on. The results for the first four iterations along with the absolute relative
true error (in percent) are summarized in the following table.
𝑡 = 𝑘ℎ 𝑦𝑡𝑟𝑢𝑒 𝑦𝐸𝑢𝑙𝑒𝑟 |𝜀𝑟 |
0 2.00000 2.00000 --
1 6.19463 5.00000 19.28 %
2 14.84392 11.40216 23.19 %
3 33.67717 25.51321 24.24 %
4 75.33896 56.84931 24.54 %

Notice how the error increases as 𝑡 increases. The error is due to the finite step size
(ℎ). This local error also propagates from one step to the next and leads to a
cumulative global error that deteriorates the accuracy of the solution as 𝑡 increases.
Of course, choosing a smaller value for ℎ will improve the results.
The following user-defined Matlab function (ode_eul) implements Euler’s method
for solving a first-order ODE.
The ode_eul solution (green trace) using ℎ = 0.1 is compared to the exact solution
(blue trace) as shown in the following plot, for 𝑡 ∈ [𝑡𝑖 𝑡𝑓 ] = [0 4]. Note how f is a
function of t and y.
Euler’s Method for Higher-Order ODEs

Many engineering problems involve a single high-order differential equation that


can be transformed into the following system of (dependent) first-order ODEs,

𝑑𝑦1
= 𝑓1 (𝑡, 𝑦1 , 𝑦2 , … , 𝑦𝑛 )
𝑑𝑡
𝑑𝑦2
= 𝑓2 (𝑡, 𝑦1 , 𝑦2 , … , 𝑦𝑛 )
𝑑𝑡
.
.
.
𝑑𝑦𝑛
= 𝑓𝑛 (𝑡, 𝑦1 , 𝑦2 , … , 𝑦𝑛 )
𝑑𝑡

with the 𝑛 associated initial conditions 𝑦𝑖 (𝑡0 ), 𝑖 = 1,2, 3, … , 𝑛.

Euler’s method can readily be extended to solve the above system of equations. The
method is applied to each equation starting from the initial values, 𝑦𝑖 (𝑡0 ), and a set
of new values, 𝑦𝑖 (𝑡0 + ℎ), is obtained. Then, the iterations are repeated over and
over until some 𝑡𝑓 is reached. Euler’s method for a system of ODEs takes the form

𝑦1 (𝑘 + 1) = 𝑦1 (𝑘) + ℎ𝑓1 (𝑡0 + 𝑘ℎ, 𝑦1 (𝑘), 𝑦2 (𝑘), … , 𝑦𝑛 (𝑘))


𝑦2 (𝑘 + 1) = 𝑦2 (𝑘) + ℎ𝑓2 (𝑡0 + 𝑘ℎ, 𝑦1 (𝑘), 𝑦2 (𝑘), … , 𝑦𝑛 (𝑘))
.
.
.
𝑦𝑛 (𝑘 + 1) = 𝑦𝑛 (𝑘) + ℎ𝑓𝑛 (𝑡0 + 𝑘ℎ, 𝑦1 (𝑘), 𝑦2 (𝑘), … , 𝑦𝑛 (𝑘))

where the first iteration (corresponding to setting 𝑘 = 0) starts with the specified
initial values:
𝑦1 (𝑡0 ), 𝑦2 (𝑡0 ), … , 𝑦𝑛 (𝑡0 )

Example. Solve (numerically) the following second-order linear ODE for 𝑡 ≥ 0.


√2
Assume the initial conditions 𝑦(0) = 0 and 𝑦′(0) = − . Set the step size to 0.1.
6
𝑑2 𝑦(𝑡) 𝑑𝑦(𝑡)
3 + 2 + 𝑦(𝑡) = 0
𝑑𝑡 2 𝑑𝑡

Solution. First, we provide the exact solution for this differential equation
employing Matlab’s symbolic toolbox function dsolve (refer to the Appendix),

1 𝑡 √2
𝑦(𝑡) = − 𝑒 −3 sin ( 𝑡)
2 3

The present differential equation can be expressed as a system of two, first-order


𝑑𝑦 𝑑𝑦1
ODEs by renaming 𝑦 as 𝑦1 and defining 𝑦2 = = ,
𝑑𝑡 𝑑𝑡

𝑑𝑦1
= 𝑦2 (Leading to: 𝑓1 (𝑡, 𝑦1 , 𝑦2 ) = 𝑦2 )
𝑑𝑡
𝑑𝑦2 𝑑2 𝑦 1 2 1 2
= = − 𝑦1 − 𝑦2 (Leading to: 𝑓2 (𝑡, 𝑦1 , 𝑦2 ) = − 𝑦1 − 𝑦2 )
𝑑𝑡 𝑑𝑡 2 3 3 3 3

The system’s associated initial conditions are 𝑦1 (0) = 0 and 𝑦2 (0) = 𝑦′(0) =
√2
− . The Euler’s iterative system (setting 𝑡0 = 0)
6

𝑦1 (𝑘 + 1) = 𝑦1 (𝑘) + ℎ𝑓1 (𝑘ℎ, 𝑦1 (𝑘), 𝑦2 (𝑘))


𝑦2 (𝑘 + 1) = 𝑦2 (𝑘) + ℎ𝑓2 (𝑘ℎ, 𝑦1 (𝑘), 𝑦2 (𝑘))

can then be expressed as

𝑦1 (𝑘 + 1) = 𝑦1 (𝑘) + 0.1𝑦2 (𝑘)


1 2
𝑦2 (𝑘 + 1) = 𝑦2 (𝑘) + 0.1 (− 𝑦1 (𝑘) − 𝑦2 (𝑘)) = −0.0333𝑦1 (𝑘) + 0.9333𝑦2 (𝑘)
3 3

First iteration (set 𝑘 = 0 and use the initial conditions):


√2
𝑦1 (1) = 𝑦1 (0) + 0.1𝑦2 (0) = 0 + 0.1 (− ) = −0.0236
6

√2
𝑦2 (1) = −0.0333𝑦1 (0) + 0.9333𝑦2 (0) = −0.0333(0) + 0.9333 (− ) = −0.2200
6
Second iteration [set 𝑘 = 1 and use the just computed 𝑦1 (1) and 𝑦2 (1)]:
𝑦1 (2) = 𝑦1 (1) + 0.1𝑦2 (1) = −0.0236 + 0.1(−0.2200) = −0.0456
𝑦2 (2) = −0.0333𝑦1 (1) + 0.9333𝑦2 (1) = −0.0333(−0.0236) + 0.9333(−0.2200)
= −0.2045

1 −𝑡 √2
The exact solution 𝑦(𝑡) = − 𝑒 3 sin ( 𝑡) gives 𝑦(0.1) = −0.0228 and 𝑦(0.2) =
2 3
−0.0440. We may then compute the following absolute relative true errors (in
percent) for the numerical solution for 𝑦1 as,

At 𝑡 = 0.1:
𝑦(0.1) − 𝑦1 (1) −0.0228 − (−0.0236)
𝜀𝑟 = | | 100% = | | 100% = 3.5%
𝑦(0.1) −0.0228
At 𝑡 = 0.2:
𝑦(0.2) − 𝑦1 (2) −0.0440 − (−0.0456)
𝜀𝑟 = | | 100% = | | 100% = 3.6%
𝑦(0.2) −0.0440
The following user-defined Matlab function (ode_eul2) implements Euler’s method
for solving a system of two first-order ODEs.

The following Matlab instructions generate the solution of the differential equation
(from the last example) using ode_eul2, with ℎ = 0.1 and 0 ≤ 𝑡 ≤ 15,
The graph below compares Euler’s solution (blue trace) to the exact solution (green
trace). Obviously, making ℎ smaller will improve the approximation.

Consider the variable 𝑦(𝑡) and its derivative 𝑦′(𝑡) from the above system of linear
ODEs. When the system is stable (i.e., lim 𝑦′(𝑡) = 0), the trajectory of points
𝑡→∞
{𝑦(𝑡), 𝑦′(𝑡)} converges to a fixed-point (also known as an attractor or equilibrium
point) starting from any initial condition {𝑦(𝑡0 ), 𝑦′(𝑡0 )}. By setting all rates of
change to zero, one can obtain the system of equations
𝑑𝑦1
= 𝑦2 = 0
𝑑𝑡
𝑑𝑦2 1 2
= − 𝑦1 − 𝑦2 = 0
𝑑𝑡 3 3

whose solution leads to the (equilibrium) fixed-point (0, 0).


The phase-plane trajectory, a plot of 𝑦 ′ (𝑡) versus 𝑦(𝑡), is depicted in the following
graph for the initial points (1, 1) and (-1, -1). Here, the point (0, 0) is an attractor.
Note: A stable linear ODE always has a single attractor.
The previous example involved a second-order linear ODE. Exact analytical
solutions are readily available for such ODE. One example of a system of ODEs
that can only be solved numerically is the predator-prey model of the two
dependent nonlinear differential equations
𝑑𝑥 (𝑡)
= 𝑎𝑥 − 𝑏𝑥𝑦
𝑑𝑡
𝑑𝑦(𝑡)
= −𝑐𝑦 + 𝑑𝑥𝑦
𝑑𝑡

In the above model, 𝑥 and 𝑦 represent the number of prey and predators,
respectively, 𝑎 is the prey growth rate, 𝑐 is the predator death rate and 𝑏 and 𝑑 are
coupling coefficients (they characterize the predator-prey interactions). The
multiplicative terms (𝑥𝑦) are what makes these equations nonlinear. The equations
are also known as the Lotka-Volterra equations that were developed independently
in the early part of the twentieth century by the American biologist Alfred Lotka
and the Italian mathematician Vito Volterra.
Example. Consider the following predator-prey model with initial conditions
𝑥(0) = 2 and 𝑦(0) = 1.
𝑑𝑥 (𝑡)
= 1.2𝑥 − 0.6𝑥𝑦
𝑑𝑡
𝑑𝑦(𝑡)
= −0.8𝑦 + 0.3𝑥𝑦
𝑑𝑡
Solve the system for 0 ≤ 𝑡 ≤ 30 using Euler’s method (try step sizes of ℎ = 0.05
and 0.001). Plot and compare the phase-plane trajectories [i.e., plot 𝑦(𝑡) versus
𝑥(𝑡)].

Solution. We first re-label 𝑥 as 𝑦1 and 𝑦 as 𝑦2. Then, function ode_eul2 is used to


solve the system of ODEs as follows,

The method diverges (in a counter-clock-wise direction) for ℎ = 0.05 due to


excessive global error, as shown in the following phase-plane plot. In numerical
solutions, based on Euler’s method, simulations with progressively smaller step
sizes are advised in order to obtain a meaningful solution.

The method converges to an orbit for ℎ = 0.001 (or smaller values), as shown in
the following plot. It is interesting to see how the model predicts an ever-lasting,
periodic change in populations.
As the prey population increases, the abundance of food flourishes the predator
population. But, as the predator population increases at a rate faster than can be
supported by the prey, the prey population starts to dwindle. This, in turn, leads to a
drop in the predator population due to lack of food. This drop continues until both
populations reach the initial population values, 𝑥(0) and 𝑦(0), and the cycle
repeats.

It should be noted here that the initial conditions for this model are scaled. For
example, you can think of 𝑥(0) = 2 and 𝑦(0) = 1 to mean initial prey and predator
populations of 2000 and 1000, respectively.

Also, note that there is no equilibrium point for this particular dynamical system at
which both populations become constant. However, there are other sets of
coefficients {𝑎, 𝑏, 𝑐, 𝑑 } that can lead to a critical equilibrium point where the
populations are constant. This equilibrium point can be derived by setting both
rates, 𝑦̇ and 𝑥̇ , to zero:
0 = 𝑎𝑥 − 𝑏𝑥𝑦
0 = −𝑐𝑦 + 𝑑𝑥𝑦

𝑐 𝑎
and obtain the two solutions: (𝑥, 𝑦) = (0,0) and ( , ). Therefore, if we start with
𝑑 𝑏
no population (trivial case), we will never “create” predators nor prey! The other
solution is more interesting. It implies that if we start our model with the initial
𝑐 0.8 8 𝑎 1.2
populations 𝑥(0) = = = and 𝑦(0) = = = 2, then the populations will
𝑑 0.3 3 𝑏 0.6
never change.

Your turn: Employ Euler’s method (with the proper value for ℎ and 𝑡 ∈ [0 𝑡𝑓 ]) to
generate the phase-plane plot [𝜃̇ (𝑡) versus 𝜃(𝑡)] for the unforced pendulum with
damping, described by the nonlinear differential equation

𝜃̈ (𝑡) + 0.5𝜃̇ (𝑡) + 9.8sin(𝜃 (𝑡)) = 0

𝜋 𝜋 𝜋
Generate plots for the initial conditions 𝜃̇ (0) = 0 and 𝜃 (0) = , , .
6 4 2

Ans. Your plot should look like this:


Error Analysis of Euler’s Method
An insight into the convergence behavior of Euler’s method can be gained by
deriving the approximation (used in Euler’s method)

1
𝑦′(𝑡𝑖 ) ≅ [𝑦(𝑡𝑖+1 ) − 𝑦(𝑡𝑖 )]

directly from the Taylor series expansion of 𝑦(𝑥),

(𝑥 − 𝑎) ′ (𝑥 − 𝑎)2 ′′ (𝑥 − 𝑎)𝑛−1 (𝑛−1)


𝑦(𝑥) = 𝑦(𝑎) + 𝑦 (𝑎) + 𝑦 (𝑎) + ⋯ + 𝑦 (𝑎) + 𝑅𝑛
1! 2! (𝑛 − 1)!
where 𝑅𝑛 is the truncation error
𝑦 (𝑛) (𝛾 )
𝑅𝑛 = (𝑥 − 𝑎)𝑛
𝑛!
If we set the expansion point 𝑎 to 𝑡𝑖 , set 𝑥 to 𝑡𝑖+1 , set ℎ = 𝑡𝑖+1 − 𝑡𝑖 and truncate at
the second-order term, we obtain the approximation

𝑦(𝑡𝑖 + 1) ≅ 𝑦(𝑡𝑖 ) + ℎ𝑦 ′ (𝑡𝑖 )


or
1
𝑦 ′ (𝑡𝑖 ) ≅ [𝑦(𝑡𝑖+1 ) − 𝑦(𝑡𝑖 )]

with truncation error
𝑦 ′′ (𝛾 )
2
𝑅2 = ℎ
2

Therefore, at the first iteration, Euler method introduces an 𝑂 (ℎ2 ) error between the
estimated 𝑦(𝑡0 + ℎ) and its true value. However, this error is amplified in the
second iteration where the local error has a component that depends on the
propagated error from the first iteration, and so on. It can be shown that the global
truncation error due to repeatedly applying Euler’s method is of order 𝑂(ℎ).
Therefore, Euler’s method is referred to as a first-order method. And this result
explains why this method requires a very small step size to ensure accurate results.
But, using a very small step size leads to an increase in the number of function
evaluations, thus slowing down the computations and potentially introducing
machine round-off error.

It should be recalled that the order of convergence of the integration method


determines the class of problems that the method is most efficient at. For example,
the first-order Euler’s method is exact for ODE solutions 𝑦(𝑡) that are linear or
constant. A second-order method will give exact results when the solutions are
quadratic (or linear or constant), and so on. In general, an 𝑛th-order integration
method for ODEs will provide highly accurate solutions (with relatively small step
size) when the solutions can be well approximated by an 𝑛th-order polynomial.
Improving Euler’s Method: Heun’s & Runge-Kutta Methods

One approach to improve the accuracy of Euler’s method is to approximate the


derivative using the average of two numerical derivatives: one evaluated at the
beginning of the interval and the second evaluated at the end of the interval (refer to
the figure below). This leads to Heun’s method, described next.

For Euler’s method, the slope at the beginning of the interval (at 𝑡𝑖 ) is
𝑦′(𝑡𝑖 ) = 𝑓(𝑡𝑖 , 𝑦(𝑡𝑖 ))
One application of Euler’s method provides an estimate of 𝑦 at 𝑡𝑖+1 . Let us refer to
this estimate as 𝑦̃(𝑡𝑖+1 ),
𝑦̃(𝑡𝑖+1 ) = 𝑦(𝑡𝑖 ) + ℎ𝑦′(𝑡𝑖 ) = 𝑦(𝑡𝑖 ) + ℎ𝑓(𝑡𝑖 , 𝑦(𝑡𝑖 ))
This expression is referred to as the predictor equation. It leads to the following
derivative approximation at the end of the interval (at 𝑡𝑖+1 ),
𝑦′(𝑡𝑖+1 ) = 𝑓(𝑡𝑖+1 , 𝑦̃(𝑡𝑖+1 ))
An improved estimate of the slope can now be obtained by averaging the slopes
𝑦′(𝑡𝑖 ) and 𝑦′(𝑡𝑖+1 ), to obtain


𝑦 ′ (𝑡𝑖 ) + 𝑦′(𝑡𝑖+1 ) 𝑓(𝑡𝑖 , 𝑦(𝑡𝑖 )) + 𝑓(𝑡𝑖+1 , 𝑦̃(𝑡𝑖+1 ))
𝑦̅ = =
2 2
Finally, this average slope is employed in Euler’s method to arrive at the improved
estimate (known as the corrector equation),
𝑓(𝑡𝑖 , 𝑦(𝑡𝑖 )) + 𝑓(𝑡𝑖+1 , 𝑦̃(𝑡𝑖+1 ))
𝑦(𝑡𝑖+1 ) = 𝑦(𝑡𝑖 ) + ℎ𝑦̅ ′ = 𝑦(𝑡𝑖 ) + ℎ
2
In fact one can, yet, enhance the accuracy of the method by performing local
iterations (using the corrector equation) where the estimate 𝑦(𝑡𝑖+1 ) is recycled
several times in the same equation [i.e., substituted for 𝑦̃(𝑡𝑖+1 )]:

A relative error stopping criterion [between two consecutive 𝑦(𝑡𝑖+1 ) estimates] can
then be used to stop this iterative phase. Next, apply the whole process, again, at the
next time step (𝑡𝑖+2 ) employing the refined 𝑦(𝑡𝑖+1 ) value, and so on. It has been
shown that Heun’s method has a global error that is 𝑂(ℎ2 ), making it much more
accurate (for a given ℎ) than Euler’s method.

Example. Apply three corrector cycles of Heun’s algorithm to approximate 𝑦(1)


given the following differential equation. Assume, 𝑡0 = 0, ℎ = 1 and 𝑦(0) = 2.
𝑦 ′ = 𝑓(𝑡, 𝑦) = 4𝑒 0.8𝑡 − 0.5𝑦
Solution. The following are the iterative formulas for Heun’s method,
Predictor formula: 𝑦̃(𝑘 + 1) = 𝑦(𝑘) + ℎ𝑓(𝑡0 + 𝑘ℎ, 𝑦(𝑘))
Corrector formula:
𝑓(𝑡0 + 𝑘ℎ, 𝑦(𝑘)) + 𝑓(𝑡0 + (𝑘 + 1)ℎ, 𝑦̃(𝑘 + 1))
𝑦(𝑘 + 1) = 𝑦(𝑘) + ℎ
2
Using 𝑡0 = 0, ℎ = 1, 𝑦(0) = 2 and 𝑓(𝑡, 𝑦) = 4𝑒 0.8𝑡 − 0.5𝑦 we obtain (for 𝑘 = 0):
The predictor formula,
𝑦̃(1) = 𝑦(0) + (1)(4𝑒 0.8(0) − 0.5𝑦(0))
and the corrector formula,
[4𝑒 0.8(0) − 0.5𝑦(0)] + [4𝑒 0.8(1) − 0.5𝑦̃(1)]
𝑦(1) = 𝑦(0) + (1)
2
Next, we apply the predictor formula to obtain
𝑦̃(1) = 𝑦(0) + (1) (4𝑒 0.8(0) − 0.5𝑦(0)) = 2 + (4 − 0.5(2)) = 𝟓

Then, we cycle through the corrector formula as follows.

[4𝑒 0.8(0) −0.5𝑦(0)]+[4𝑒 0.8(1) −0.5𝑦̃(1)]


Corrector cycle 1: 𝑦(1) = 𝑦(0) + (1)
2
1
= 2 + [(4 − 1) + (8.90216 − 0.5(5))] = 𝟔. 𝟕𝟎𝟏𝟎𝟖
2
(3)+[4𝑒 0.8(1) −0.5(6.70108)]
Corrector cycle 2: 𝑦(1) = 𝑦(0) + (1)
2
1
= 2 + [(3) + (8.90216 − 3.35054)] = 𝟔. 𝟐𝟕𝟓𝟖𝟏
2
1
Corrector cycle 3: 𝑦(1) = 2 + [(3) + (8.90216 − 0.5(6.27581))] = 𝟔. 𝟑𝟖𝟐𝟏𝟑
2

Your turn: Show that 𝑦(1) ≅ 6.36086 after 10 corrector cycles.

The results for Heun’s method are summarized in the following table and are
compared to a single application of Euler’s method. The error measure used in the
table is the percent absolute relative true error.

(ℎ = 1) 𝑦𝑡𝑟𝑢𝑒 𝑦𝐸𝑢𝑙𝑒𝑟 |𝜀𝐸𝑢𝑙𝑒𝑟 | 𝑦𝐻𝑒𝑢𝑛 |𝜀𝐻𝑒𝑢𝑛 |


𝑡=0 2.00000 2.00000 -- 2.00000 --

𝑡=1 6.19463 5.00000 19.28 % 6.70108 8.18 %


Corrector cycle 1

𝑡=1 -- -- 6.27581 1.31%


Corrector cycle 2

𝑡=1 -- -- 6.38213 3.03%


Corrector cycle 3

𝑡=1 -- -- 6.36086 2.68%


Corrector cycle 10
Your turn: Email your function and solution in a report format to your instructor
by Sunday, before 5:00 pm. Write a Matlab function that implements Heun’s
method for a first-order ODE. Your function should have the same inputs and
outputs as function ode_eul with the addition of one new input, 𝑚; where 𝑚 is the
number of corrector steps to be taken. (1) Verify the above example using your
function. (2) Use your function to solve the ODE in the above example for 𝑡 ∈
[0 4] (use ℎ = 1 and 𝑚 = 3). Compare (by graphing) your solution to that of
Euler’s method (with ℎ = 1) and to the exact solution. Your graph should look like
the one in the following figure.

Runge-Kutta Methods. So far, we have seen that both Euler’s and Heun’s methods
have the general form
𝑦(𝑡𝑖+1 ) = 𝑦(𝑡𝑖 ) + ℎ 𝜑(ℎ, 𝑡𝑖 , 𝑦(𝑡𝑖 ))
where the increment function 𝜑 is designed to estimate the slope over the interval.
The Runge-Kutta (RK) methods belong to this general iterative equation. RK
methods exist with varying order of accuracy, the most popular (and useful) is the
fourth-order RK method (RK4) which has a global truncation error of 𝑂(ℎ4 ). The
following is the RK4 iterative formula for solving 𝑦′(𝑡𝑖 ) = 𝑓(𝑡𝑖 , 𝑦(𝑡𝑖 )),

𝑦(𝑡𝑖+1 ) = 𝑦(𝑡𝑖 ) + (𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4 )
6
where the 𝑘𝑖 functions are given by

𝑘1 = 𝑓(𝑡𝑖 , 𝑦(𝑡𝑖 ))
ℎ ℎ
𝑘2 = 𝑓 (𝑡𝑖 + , 𝑦(𝑡𝑖 ) + 𝑘1 )
2 2
ℎ ℎ
𝑘3 = 𝑓 (𝑡𝑖 + , 𝑦(𝑡𝑖 ) + 𝑘2 )
2 2
𝑘4 = 𝑓(𝑡𝑖 + ℎ, 𝑦(𝑡𝑖 ) + ℎ𝑘3 )

Similar in spirit to Heun’s method, the RK4 method can be thought of as applying
an improved estimate of the slope by using a weighted-average of four different
slope estimates. The following example illustrates the application of the RK4
method.

Example. Employ the RK4 method to integrate 𝑦 ′ = 4𝑒 0.8𝑡 − 0.5𝑦 from 𝑡 = 0 to


1, using step size ℎ = 1 and 𝑦(0) = 2. (Note: Euler’s method error was 19.28% and
Heun’s method error, with 10 correction cycles, was 2.68%).

Solution. Here are the calculations involved:


𝑘1 = 𝑓(0,2) = 4𝑒 0.8(0) − 0.5(2) = 3
𝑘2 = 𝑓(0.5, 2 + 0.5(3)) = 𝑓(0.5, 3.5) = 4𝑒 0.8(0.5) − 0.5(3.5) = 4.2173
𝑘3 = 𝑓(0.5, 2 + 0.5(4.2173)) = 𝑓(0.5, 4.1087) = 4𝑒 0.8(0.5) − 0.5(4.1087)
= 3.9130
𝑘4 = 𝑓(1, 2 + (1)(3.9130)) = 𝑓(1, 5.9130) = 4𝑒 0.8(1) − 0.5(5.9130) = 5.9457

Therefore, the estimate at 𝑡 = 1 is



𝑦(1) ≅ 𝑦(0) + (𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4 )
6
1
= 2 + (3 + 2(4.2173) + 2(3.9130) + 5.9457) = 6.2010
6
6.1946−6.2010
which leads to an absolute relative true error of | | 100% = 0.1%.
6.1946

This result is superior (in accuracy) to the one obtained using Euler’s and Heun’s
methods. However, one should keep in mind that the slope estimate of RK4 is more
elaborate than the simple one used by Euler’s method (RK4 uses four function
evaluations and Euler’s method uses only one).

Your turn: (See Problem 8 in Assignment 14) Develop a Matlab function that
implements the RK4 method. Test your function for the ODE of the last example.
Compare (by graphing) the RK4 solution to that of Euler’s method and to the exact
solution (use ℎ = 1, and 𝑡 ∈ [0 4]).

Your turn: Develop a Matlab function that implements the [𝑂(ℎ2 )] two-step
Adams–Bashforth method, given be the iterative formula (𝑘 = 1, 2, 3, 4, … ),
3 1
𝑦(𝑘 + 1) = 𝑦(𝑘) + ℎ𝑓(𝑘ℎ, 𝑦(𝑘)) − ℎ𝑓((𝑘 − 1)ℎ, 𝑦(𝑘 − 1))
2 2
Employ one step of Euler’s method, starting from the specified 𝑦(0), to obtain
𝑦(1). These two initial values [𝑦(0) and 𝑦(1)] are required to initiate the Adams–
Bashforth method. Test your function for the ODE of the last example. Compare
(by graphing) the Adam-Bashforth solution to that of Euler’s method and to the
exact solution (use ℎ = 1, and 𝑡 ∈ [0 4]).
Matlab’s Function ode45

Matlab has several built-in ODE solvers. One particular solver is called ode45 and
is based on fourth- and fifth-order Runge-Kutta methods. The function can solve a
single ODE or a system of ODEs. The basic call has the syntax,

[t,y]=ode45(fun, tspan, y0)

y is the solution array where each column is one of the dependent variables, t is the
corresponding time column vector and fun is a user defined function that represents
the right-hand-side array of the ODE system, 𝑓(𝑡, 𝑦(𝑡)). Also, tspan = [ti tf]
specifies the initial and final values of the desired solution interval and y0 is a
vector containing the initial values. ode45 computes the step size ℎ adaptively and,
therefore, there is no need to specify its value in the function call.

Example. Use ode45 to solve the first-order ODE

𝑦 ′ = 30(e−𝑡 sin(𝑡) − 𝑦) + 3cos(𝑡)

for 𝑡 ∈ [0 8], with the initial condition 𝑦(0) = 0. Plot 𝑦(𝑡).

Solution.
Example. Solve the predator-prey model using ode45. Assume the initial
normalized populations 𝑥(0) = 2 and 𝑦(0) = 1. Plot 𝑥(𝑡) and 𝑦(𝑡). Also, generate
the phase-plane trajectory for 𝑦(𝑡) versus 𝑥(𝑡).

𝑑𝑥 (𝑡)
= 1.2𝑥 − 0.6𝑥𝑦
𝑑𝑡
𝑑𝑦(𝑡)
= −0.8𝑦 + 0.3𝑥𝑦
𝑑𝑡

Solution. When dealing with a system of equations, ode45 requires a function that
represents the right-hand-side of the system of ODEs as an array. Here, we have to
treat 𝑥 and 𝑦 as the elements of an array and rename them as, [𝑦(1) 𝑦(2)].

We may also have used the following equivalent but more explicit syntax,
We may generate the phase-plane trajectory plot for 𝑦(𝑡) versus 𝑥(𝑡) as follows,

The ode45 plots for the above system can be made smoother if we restrict the size of
the largest step size ℎ that the algorithm generates. This can be done employing the
“options” command. So, if we want to restrict ℎ < 0.01 we then use the instructions:

which results in the following (smooth) phase-plane trajectory


Similar smoothing effects can be achieved if we set the “options” so as to constrain
the iterative relative error (its default value is set to 10−3 ). Here is an example:

An alternative representation of the system of first-order ode’s is through the use of


a single anonymous function, as follows:
The user might want to write a single function to solve the predator-prey problem.
As an example, the user defined function (see below) predprey1(x0,y0) includes the
declaration of the system of first-order differential equation, the ode45 function call
and figure generation. Here, x0 and y0 are the initial populations.

Typing

results in
Case Study: Bungee Jumper

You are hired by an amusement park to determine the maximum weight allowed for
a bungee jumper. You were told that during the jump the jumper should be 8 meters
above ground level, at all times.

The first step was for you to refresh on the physics of a mass in motion. You
recalled the following physical laws/principles:

𝑑𝑣(𝑡)
 Newton’s second law: ∑ 𝐹⃗ = 𝑚𝑎 = 𝑚 (𝑚 is the jumper’s mass in kg
𝑑𝑡
and 𝑎 is gravitational acceleration constant in m/s2).
 Force (pointing toward earth) exerted by gravity on a falling object of mass 𝑚
is 𝐹𝑔 = 𝑚𝑔, where 𝑔 is gravitational acceleration (𝑔 ≅ 9.81m/s2).
 Force due to a stretched spring (elastic cord) of length L (Hooke’s law):
𝐹𝑐𝑜𝑟𝑑 = 𝑘(𝑥 − 𝐿), where 𝑥 > 𝐿 and 𝑘 is the spring constant. The force
direction is opposite to that of motion.
 Force due to air resistance (opposite to direction of motion): 𝐹𝑎𝑖𝑟 = 𝑐𝑑 𝑣 2 ,
where 𝑐𝑑 (kg/meter) is the drag coefficient and 𝑣 is the object’s velocity.
 Force due to damping in cord (opposite to direction of motion): 𝐹𝑑 = 𝛾𝑣,
where 𝛾 (N.s/meter) is a damping coefficient.

You also made the following initial measurements and did some research to arrive
at the following:

 The jump is from a platform 100 meters above ground level


 The rules include that the jumper starts the jump in free fall (𝑣(0) = 0)
 Bungee cord length at rest is 𝐿 = 30 m
 Drag coefficient of a falling human body in air is 𝑐𝑑 = 0.25 kg/m
 The cord’s spring constant is 𝑘 = 40 N/m
 The cord’s damping coefficient is 𝛾 = 8 N.s/m

So, by applying Newton’s second law to the falling body you obtained the equation

𝑑𝑣
𝑚 = 𝐹𝑔 − 𝐹𝑎𝑖𝑟 − 𝐹𝑐𝑜𝑟𝑑 − 𝐹𝑑
𝑑𝑡
𝑑𝑣
𝑚 = 𝑚𝑔 − 𝑠𝑖𝑔𝑛(𝑣)𝑐𝑑 𝑣 2 − 𝑘 (𝑥 − 𝐿) − 𝛾𝑣
𝑑𝑡
Dividing the above equation by 𝑚, you obtained the following first-order ODE that
models the bungee jumper’s velocity,

𝑑𝑣 𝑐𝑑 2 𝑘 𝛾
= 𝑔 − sign(𝑣) 𝑣 − (𝑥 − 𝐿) − 𝑣
𝑑𝑡 𝑚 𝑚 𝑚

After substituting the parameter values you arrived at

𝑑𝑣 0.25 2 40 8
= 9.81 − sign(𝑣) 𝑣 − (𝑥 − 30) − 𝑣 (CS1)
𝑑𝑡 𝑚 𝑚 𝑚

In this (nonlinear) ODE, positive velocity points towards Earth. Since the force due
to air resistance has a direction opposite to that of motion, and since the bungee
jumper can travel upward due to the “sling shot effect” of the cord, the sign of this
force should be opposite to that of velocity 𝑣. The sign(𝑣) function (also, built into
Matlab) returns the sign of its argument; thus automating the process of reversing
the direction of the air resistance force. We should also note that if 𝑥 < 30 (cord is
40 8
un-stretched) then the above equation should exclude the (𝑥 − 30) and 𝑣 terms
𝑚 𝑚
.

Now, the hard work is done! What is left is to employ a numerical technique to
solve the ODE. You decided to use Matlab’s ode45 function with the formulation
𝑑𝑥
𝑦1 = 𝑥 and 𝑦2 = 𝑣 = . Therefore, you represented the ODE as the system of
𝑑𝑡
equations,

𝑑𝑦1
= 𝑦2
𝑑𝑡
𝑑𝑦2 0.25 2 40 8
= 9.81 − sign(𝑦2 ) −
𝑦2 + { 𝑚 1 (𝑦 − 30 ) − 𝑦2 𝑖𝑓 𝑦1 > 30
𝑑𝑡 𝑚 𝑚
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

You then created the following Matlab function that evaluates the right-hand-side of
the above system of ODEs. This function allows the mass to be an input parameter.
You set the mass of the jumper (initially) to 100 kg.
Here is the solution you obtained for the displacement of the jumper for the settings
0 ≤ 𝑡 ≤ 100 sec, 𝑥 (0) = 0 and 𝑣 (0) = 0 (recall that the rules require the jumper to
drop in a free fall). Here, the platform is taken as the reference point (origin) for
displacement, 𝑥.

Note that, for convenience, the displacement is multiplied by −1 so that distance is


negative going down. Also, 𝑦(: ,1) is the vector with the 𝑥 values and 𝑦(: ,2) is the
vector with the 𝑣 values.

According to the above plot, if the jumper weight is 100 kg he/she will come as
close as 12 meters from ground level. You repeated the simulation using a slightly
larger mass, say 105 kg, and obtained:
The above simulation reveals that a bungee jumper of mass 105 kg will drop to
about 8 meters from ground level. Based on these results you submit your report to
the amusement park management advising for a 105 kg (about 230 lb.) limit on the
jumper’s weight.

Note that the terminal (final) position of the jumper is obtained by setting the
40
velocity and its derivative to zero in Equation (CS1), 9.81 − (𝑥 − 30) = 0,
105
or 𝑥 ≅ 55.75 meters below the platform.

Just for fun, you generated the following phase-plane trajectory plot describing the
motion of the bungee jumper,

From the plot, we see that the maximum velocity of the jumper is about 25 m/s (or
90 km/hr or 56 miles/hr), and is attained at about 45 meters below the platform.
Appendix: Symbolic Solution of ODE’s Using Matlab’s dsolve

The following is an example of Matlab’s symbolic solution of the linear second-


order ordinary differential equation
𝑑2 𝑑
3 𝑦(𝑡) + 2 𝑦(𝑡) + 𝑦(𝑡) = 0, 𝑡 ≥ 0
𝑑𝑡 2 𝑑𝑡

√2
with 𝑦(0) = 0 and 𝑦′(0) = −
6

You might also like