course_notes_files/stat_890_week1_number4_ode solvers in
TRANSCRIPT
ODE solvers and Matlab functions
How they work and how you’ll make them work
© Dave Campbell 2009
Friday, June 12, 2009
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
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
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
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
dVdt
= γ V −V3 / 3+ R( )dRdt
= −1γ
βR +α −V( )Friday, June 12, 2009
Errors propagateFriday, June 12, 2009
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
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
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
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
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
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
dVdt
= γ V −V3 / 3+ R( )dRdt
= −1γ
βR +α −V( )Friday, June 12, 2009
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
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
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
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
Other solvers
Matlab has a lot of solvers, ode45 is a pretty general solver
ode15s is another useful tool
Friday, June 12, 2009
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
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
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
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
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
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
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
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
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
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
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
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
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
[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
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