course_notes_files/stat_890_week1_number4_ode solvers in

34
ODE solvers and Matlab functions How they work and how you’ll make them work © Dave Campbell 2009 Friday, June 12, 2009

Upload: phamngoc

Post on 11-Feb-2017

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ODE solvers and Matlab functions

How they work and how you’ll make them work

© Dave Campbell 2009

Friday, June 12, 2009

Page 2: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ODE functionsCreating functions

specifying initial conditions

How a solver works Runga-Kutta

specifying options

Running one in Matlab

an example

What are the problems?

ODE45 vs ODE15s what is a stiff system?Friday, June 12, 2009

Page 3: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Approximating ODE solutions

From the definition of a derivative

Or it’s intuition as change in height over change in time

We can approximate an ODE solution

Friday, June 12, 2009

Page 4: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Approximating ODE solutions

From the definition of a derivative

Or it’s intuition as change in height over change in time

We can approximate an ODE solution

df (t)dt

= limΔt→0

f (t + Δt) − f (t − Δt)2Δt

Friday, June 12, 2009

Page 5: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Euler approximation

x=0:.1:3; A(1)=6;for lp=2:length(x);

dAdt=-4*A(lp-1)+4;A(lp)=dAdt*.1+A(lp-1);

endFriday, June 12, 2009

Page 6: Course_Notes_files/stat_890_week1_number4_ODE solvers in

dVdt

= γ V −V3 / 3+ R( )dRdt

= −1γ

βR +α −V( )Friday, June 12, 2009

Page 7: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Errors propagateFriday, June 12, 2009

Page 8: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Given the value of a function at xo we can approximate the function at some other point x with a Taylor approximation:

f (x) ≈ f (x0 ) + ′f (x0 )(x − x0 ) +f ''(x0 )(x − x0 )

2

2!+f '''(x0 )(x − x0 )

3

3!

f (x) = f (n) (x0 )n!

(x − x0 )n=0

Friday, June 12, 2009

Page 9: Course_Notes_files/stat_890_week1_number4_ODE solvers in

For an ODE model we already have the derivative information f’ and if we know the initial conditions xo we can approximate the function at the point x

f (x) ≈ f (x0 ) + ′f (x0 )(x − x0 ) +f ''(x0 )(x − x0 )

2

2!+f '''(x0 )(x − x0 )

3

3!

Friday, June 12, 2009

Page 10: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Runge Kutta methods take this idea in a slightly smarter direction

Instead of depending on derivatives of the ODE model they just use extra evaluations of the ODE model

Runge-Kutta methods use a weighted average of the function to integrate evaluated at different points along the time step

Friday, June 12, 2009

Page 11: Course_Notes_files/stat_890_week1_number4_ODE solvers in

A Runge-Kutta a-b method uses the ath order approximation to the equation you want to integrate and limits the propagating errors on the order of (time step)b.

Runge-Kutta 45 k1 = Δtf tn , yn( )k2 = Δtf tn + .5Δt, yn + .5k1( )k3 = Δtf tn + .5Δt, yn + .5k2( )k4 = Δtf tn + .5Δt, yn + .5k3( )yn+1 = yn +

k16+k23+k33+k46+O(Δt 5 )

Friday, June 12, 2009

Page 12: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Matlab has Runge-Kutta solvers that use adaptive time steps to ensure that the approximation is pretty good

runge-kutta 45: [t,y]=ode45(function,time,y0,options,ODEpars)

time is the interval or points over which you want to evaluate your function

y0 is a vector of initial conditions

Friday, June 12, 2009

Page 13: Course_Notes_files/stat_890_week1_number4_ODE solvers in

runge-kutta 45: [t,y]=ode45(function,time,y0,options,ODEpars)

ODEpars are anything you want to send to your ODE function

options can be set using options = odeset('RelTol',1e-12);

function is your ODE as a function handle...

Friday, June 12, 2009

Page 14: Course_Notes_files/stat_890_week1_number4_ODE solvers in

dVdt

= γ V −V3 / 3+ R( )dRdt

= −1γ

βR +α −V( )Friday, June 12, 2009

Page 15: Course_Notes_files/stat_890_week1_number4_ODE solvers in

function r = fhnfunode(t,y,p)%%%%%%%%%%%%%%%%%%%%% The FitzHugh-Nagumo equations in scalar form%%%%%%%%%%%%%%%%%%%% r = y;r(1) = p(3)*(y(1) - y(1).^3/3 + y(2));r(2) = -(y(1) -p(1) + p(2)*y(2))/p(3); end

Matlab functions

dVdt

= γ V −V3 / 3+ R( )dRdt

= −1γ

βR +α −V( )

Friday, June 12, 2009

Page 16: Course_Notes_files/stat_890_week1_number4_ODE solvers in

function r = fhnfunode(t,y,p)%%%%%%%%%%%%%%%%%%%%% The FitzHugh-Nagumo equations in scalar form%%%%%%%%%%%%%%%%%%%% r = y;r(1) = p(3)*(y(1) - y(1).^3/3 + y(2));r(2) = -(y(1) -p(1) + p(2)*y(2))/p(3); end

Matlab functionsfunction is named here

inputs must be t,y, then whatever you want

commented descriptiongiven when you typehelp fhnfunode

Defines the start of a function

function outputs are defined

function ends or just stop typing (optional)Friday, June 12, 2009

Page 17: Course_Notes_files/stat_890_week1_number4_ODE solvers in

function r = fhnfunode(t,y,p)%%%%%%%%%%%%%%%%%%%%% The FitzHugh-Nagumo equations in scalar form%%%%%%%%%%%%%%%%%%%% r = y;r(1) = p(3)*(y(1) - y(1).^3/3 + y(2));r(2) = -(y(1) -p(1) + p(2)*y(2))/p(3);

end

Matlab functionsyour function must be saved as fhnfunode.m to be found

save it in the search pathFriday, June 12, 2009

Page 18: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Let ode45 know you want to use this function by giving it the function handle: @fhnfunode

@tells matlab that it is the name of a function

Friday, June 12, 2009

Page 19: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Other solvers

Matlab has a lot of solvers, ode45 is a pretty general solver

ode15s is another useful tool

Friday, June 12, 2009

Page 20: Course_Notes_files/stat_890_week1_number4_ODE solvers in

Stiff equations

Basically ‘stiff’ equations are ones that change rapidly and force a solver to take a lot of tiny time steps so that it will take a long long long time to get a numerical solver.

Friday, June 12, 2009

Page 21: Course_Notes_files/stat_890_week1_number4_ODE solvers in

dy/dt=ßy is a model for unbounded exponential growth.

The derivative and y(t) grow rapidly so that the step size required by ode45 has to shrink to un-useable speed

Also when your equations model a system of exponential decaying things, one fast and one slow they require different time steps.

Friday, June 12, 2009

Page 22: Course_Notes_files/stat_890_week1_number4_ODE solvers in

try a stiff solver like ode15s

For reliability and accuracy of a stiff system, the solver needs the Jacobian matrix of derivatives of functions with respect to system outputs: J=df/dy

Matlab will numerically approximate this if you do not supply one.

try help ode45 for info on all solvers

Friday, June 12, 2009

Page 23: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair. It is a one-step solver – in computing y(tn), it needs only the solution at the immediately preceding time point, y(tn-1). In general, ode45 is the best function to apply as a first try for most problems.

*from Matlab help files

Friday, June 12, 2009

Page 24: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ode23 is an implementation of an explicit Runge-Kutta (2,3) pair of Bogacki and Shampine. It may be more efficient than ode45 at crude tolerances and in the presence of moderate stiffness. Like ode45, ode23 is a one-step solver.

*from Matlab help files

Friday, June 12, 2009

Page 25: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ode113 is a variable order Adams-Bashforth-Moulton PECE solver. It may be more efficient than ode45 at stringent tolerances and when the ODE file function is particularly expensive to evaluate. ode113 is a multistep solver — it normally needs the solutions at several preceding time points to compute the current solution.

*from Matlab help files

Friday, June 12, 2009

Page 26: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ode15s is a variable order solver based on the numerical differentiation formulas (NDFs). Optionally, it uses the backward differentiation formulas that are usually less efficient. Like ode113, ode15s is a multistep solver.

*from Matlab help files

Friday, June 12, 2009

Page 27: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ode23s is based on a modified Rosenbrock formula of order 2. Because it is a one-step solver, it may be more efficient than ode15s at crude tolerances. It can solve some kinds of stiff problems for which ode15s is not effective.

*from Matlab help files

Friday, June 12, 2009

Page 28: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ode23t is an implementation of the trapezoidal rule using a "free" interpolant. Use this solver if the problem is only moderately stiff and you need a solution without numerical damping. ode23t can solve DAEs.

*from Matlab help files

Friday, June 12, 2009

Page 29: Course_Notes_files/stat_890_week1_number4_ODE solvers in

ode23tb is an implementation of TR-BDF2, an implicit Runge-Kutta formula with a first stage that is a trapezoidal rule step and a second stage that is a backward differentiation formula of order two. By construction, the same iteration matrix is used in evaluating both stages. Like ode23s, this solver may be more efficient than ode15s at crude tolerances.

Friday, June 12, 2009

Page 30: Course_Notes_files/stat_890_week1_number4_ODE solvers in

I start with ode45 and if I’m integrating something very sharp or suspect that ode45 won’t work I use ode15s

Friday, June 12, 2009

Page 31: Course_Notes_files/stat_890_week1_number4_ODE solvers in

A & C have the same derivative, they are just shifted versions of each other

We could re-write the system as a Differential Algebraic System like C=A+ß

ode113 and ode15s will also work well for a DAEFriday, June 12, 2009

Page 32: Course_Notes_files/stat_890_week1_number4_ODE solvers in

A & C have the same derivative, they are just shifted versions of each other

We could re-write the system as a Differential Algebraic System like C=A+ß

ode113 and ode15s will also work well for a DAE

dAdt

= −kpCA +kpKa

LW⎛⎝⎜

⎞⎠⎟10−3

dCdt

= −kpCA +kpKa

LW⎛⎝⎜

⎞⎠⎟10−3

dLdt

= kpCA −kpKa

LW⎛⎝⎜

⎞⎠⎟10−3

dWdt

= kpCA −kpKa

LW⎛⎝⎜

⎞⎠⎟10−3 − 24.3 W −Weq⎡⎣ ⎤⎦

Friday, June 12, 2009

Page 33: Course_Notes_files/stat_890_week1_number4_ODE solvers in

[t,y]=ode45(function, time, y0, options, ODEpars)

[t,y]=ode45(@fhnfunode,[0,20],[-1,1],[],[.2,.2,3])

omitting the outputs will just plot the functionFriday, June 12, 2009

Page 34: Course_Notes_files/stat_890_week1_number4_ODE solvers in

p3=[.25,.5,1:10]; for lp=1:length(p3); [t,y(:,lp)]=ode45(@fhnfunode,[0,20],[-1,1],[],… [.2,.2,p3(lp)]); end y=y(:,1:end-1); surf(p3,t,y)

Friday, June 12, 2009