Solving ODEs frequently requires fine-tuning parameters, adjusting error tolerances, or passing additional information to the solver. This topic shows how to specify options, and which differential equation solvers each option is compatible with.
Use the odeset
function to create an options
structure that you then pass to the solver as the fourth input argument. For
example, to adjust the relative and absolute error tolerances:
opts = odeset('RelTol',1e-2,'AbsTol',1e-5); [t,y] = ode45(@odefun,tspan,y0,opts);
If you use the command odeset
with no inputs, then MATLAB® displays a list of the possible values for each option, with default
values indicated by curly braces {}
.
The odeget
function queries the value of
an option in an existing structure, which you can use to dynamically change option
values based on conditions. For example, this code detects whether
Stats
is set to 'on'
, and changes the
value if necessary:
if isempty(odeget(opts,'Stats')) odeset(opts,'Stats','on') end
Some options in odeset
are generic and compatible with any
solver, while others are solver-specific. This table summarizes the compatibility of
each option with the different solvers.
Option | ode45 | ode23 | ode113 | ode15s | ode23s | ode23t | ode23tb | ode15i |
---|---|---|---|---|---|---|---|---|
| ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| ✓ | ✓ | ✓ | ✓* | — | ✓* | ✓* | — |
| ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓** |
| ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| — | — | — | ✓ | ✓ | ✓ | ✓ | ✓ |
| ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
| — | — | — | ✓ | — | ✓ | — | — |
| — | — | — | ✓ | — | — | — | ✓ |
* Use the NonNegative
parameter with
ode15s
, ode23t
, and
ode23tb
only for those problems in which there is no mass
matrix.
** The events function for ode15i
must accept a third input
argument for yp
.
MATLAB includes several example files that show how to use various options.
For example, type edit ballode
to see an example that uses
'Events'
to specify an events function, or edit
batonode
to see an example that uses 'Mass'
to
specify a mass matrix. For a complete summary of example files and which options
they use, see Summary of ODE Examples and Files.