Title: Fin500J Mathematical Foundations in Finance
1- Fin500J Mathematical Foundations in Finance
- Topic 7 Numerical Methods for Solving Ordinary
Differential EquationsPhilip H. Dybvig - Reference Numerical Methods for Engineers,
Chapra and Canale, chapter 25, 2006 - Slides designed by Yajun Wang
2Numerical Solutions
- Numerical method are used to obtain a graph or a
table of the unknown function - Most of the Numerical methods used to solve ODE
are based directly (or indirectly) on truncated
Taylor series expansion - Taylor Series methods
- Runge-Kutta methods
3Taylor Series Method
- The problem to be solved is a first order ODE
Estimates of the solution at different base
points are computed using truncated Taylor
series expansions
4Taylor Series Expansion
nth order Taylor series method uses nth
order Truncated Taylor series expansion
5First Order Taylor Series Method(Euler Method)
6Euler Method
7Interpretation of Euler Method
y2
y1
y0
x0 x1 x2
x
8Interpretation of Euler Method
Slopef(x0,y0)
y1
y1y0hf(x0,y0)
hf(x0,y0)
y0
x0 x1 x2
x
h
9Interpretation of Euler Method
y2y1hf(x1,y1)
y2
Slopef(x1,y1)
hf(x1,y1)
Slopef(x0,y0)
y1y0hf(x0,y0)
y1
hf(x0,y0)
y0
x0 x1 x2
x
h
h
10High Order Taylor Series methods
11Runge-Kutta Methods (Motivation)
- We seek accurate methods to solve ODE that does
not require calculating high order derivatives. - The approach is to a formula involving unknown
coefficients then determine these coefficients to
match as many terms of the Taylor series
expansion
12Runge-Kutta Method
13Taylor Series in One Variable
Approximation
Error
14Taylor Series in One Variableanother look
15Definitions
16Taylor Series Expansion
17Taylor Series in Two Variables
yk
y
x
xh
18Runge-Kutta Method
19Runge-Kutta Method
20Runge-Kutta Method
21Runge-Kutta Method
22Runge-Kutta MethodAlternative Formulas
23Runge-Kutta Methods
24Runge-Kutta Methods
25Runge-Kutta Methods
Higher order Runge-Kutta methods are
available Higher order methods are more
accurate but require more calculations. Fourth
order is a good choice. It offers good accuracy
with reasonable calculation effort
26Example 1Second Order Runge-Kutta Method
27Example 1Second Order Runge-Kutta Method
28Example 1Summary of the solution
Summary of the solution
29Solution after 100 steps
30Numerically Solving ODE in Matlab
- Matlab has a few different ODE solvers, Matlab
recommends ode45 is used as a first solver for a
problem - ode45 uses simultaneously fourth and fifth order
Runge-Kutta formula (DormandPrince)
31Numerically Solving ODE in Matlab (Example 1)
- Step 1 Create a M-file for dy/dx as firstode.m
- function yprimefirstode(x,y)
- yprime1y2x3
- Step 2 At a Matlab command window
- gtgtx,yode45(_at_firstode,1,2,-4)
- gtgt x,y
- Matlab returns two column vectors, the first with
values of x and the second with value of y.
32Numerically Solving ODE in Matlab (Example 1)
gtgt plot(x,y,'')
33Solving a system of first order ODEs
- Methods discussed earlier such as Euler,
Runge-Kutta,are used to solve first order
ordinary differential equations - The same formulas will be used to solve a system
of first order ODEs. In this case, the
differential equation is a vector equation and
the dependent variable is a vector variable.
34Euler method for solving a system of first order
ODEs
- Recall Euler method for solving first order ODE.
35Solving a system of n first order ODEs using
Euler method
- Exactly the same formula is used but the scalar
variables and functions are replaced by vector
variables and vector values functions. - Y is a vector of length n
- F(Y,x) is vector valued function
36Example Euler method for solving a system of
first order ODEs
37Example RK2 method for solving a system of
first order ODEs
38Example RK2 method for solving a system of
first order ODEs
39The general approach to solve high order ODE
convert
solve
high order ODE
System of first order ODE
convert
solve
Second order ODE
Two first order ODEs
40Conversion Procedure
convert
solve
high order ODE
System of first order ODE
- Select of dependent variables
- One way is to take the original dependent
variable and its derivatives up to one degree
less than the highest order derivative. - Write the Differential Equations in terms of the
new variables. The equations comes from the way
the new variables are defined or from the
original equation. - Express the equations in matrix form
41Example of converting High order ODE to first
order ODEs
One degree less than the highest order derivative
42Example of converting High order ODE to first
order ODEs
43Numerically Solving high order ODE in Matlab
- Step 1 First convert the second order equation
to an equivalent system of first order ODEs, let
z1y, z2y
44Numerically Solving high order ODE in Matlab
- Step 2 Create the following M-file and save it
as F.m - function zprimeF(x,z)
- zprimezeros(2,1) since output must be a
column vector - zprime(1)z(2)
- zprime(2)-xz(1)exp(x)z(2)3sin(2x)
- Step 3 At Matlab prompt
- gtgt x,zode45(_at_F,0,1,2,8)
- Since z1(x)y, to print out the solution y
- gtgt x,z(,1)
-
45Numerically Solving ODE in Matlab (Example 1)
To plot y against x gtgt plot(x, z(,1))
Because the vector z has first component
z1y