Section 9 Ordinary Differential Equations
Section 9 Ordinary Differential Equations
Section 9 Ordinary Differential Equations
9: ORDINARY
DIFFERENTIAL EQUATIONS
MAE 4020/5020 – Numerical Methods with MATLAB
2 Introduction
K. Webb MAE 4020/5020
Ordinary Differential Equations
3
Differential equations can be categorized as either
ordinary or partial differential equations
Ordinary differential equations (ODE’s) – functions of a
single independent variable
Partial differential equations (PDE’s) – functions of two or
more independent variables
We’ll focus on ordinary differential equations only
Note that we are not making any assumption of
linearity here
All techniques we’ll look at apply equally to linear or
nonlinear ODE’s
K. Webb MAE 4020/5020
Differential Equation Order
4
The order of a differential equation is the highest
derivative it contains
First‐order ODE’s contain only first derivatives
Second‐order ODE’s include second derivatives (possibly
first, as well), and so on …
Any ‐ order ODE can be reduced to a system of
first‐order ODE’s
Solution requires knowledge of initial or boundary
conditions
We’ll focus on techniques to solve first‐order ODE’s
Can be applied to systems of first‐order ODE’s representing
higher‐order ODE’s
K. Webb MAE 4020/5020
Initial‐Value vs. Boundary‐Value Problems
5
In this course, we’ll focus exclusively on initial‐value
problems
K. Webb MAE 4020/5020
Solving ODE’s – General Aproach
6
Have an ODE that is some function of the independent and
dependent variables:
,
Numerical solutions amounts to approximating
Starting at some known initial condition, 0 , propagate the
solution forward in time:
or
is called the increment function
Represents a slope, though not necessarily the slope at ,
is the time step:
K. Webb MAE 4020/5020
One‐Step vs. Multi‐Step Methods
7
One‐step methods
Use only information at current value of (i.e.
, or ) to determine the increment function, , to
be used to propagate the solution forward to
Collectively known as Runge‐Kutta methods
We’ll focus on these exclusively in this course
Multi‐step methods
Use both current and past values of to provide
information about the trajectory of
Improved accuracy
K. Webb MAE 4020/5020
8 Euler’s Method
We’ll first look at three specific Runge‐Kutta
algorithms, before returning to a development
of the Runge‐Kutta approach from a more
general perspective.
K. Webb MAE 4020/5020
Euler’s Method
9
Given an ODE of the form
approximate the solution, , using the formula
where the increment function is the current derivative
,
That is, assume the slope of is constant for
Use the slope at , to extrapolate to
K. Webb MAE 4020/5020
Euler’s Method
10
Euler’s method
formula:
Increment function is
the current slope:
K. Webb MAE 4020/5020
Euler’s Method ‐ Example
11
Use Euler’s method to
solve
.
5 0.5
given an initial condition
of
0 3
and a step size of
0.5
True solution is:
. .
5 ∙
K. Webb MAE 4020/5020
Euler’s Method ‐ Example
12
K. Webb MAE 4020/5020
Euler’s Method ‐ Error
13
Two types of truncation error:
Local – error due to the approximation associated with the
given method over a single time step
Global – error propagated forward from previous time steps
Total error is the sum of local and global error
Representing the solution to the ODE as a Taylor series
expansion about , the solution at is:
, , ⋯ ,
2! !
Where the remainder term is:
K. Webb MAE 4020/5020
Euler’s Method ‐ Error
14
Euler’s method is the Taylor series, truncated after the
first derivative term
For small enough , the error is dominated by the next
term in the series, so
Local error is proportional to
Analysis of the global (i.e. propagated) error is beyond
the scope of this course, but the result is that global
error is proportional to
K. Webb MAE 4020/5020
Euler’s Method – Stability
15
Euler’s method will result in error, but worse yet, it may be unstable
Unstable if errors grow without bound
Consider, for example, the following ODE:
The true solution decays exponentially to zero:
Using Euler’s method, the solution is
1
This solution will grow without bound if 1 1, i.e. if 2/
If the step size is too large, solution blows up
Euler’s method is conditionally stable
K. Webb MAE 4020/5020
Stability of Euler’s Method – Examples
16
K. Webb MAE 4020/5020
17 Heun’s Method
K. Webb MAE 4020/5020
Heun’s Method
18
Euler’s assumes a constant slope for the increment
function:
Improve accuracy of the solution by using a more
accurate slope estimate for
Heun’s method first applies Euler’s method to predict
the value of at – the predictor equation:
This value is then used to predict the slope at
K. Webb MAE 4020/5020
Heun’s Method
19
The increment function is the average of the slope
at and the slope at
The next value of is given by the corrector
equation:
K. Webb MAE 4020/5020
Heun’s Method – Summary
20
Apply Euler’s – the
predictor equation – to
predict
Calculate slope at
Compute average of the
two slopes
Use slope average to
propagate the solution
forward to – the
corrector equation
K. Webb MAE 4020/5020
Heun’s Method – Example
21
K. Webb MAE 4020/5020
Heun’s Method with Iteration
22
Predictor equation:
,
Corrector equation:
, ,
2
The corrector equation can be applied iteratively, providing a
refined estimate of
Iterate until approximate error falls below some stopping criterion
∙ 100%
K. Webb MAE 4020/5020
Iterative Heun’s Method – Algorithm
23
,
1
While
, ,
∙ 100%
1
Does not necessarily converge to the correct solution,
though will converge to a finite value
K. Webb MAE 4020/5020
Iterative Heun’s Method – Example
24
K. Webb MAE 4020/5020
25 Midpoint Method
K. Webb MAE 4020/5020
Midpoint Method
26
The slope at the
midpoint of a time
interval used as the
increment function
Provides a more
accurate estimate of
the slope across the
entire time interval
K. Webb MAE 4020/5020
Midpoint Method
27
Apply Euler’s method to
approximate at midpoint
,
2
Slope estimate at midpoint:
,
Midpoint slope estimate is
increment function
,
K. Webb MAE 4020/5020
Midpoint Method – Example
28
K. Webb MAE 4020/5020
One‐Step Methods – Error
29
Euler’s
Heun’s (w/o iter.)
Midpoint
K. Webb MAE 4020/5020
30 Runge‐Kutta Methods
K. Webb MAE 4020/5020
Runga‐Kutta Methods
31
Euler’s, Heun’s, and midpoint methods are specific
cases of the broader category of one‐step methods
known as Runge‐Kutta methods
Runge‐Kutta methods all have the same general form
The increment function has the following form
is the order of the Runge‐Kutta method
We’ll see that Euler’s is a first‐order method, while Heun’s
and midpoint are both second‐order
K. Webb MAE 4020/5020
Runge‐Kutta Methods
32
The increment function is
where
,
,
,
⋮ ⋮
, , ⋯ ,
K. Webb MAE 4020/5020
Runge‐Kutta Methods
33
An under‐determined system results
Arbitrarily set one constant and solve for others
Procedure is the same for all orders
We’ll step through the derivation of the second‐order
Runge‐Kutta formulas
K. Webb MAE 4020/5020
Second‐Order Runge‐Kutta Methods
34
Second‐order Runge‐Kutta:
(1)
where
, (2)
, (3)
Second‐order Taylor series:
,
, (4)
!
where
, (5)
K. Webb MAE 4020/5020
Second‐Order Runge‐Kutta Methods
35
Substituting (5) into (4), and recognizing that , ,
the Taylor series becomes
, , (6)
!
Next, represent (3) as a first‐order Taylor series
It’s a function of two variables, for which the first‐order
Taylor series has the following form
Δ , Δ , Δ Δ (7)
Using (7), (3) becomes
, (8)
K. Webb MAE 4020/5020
Second‐Order Runge‐Kutta Methods
36
Substituting (2) and (8) into (1)
, ,
, (9)
Now, set (9) equal to (6), the Taylor series
, , ,
, , (10)
Equating the coefficients in (10) gives three equations with four
unknowns:
1 (11)
(12)
(13)
K. Webb MAE 4020/5020
Second‐Order Runge‐Kutta Methods
37
We have three equations in four unknowns
1 (11)
(12)
(13)
An under‐determined system
An infinite number of solutions
Arbitrarily set one constant – – to a certain value
and solve for the other three constants
Different solution for each value of – a family of
solutions
K. Webb MAE 4020/5020
– Heun’s Method
38
Arbitrarily set and solve for the other constants
, , 1, 1
The second‐order Runge‐Kutta formula becomes
1 1
2 2
where
,
, ,
This is Heun’s method
, ,
2
K. Webb MAE 4020/5020
– Midpoint Method
39
Arbitrarily set and solve for the other constants
0, 1, ,
The second‐order Runge‐Kutta formula becomes
where
,
, ,
2 2
This is the midpoint method
,
K. Webb MAE 4020/5020
Fourth‐Order Runge‐Kutta
40
The most commonly used Runge‐Kutta method is the fourth‐order method
Derivation proceeds similar to that of the second‐order method
Under‐determined system – family of solutions
Most common fourth‐order Runge‐Kutta method:
1
2 2
6
where
,
1 1
,
2 2
1 1
,
2 2
The increment function is a weighted average of four different slopes
K. Webb MAE 4020/5020
4th‐Order Runge‐Kutta – Algorithm
41
1. Calculate the slope at ,
→ this is
2. Use to approximate /
from . Calculate the slope
here → this is
3. Use to re‐approx. /
from . Calculate the slope
here → this is
4. Use to approx. from .
Calculate the slope here → this
is
5. Calculate as a weighted
average of the four slopes
K. Webb MAE 4020/5020
Fourth‐Order Runge‐Kutta – Example
42
K. Webb MAE 4020/5020
43 Systems of Equations
K. Webb MAE 4020/5020
Higher‐Order Differential Equations
44
The ODE solution techniques we’ve looked at so far
pertain to first‐order ODE’s
Can be extended to higher‐order ODE’s by reducing
to systems of first‐order equations
An ‐order ODE can be represented as a system of
first‐order ODE’s
Solution method is applied to each equation at each
time step before advancing to the next time step
We’ll now revisit the fourth‐order quarter‐car
example from the first day of class
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
45
Recall the quarter‐car model from the introductory
section of this course
Apply Newton’s second law to each
mass to derive the governing fourth‐
order ODE
Single 4th‐order equation, or
Two 2nd‐order equations
0
Want to reduce to a system of four
first‐order ODE’s
Put into state‐space form
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
46
0 (1)
(2)
Reducing the ODE to a system of first‐order ODE’s is very similar to
representing our system in state‐space form:
The only difference being that we ultimately won’t actually represent
the system in matrix form
Define a state vector of displacements and velocities:
(3)
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
47
Rewrite (1) and (2) using the state variables defined
in (3)
0 (4)
(5)
The state variable representation of the system is
0 0 1 0 0
0 0 0 1 0
0 ∙ (6)
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
48
Equation (6) clearly shows our system of four first‐order
ODE’s
Alternatively, could have derived the state‐space equations
directly (e.g. using a bond graph approach)
In MATLAB, we’ll represent our system as an
n‐dimensional function
A vector of n functions:
(7)
(8)
(9)
(10)
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
49
In MATLAB, define the ‐order system of ODE’s as
shown below
An ‐dimensional function
Here, the ODE function includes parameters ( , ,
etc.) in addition to variables and
Can create an anonymous function wrapper in the calling m‐
file to allow for the passing of parameters
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
50
Basic formula remains the same
Advance the solution to the next time step using the increment
function
Now, the output is the vector of states, and the increment
function is an ‐dimensional vector
or
, , , ,…, , , , , ,…, , , ,…,
Requires only a minor modification of the code written for
first‐order ODE’s to accommodate ‐dimensional functions
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
51
Often want to pass parameters (i.e. Input arguments in addition to
and ) to the ODE function
Two options (see Section 2: Programming with MATLAB notes):
Include a varargin input argument in the ODE solver definition
Use an anonymous function wrapper for the ODE function, e.g.:
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
52
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
53
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
54
K. Webb MAE 4020/5020
55 Solving ODE’s in MATLAB
K. Webb MAE 4020/5020
MATLAB’s ODE Solvers
56
MATLAB has several ODE solvers
ode45.m should usually be first choice for non‐stiff problems
Stiff ODE’s are those with a large range of eigenvalues – i.e. both very fast
and very slow system poles
Numerical solution is difficult
From the MATLAB documentation:
Solver Stiffness Accuracy When to use
ode45 Non‐stiff Medium Most of the time. First choice.
K. Webb MAE 4020/5020
Solving ODE’s in MATLAB – ode45.m
57
[t,y] = ode45(dydt,tspan,y0,options)
dydt: handle to the ODE function – n‐dimensional
tspan: vector of initial and final times – [ti,tf]
y0: initial conditions – an n‐vector
options: structure of options created with odeset.m
t: column vector of time points
y: solution matrix – length(t) n
Syntax for all other solvers is identical
ode45 uses an adaptive algorithm that uses fourth‐ and
fifth‐order Runge‐Kutta formulas
Variable step size
K. Webb MAE 4020/5020
Fourth‐Order ODE – Example
58
K. Webb MAE 4020/5020
Passing Parameters as varargin
59
K. Webb MAE 4020/5020
Exercise – Solving ODE’s in MATLAB
60
A simple pendulum of length is described by the
following second‐order ODE
sin
This can be reduced to a system of two first‐order
ODE’s:
Exercise
sin
Define a function to describe this system of ODE’s
Write an m‐file that uses ode45.m to determine
and plot and for 0 10
0.5
10° and 175°
0
Use odeset.m to set Reltol to different values
(e.g. 10e-3 and 10e-6) and notice the effect on
the stability for 175°
K. Webb MAE 4020/5020