MATLAB Functions | Help Desk |
odefile
Define a differential equation problem for ODE solvers
odefile
is not a command or function. It is a help entry that describes how to create an M-file defining the system of equations to be solved. This definition is the first step in using any of MATLAB's ODE solvers. In MATLAB documentation, this M-file is referred to as odefile
, although you can give your M-file any name you like.
You can use the odefile
M-file to define a system of differential equations in one of these forms:t
and y
, although it does not have to use them. By default, the ODE file must return a column vector the same length as y
.
Only the stiff solver ode15s
can solve ode15s
and ode23s
can solve equations of the form
M-file, eliminating the need to supply time and initial value vectors at the command line (see Examples on page 2-464).
help odefile
to display the help entry.
function [out1,out2,out3] = odefile(t,y,flag,p1,p2) % ODEFILE The template for ODE files. %
if nargin < 3 | isempty(flag) % Return dy/dt = F(t,y) out1 = < Insert a function of t and/or y, p1, and p2 here >; else
switch(flag) case 'init' % Return default [tspan, y0, and options]
out1 = < Insert tspan here >; out2 = < Insert y0 here >; out3 = < Insert options = odeset(...) or [] here >;
case 'jacobian' % Return matrix J(t,y) = dF/dy out1 = < Insert Jacobian matrix here >;
case 'jpattern' % Return sparsity pattern matrix S out1 = < Insert Jacobian matrix sparsity pattern here >
case 'mass' % Return mass matrix M(t) or M out1 = < Insert mass matrix here >; case 'events' % Return event vector and info out1 = < Insert event function vector here >;
out2 = < Insert logical isterminal vector here >; out3 = < Insert direction vector here >;
otherwise error(['Unknown flag''' flag '''.']); end end
t
and y
vectors from the ODE solvers and must return a column vector the same length as y
. The optional input argument flag
determines the type of output (mass matrix, Jacobian, etc.) returned by the ODE file.
switch
statement determines the type of output required, so that the ODE file can pass the appropriate information to the solver. (See steps 4 - 9.)
'init'
) case, the ODE file returns basic information (time span, initial conditions, options) to the solver. If you omit this case, you must supply all the basic information on the command line.
'jacobian'
case, the ODE file returns a Jacobian matrix to the solver. You need only provide this case when you wish to improve the performance of the stiff solvers ode15s
and ode23s
.
'jpattern'
case, the ODE file returns the Jacobian sparsity pattern matrix to the solver. You need provide this case only when you want to generate sparse Jacobian matrices numerically for a stiff solver.
'mass
' case, the ODE file returns a mass matrix to the solver. You need provide this case only when you want to solve a system in either of the forms 'events'
case, the ODE file returns to the solver the values that it needs to perform event location. When the Events
property is set to 1
, the ODE solvers examine any elements of the event
vector for transitions to, from, or through zero. If the corresponding element of the logical isterminal
vector is set to 1
, integration will halt when a zero-crossing is detected. The elements of the direction
vector are -1
, 1
, or 0
, specifying that the corresponding event must be decreasing, increasing, or that any crossing is to be detected. See the Applying MATLAB and also the examples ballode
and orbitode
.
flag
generates an error.
function out1 = vdp1(t,y) out1 = [y(2); (1-y(1)^2)*y(2) - y(1)];defines this system of equations (with µ = 1). To solve the van der Pol system on the time interval
[0 20]
with initial values (at time 0
) of y(1) = 2
and y(2) = 0
, use:
[t,y] = ode45('vdp1',[0 20],[2; 0]); plot(t,y(:,1),'-',t,y(:,2),'-.')To specify the entire initial value problem (IVP) within the M-file, rewrite
vdp1
as follows:
functionYou can now solve the IVP without entering any arguments from the command line:[out1,out2,out3]
=
vdp1(t,y,flag) if nargin < 3 | isempty(flag) out1
=
[y(1).*(1-y(2).^2)-y(2);
y(1)]; else switch(flag) case 'init' % Return tspan, y0 and options out1 = [0 20]; out2 = [2; 0]; out3 = []; otherwise error(['Unknown request ''' flag '''.']); end end
[T,Y] = ode23('vdp1')In this example the
ode23
function looks to the vdp1
M-file to supply the missing arguments. Note that, once you've called odeset
to define options
, the calling syntax:
[T,Y] = ode23('vdp1',[],[],options)also works, and that any
options
supplied via the command line override corresponding options specified in the M-file (see odeset
).
Some example ODE files we have provided include b5ode,brussode,vdpode,orbitode,
and rigidode
. Use type
filename
from the MATLAB command line to see the coding for a specific ODE file.
The Applying MATLAB and the reference entries for the ODE solvers and their associated functions:
ode23
, ode45
, ode113
, ode15s
, ode23s
, odeget
, odeset