460 likes | 812 Views
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. Numerical Solutions.
E N D
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 Fall 2010 Olin Business School
Numerical 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 Fall 2010 Olin Business School
Taylor 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 Fall 2010 Olin Business School
Taylor Series Expansion nth order Taylor series method uses nth order Truncated Taylor series expansion Fall 2010 Olin Business School
First Order Taylor Series Method(Euler Method) Fall 2010 Olin Business School
Euler Method Fall 2010 Olin Business School
Interpretation of Euler Method y2 y1 y0 x0 x1 x2 x Fall 2010 Olin Business School
Interpretation of Euler Method Slope=f(x0,y0) y1 y1=y0+hf(x0,y0) hf(x0,y0) y0 x0 x1 x2 x h Fall 2010 Olin Business School
Interpretation of Euler Method y2 y2=y1+hf(x1,y1) Slope=f(x1,y1) hf(x1,y1) Slope=f(x0,y0) y1=y0+hf(x0,y0) y1 hf(x0,y0) y0 x0 x1 x2 x h h Fall 2010 Olin Business School
High Order Taylor Series methods Fall 2010 Olin Business School
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 Fall 2010 Olin Business School
Runge-Kutta Method Fall 2010 Olin Business School
Taylor Series in One Variable Approximation Error Fall 2010 Olin Business School
Taylor Series in One Variableanother look Fall 2010 Olin Business School
Definitions Fall 2010 Olin Business School
Taylor Series Expansion Fall 2010 Olin Business School
Taylor Series in Two Variables y+k y x x+h Fall 2010 Olin Business School
Runge-Kutta Method Fall 2010 Olin Business School
Runge-Kutta Method Fall 2010 Olin Business School
Runge-Kutta Method Fall 2010 Olin Business School
Runge-Kutta Method Fall 2010 Olin Business School
Runge-Kutta MethodAlternative Formulas Fall 2010 Olin Business School
Runge-Kutta Methods Fall 2010 Olin Business School
Runge-Kutta Methods Fall 2010 Olin Business School
Runge-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 Fall 2010 Olin Business School
Example 1Second Order Runge-Kutta Method Fall 2010 Olin Business School
Example 1Second Order Runge-Kutta Method Fall 2010 Olin Business School
Example 1Summary of the solution Summary of the solution Fall 2010 Olin Business School
Solution after 100 steps Fall 2010 Olin Business School
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) Fall 2010 Olin Business School
Numerically Solving ODE in Matlab (Example 1) 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. Fall 2010 Olin Business School
Numerically Solving ODE in Matlab (Example 1) >> plot(x,y,'+') Fall 2010 Olin Business School
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. Fall 2010 Olin Business School
Euler method for solving a system of first order ODEs Recall Euler method for solving first order ODE. Fall 2010 Olin Business School
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 Fall 2010 Olin Business School
Example :Euler method for solving a system of first order ODEs Fall 2010 Olin Business School
Example :RK2 method for solving a system of first order ODEs Fall 2010 Olin Business School
Example :RK2 method for solving a system of first order ODEs Fall 2010 Olin Business School
The 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 Fall 2010 Olin Business School
Conversion Procedure convert solve • 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 high order ODE System of first order ODE Fall 2010 Olin Business School
Example of converting High order ODE to first order ODEs One degree less than the highest order derivative Fall 2010 Olin Business School
Example of converting High order ODE to first order ODEs Fall 2010 Olin Business School
Numerically Solving high order ODE in Matlab Step 1: First convert the second order equation to an equivalent system of first order ODEs, let z1=y, z2=y’: Fall 2010 Olin Business School
Numerically Solving high order ODE in Matlab 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)] Fall 2010 Olin Business School
Numerically Solving ODE in Matlab (Example 1) To plot y against x >> plot(x, z(:,1)) Because the vector z has first component z1=y Fall 2010 Olin Business School