fin500j topic 7fall 2010 olin business school 1 fin500j mathematical foundations in finance topic 7:...

45
Fin500J Topic 7 Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential Equations Philip H. Dybvig Reference: Numerical Methods for Engineers, Chapra and Canale, chapter 25, 2006 Slides designed by Yajun Wang

Upload: geraldine-underwood

Post on 23-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 1

Fin500J Mathematical Foundations in Finance

Topic 7: Numerical Methods for Solving Ordinary Differential Equations

Philip H. Dybvig

Reference: Numerical Methods for Engineers, Chapra and Canale, chapter 25, 2006

Slides designed by Yajun Wang

Page 2: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Numerical SolutionsNumerical method are used to obtain a graph

or a table of the unknown functionMost of the Numerical methods used to solve

ODE are based directly (or indirectly) on truncated Taylor series expansion

Taylor Series methodsRunge-Kutta methods

Fin500J Topic 7 Fall 2010 Olin Business School 2

Page 3: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 3

Taylor Series MethodTaylor Series MethodThe problem to be solved is a first order ODE

00 )(),,()(

yxyyxfdx

xdy

Estimates of the solution at different base points

are computed using truncated Taylor seriesexpansions

....),3(),2(),( 000 hxyhxyhxy

Page 4: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 4

Taylor Series ExpansionTaylor Series Expansion

0

0

0

00

0 ,,2

22

,00 !

...!2

)()(

Expansion SeriesTaylor Truncated

yyxx

n

nn

yyxx

yyxx dx

yd

n

h

dx

ydh

dx

dyhxyhxy

nth order Taylor series method uses nth order Truncated Taylor series expansion

Page 5: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 5

First Order Taylor Series MethodFirst Order Taylor Series Method(Euler Method)(Euler Method)

),(

),(

),(,

:

)()()(

1

,

0

2

,00

0

0

iiii

ii

yyxx

nnn

yyxx

yxfhyy

MethodEuler

yxfdx

dy

xyynhxx

Notation

hodx

dyhxyhxy

i

i

Page 6: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 6

Euler MethodEuler Method

,...2,1),(

)(

:MethodEuler

,...2,1)(Determine

)( condition initial with the

),()( ODEorder first Given the

:Problem

1

00

0

00

iforyxfhyy

xyy

iforihxyy

xyy

yxfxy

iiii

i

Page 7: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 7

Interpretation of Euler Method

x0 x1 x2 x

y0

y1

y2

Page 8: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 8

Interpretation of Euler Method

hx0 x1 x2 x

y0

y1=y0+hf(x0,y0)

Slope=f(x0,y0)

hf(x0,y0)

y1

Page 9: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 9

Interpretation of Euler Method

hx0 x1 x2 x

y0

y1=y0+hf(x0,y0)

Slope=f(x0,y0)

hf(x0,y0)y1

h

y2=y1+hf(x1,y1)Slope=f(x1,y1)

y2

hf(x1,y1)

Page 10: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 10

High Order Taylor Series methods

ly.analatical derived be toneed ,.....,,

)(!

....!2

method SeriesTaylor order n

)(),,()(

3

3

2

2

12

22

1

th

00

n

n

nn

nn

ii

dx

yd

dx

yd

dx

yd

hOdx

yd

n

h

dx

ydh

dx

dyhyy

yxyxyfdx

xdyGiven

Page 11: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 11

Runge-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

Page 12: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 12

Runge-Kutta Method

possible. as accurate as is such that

,,,

:Problem

)()(

),(

),(

Kutta RungeOrder Second

21

2211

12

1

h)x(t

wwFind

KwKwtxhtx

KxhtfhK

xtfhK

Page 13: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 13

Taylor Series in One VariableTaylor Series in One Variable

hxandxbetweenisxwhere

xfn

hxf

i

hhxf n

ni

n

i

i

)(!

)(!

)(

f(x) ofexpansion SeriesTaylor The

)()(1

0

Approximation Error

Page 14: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 14

Taylor Series in One VariableTaylor Series in One Variableanother lookanother look

hxxx

xfdx

dh

nxf

dx

dh

ihxf

f(x)

hxfdx

xfdhxf

dx

dh

nn

i

i

iii

ii

i

and between is

)(!

1)(

!

1)(

ofexpansion SeriesTaylor The

)()(

)(

Define

1

0

)(

Page 15: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 15

DefinitionsDefinitions

2

22

2

2

22

2

1

0

),(),(2

),(),(

),(),(),(

),(),(

),(

y

yxfk

yx

yxfkh

x

yxfhyxf

yk

xh

y

yxfk

x

yxfhyxf

yk

xh

yxfyxfy

kx

h

x

fhyxf

xh

Define

i

ii

i

Page 16: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 16

Taylor Series Expansion Taylor Series Expansion

)0,0(

)0.0(

1

)0.0(

0

2

),(

4),(

(0,0)at evaluated sderivative Parial

)2)(1(,

yx fkfhyxfy

kx

h

yxfy

kx

h

yxxy)f(x

Page 17: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 17

Taylor Series in Two VariablesTaylor Series in Two Variables

),( and ),(between joining line on the is),(

),(!

1),(

!

1),(

, ofexpansion SeriesTaylor The

1

0

kyhxyxyx

errorionapproximat

yxfy

kx

hn

yxfy

kx

hi

kyhxf

y)f(xnn

i

i

x x+h

y

y+k

Page 18: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 18

Runge-Kutta Method

possible. as accurate as is such that

,,,

:Problem

)()(

),(

),(

Kutta RungeOrder Second

21

2211

12

1

h)x(t

wwFind

KwKwtxhtx

KxhtfhK

xtfhK

Page 19: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 19

Runge-Kutta Method

)(),()()(

),(2

1),(

)),(,(),()()(

...)('''6

)(''2

)(')()(

possible. as many terms asmatch to

,,, :Problem

322

2221

2

21

32

21

hOffhwfhwxtfhwwtxhtx

xtfx

hft

hffhfhffhxhtf

xtfhxhtfhwxtfhwtxhtx

txh

txh

thxtxhtx

wwFind

xt

xt

Page 20: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 20

Runge-Kutta Method

)(),()()(

...)('''6

)(''2

)(')()(

322

2221

32

hOffhwfhwxtfhwwtxhtx

txh

txh

thxtxhtx

xt

1,1,5.0,5.0

solution possibleOne

5.0,5.0,1

21

2221

ww

wwww

Page 21: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 21

Runge-Kutta Method

21

12

1

2)()(

),(

),(

Form eAlternativ

FFh

txhtx

hFxhtfF

xtfF

21

12

1

2

1)()(

),(

),(

Kutta RungeOrder Second

KKtxhtx

KxhtfhK

xtfhK

Page 22: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 22

Runge-Kutta MethodAlternative Formulas

21

12

1

2

1

2

11)()(

),(

),(

)0(select Formulas Kutta RungeOrder Second

FFtxhtx

KxhtfhK

xtfhK

2

1,

2

11,

number zero-nonany Pick

solutionanother

5.0,5.0,1

21

2221

ww

wwww

Page 23: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 23

Runge-Kutta Methods

321

213

12

1

46

1)()(

)2,2

1(

)2

1,

2

1(

),(

(RK3) Kutta RungeOrder Third

KKKxyhxy

hKhKyhxfK

hKyhxfK

yxfK

ii

ii

ii

Page 24: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 24

Runge-Kutta Methods

43211

34

23

12

1

226

),(

)2

1,

2

1(

)2

1,

2

1(

),(

(RK4) Kutta RungeOrder Fourth

KKKKh

yy

hKyhxfK

hKyhxfK

hKyhxfK

yxfK

ii

ii

ii

ii

ii

Page 25: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 25

Runge-Kutta Methods

Higher order Runge-Kutta methods are available

Higher order methods are more accurate butrequire more calculations.

Fourth order is a good choice. It offers good accuracy with reasonable calculation effort

Page 26: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 26

Example 1Second Order Runge-Kutta Method

8254.3)92.1618(2

01.04

2

92.16))01.()18.0(1(),(

0.18)1(),(

:1 Step

2101

30

201002

30

20001

KKh

yy

xyhKyhxfK

xyyxfK

)02.1(),01.1(2

4)1(,1

:Problem

32

yyfindtoRKUse

yxydx

dy

4,1

1),(

0.01h

00

32

yx

xyyxf

Page 27: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 27

Example 1Second Order Runge-Kutta Method

6648.3)45.1566.16(2

01.08254.3

2

45.15))01.()1666.0(1(),(

66.16)1(),(

:2 Step

2112

31

211112

31

21111

KKh

yy

xyhKyhxfK

xyyxfK

)02.1(),01.1(2

4)1(,1

:Problem

32

yyfindtoRKUse

yxydx

dy

8254.3,01.1

1),(

0.01h

11

32

yx

xyyxf

Page 28: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 28

Example 1Summary of the solution

6648.30212

8254.30111

0000.40010

.

.

.

yxi ii

)02.1(),01.1(2

4)1(,1

:Problem

32

yyfindtoRKUse

yxydx

dy

Summary of the solution

Page 29: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 29

Solution after 100 steps

Page 30: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Numerically 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 (Dormand–Prince)

Fin500J Topic 7 Fall 2010 Olin Business School 30

Page 31: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Numerically Solving ODE in Matlab (Example 1)

Fin500J Topic 7 Fall 2010 Olin Business School 31

[1,2]. interval over the,4)1(,1

:Problem

32 yxydx

dy

Step 1: Create a M-file for dy/dx as firstode.m function yprime=firstode(x,y);

yprime=1+y^2+x^3;

Step 2: At a Matlab command window >>[x,y]=ode45(@firstode,[1,2],-4); >> [x,y] Matlab returns two column vectors, the first with values

of x and the second with value of y.

Page 32: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Numerically Solving ODE in Matlab (Example 1)

Fin500J Topic 7 Fall 2010 Olin Business School 32

1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2-4

-3

-2

-1

0

1

2

3

4

5

>> plot(x,y,'+')

Page 33: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 33

Solving 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.

Page 34: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 34

Euler method for solving a system of first order ODEsRecall Euler method for solving first order ODE.

)2),2(()2()3(

)),(()()2(

)),(()()(

:

)(),,()(

hahayfhhayhay

hahayfhhayhay

aayfhayhay

MethodEuler

yayxyfdx

xdyGiven a

Page 35: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 35

Solving 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

)2),2(()2()3(

)),(()()2(

)),(()()(

),(

),(

...

),(

),(

...

)(

1

)(

...

)(

)(

)(

2

1

2

1

2

1

hahaYFhhaYhaY

hahaYFhhaYhaY

aaYFhaYhaY

xYF

xYf

xYf

xYf

dx

yd

dx

yddx

yd

dx

xYd

vectornisY

xy

xy

xy

xY

nn

n

Page 36: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 36

Example :Euler method for solving a system of first order ODEs

39.1

78.0

)9.01(1.2.1

12.09.0

)1.0(1

)1.0(1.0

)1.0(

)1.0(

)2.0(

)2.0(

)),(()()20(

:2

2.1

9.0

)11(1.01

1.01

)0(1

)0(1.0

)0(

)0(

)1.0(

)1.0(

)0),0(()0()0(

:1

1.0

1

1

)0(

)0()0(),,(

1)('

)('

1

2

2

1

2

1

1

2

2

1

2

1

2

1

1

2

2

1

y

y

y

y

y

y

hhYFhhYhY

STEP

y

y

y

y

y

y

YFhYhY

STEP

hwithMethodEulerofstepsTwo

y

yYxYF

y

y

xy

xy

Page 37: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 37

Example :RK2 method for solving a system of first order ODEs

195.1

89.0

19.0

12.0

2.0

1.0

2

1

1

1

)1.0(

)1.0(

)21(5.0)0()0(

19.0

12.0

)1.0)0((1

2.0)0(1.0)0,1)0((2

2.0

1.0

)0(1

)0(1.0)0),0((1

:1

1.0MethodKuttaRungeorder second

1

1

)0(

)0()0(),,(

1)('

)('

2

1

1

2

1

2

2

1

1

2

2

1

y

y

KKYhY

y

yhKYFhK

y

yYFhK

STEP

hwithofstepsTwo

y

yYxYF

y

y

xy

xy

Page 38: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 38

Example :RK2 method for solving a system of first order ODEs

3780.1

7611.0

1771.0

1384.0

1890.0

1195.0

2

1

195.1

89.0

)2.0(

)2.0(

)21(5.0)1.0()1.0(

1771.0

1384.0

)1195.0)1.0((1

189.0)1.0(1.0)1.0,1)1.0((2

1890.0

1195.0

)1.0(1

)1.0(1.0)1.0),1.0((1

:2

1

1

)0(

)0()0(),,(

1)('

)('

2

1

1

2

1

2

2

1

1

2

2

1

y

y

KKYhY

y

yhKYFhK

y

yYFhK

STEP

y

yYxYF

y

y

xy

xy

Page 39: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 39

The general approach to solve high order ODE

4)0(;1)0('

16'3''

yy

yyy convert

1

4)0(

,631'

'

12

2

2

1

Z

zz

z

z

z

solve

high order ODE convert System of first order ODE solve

Second order ODE Two first order ODEs

Page 40: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 40

Conversion Procedure high order ODE convert System of first order ODE solve

1. 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.

2. 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.

3. Express the equations in matrix form

Page 41: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 41

Example of converting High order ODE to first order ODEs

'

) variables twoneed WeODEorder (second

variableofset new aSelect 1.

ODEorder first of system a to

4)0(;1)0(',16'3''Convert

2

1

yz

yz

yyyyy

One degree less than the highest order derivative

Page 42: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Fin500J Topic 7 Fall 2010 Olin Business School 42

Example of converting High order ODE to first order ODEs

1

4)0(,

631'

'

631'1'

'4

cond. name name

Equation Initial new old

12

2

2

1

1222

211

Zzz

z

z

z

zzzzy

zzzy

Page 43: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Numerically Solving high order ODE in Matlab

Fin500J Topic 7 Fall 2010 Olin Business School 43

[0,4]. interval over the,8)0(',2)0(,2sin3'''

:Example

yyxyexyy x

Step 1: First convert the second order equation to an equivalent system of first order ODEs, let z1=y, z2=y’:

8

2)0(

,2sin3'

'

21

2

2

1

Z

xzexz

z

z

zx

Page 44: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Numerically Solving high order ODE in Matlab

Fin500J Topic 7 Fall 2010 Olin Business School 44

[0,1]. interval over the,8)0(',2)0(,2sin3'''

:Example

yyxyexyy x

Step 2: Create the following M-file and save it as F.m

function zprime=F(x,z) zprime=zeros(2,1); %since output must be a column vector zprime(1)=z(2); zprime(2)=-x*z(1)+exp(x)*z(2)+3*sin(2*x);

Step 3: At Matlab prompt >> [x,z]=ode45(@F,[0,1],[2,8])

Since z1(x)=y, to print out the solution y >> [x,z(:,1)]

Page 45: Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential

Numerically Solving ODE in Matlab (Example 1)

Fin500J Topic 7 Fall 2010 Olin Business School 45

To plot y against x

>> plot(x, z(:,1))

Because the vector z has first component z1=y

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 12

4

6

8

10

12

14

16

18

20