Octave also provides a set of solvers for initial value problems for Ordinary Differential Equations that have a MATLAB-compatible interface. The options for this class of methods are set using the functions.
odeset
odeget
Currently implemented solvers are:
ode45
Integrates a system of non–stiff ordinary differential equations
(non–stiff ODEs and DAEs) using second order Dormand-Prince
method. This is a fourth–order accurate integrator therefore the local
error normally expected is O(h^5). This solver requires six
function evaluations per integration step.
ode23
Integrates a system of non–stiff ordinary differential equations
(non-stiff ODEs and DAEs) using second order Bogacki-Shampine
method. This is a second-order accurate integrator therefore the local
error normally expected is O(h^3). This solver requires three
function evaluations per integration step.
Solve a set of non-stiff Ordinary Differential Equations (non-stiff ODEs) with the well known explicit Dormand-Prince method of order 4.
fun is a function handle, inline function, or string containing the
name of the function that defines the ODE: y' = f(t,y)
. The function
must accept two inputs where the first is time t and the second is a
column vector of unknowns y.
trange specifies the time interval over which the ODE will be
evaluated. Typically, it is a two-element vector specifying the initial and
final times ([tinit, tfinal]
). If there are more than two elements
then the solution will also be evaluated at these intermediate time
instances.
By default, ode45
uses an adaptive timestep with the
integrate_adaptive
algorithm. The tolerance for the timestep
computation may be changed by using the options "RelTol"
and "AbsTol"
.
init contains the initial value for the unknowns. If it is a row vector then the solution y will be a matrix in which each column is the solution for the corresponding initial value in init.
The optional fourth argument ode_opt specifies non-default options to
the ODE solver. It is a structure generated by odeset
.
The function typically returns two outputs. Variable t is a column vector and contains the times where the solution was found. The output y is a matrix in which each column refers to a different unknown of the problem and each row corresponds to a time in t.
The output can also be returned as a structure solution which
has a field x containing a row vector of times where the solution
was evaluated and a field y containing the solution matrix such
that each column corresponds to a time in x.
Use fieldnames (solution)
to see the other fields and
additional information returned.
If no output arguments are requested, and no OutputFcn
is
specified in ode_opt, then the OutputFcn
is set to
odeplot
and the results of the solver are plotted immediately.
If using the "Events"
option then three additional outputs may
be returned. te holds the time when an Event function returned a
zero. ye holds the value of the solution at time te. ie
contains an index indicating which Event function was triggered in the case
of multiple Event functions.
Example: Solve the Van der Pol equation
fvdp = @(t,y) [y(2); (1 - y(1)^2) * y(2) - y(1)]; [t,y] = ode45 (fvdp, [0, 20], [2, 0]);
Solve a set of non-stiff Ordinary Differential Equations (non-stiff ODEs) with the well known explicit Bogacki-Shampine method of order 3. For the definition of this method see http://en.wikipedia.org/wiki/List_of_Runge%E2%80%93Kutta_methods.
fun is a function handle, inline function, or string containing the
name of the function that defines the ODE: y' = f(t,y)
. The function
must accept two inputs where the first is time t and the second is a
column vector of unknowns y.
trange specifies the time interval over which the ODE will be
evaluated. Typically, it is a two-element vector specifying the initial and
final times ([tinit, tfinal]
). If there are more than two elements
then the solution will also be evaluated at these intermediate time
instances.
By default, ode23
uses an adaptive timestep with the
integrate_adaptive
algorithm. The tolerance for the timestep
computation may be changed by using the options "RelTol"
and "AbsTol"
.
init contains the initial value for the unknowns. If it is a row vector then the solution y will be a matrix in which each column is the solution for the corresponding initial value in init.
The optional fourth argument ode_opt specifies non-default options to
the ODE solver. It is a structure generated by odeset
.
The function typically returns two outputs. Variable t is a column vector and contains the times where the solution was found. The output y is a matrix in which each column refers to a different unknown of the problem and each row corresponds to a time in t.
The output can also be returned as a structure solution which
has a field x containing a row vector of times where the solution
was evaluated and a field y containing the solution matrix such
that each column corresponds to a time in x.
Use fieldnames (solution)
to see the other fields and
additional information returned.
If no output arguments are requested, and no OutputFcn
is
specified in ode_opt, then the OutputFcn
is set to
odeplot
and the results of the solver are plotted immediately.
If using the "Events"
option then three additional outputs may
be returned. te holds the time when an Event function returned a
zero. ye holds the value of the solution at time te. ie
contains an index indicating which Event function was triggered in the case
of multiple Event functions.
Example: Solve the Van der Pol equation
fvdp = @(t,y) [y(2); (1 - y(1)^2) * y(2) - y(1)]; [t,y] = ode23 (fvdp, [0, 20], [2, 0]);
Create or modify an ODE options structure.
When called with no input argument and one output argument, return a new ODE options structure that contains all possible fields initialized to their default values. If no output argument is requested, display a list of the common ODE solver options along with their default value.
If called with name-value input argument pairs "field1", "value1", "field2", "value2", … return a new ODE options structure with all the most common option fields initialized, and set the values of the fields "field1", "field2", … to the values value1, value2, ….
If called with an input structure oldstruct then overwrite the values of the options "field1", "field2", … with new values value1, value2, … and return the modified structure.
When called with two input ODE options structures oldstruct and newstruct overwrite all values from the structure oldstruct with new values from the structure newstruct. Empty values in newstruct will not overwrite values in oldstruct.
The most commonly used ODE options, which are always assigned a value
by odeset
, are the following:
Absolute error tolerance.
Use BDF formulas in implicit multistep methods. Note: This option is not yet implemented.
Event function. An event function must have the form
[value, isterminal, direction] = my_events_f (t, y)
Consistent initial slope vector for DAE solvers.
Initial time step size.
Jacobian matrix, specified as a constant matrix or a function of time and state.
Specify whether the Jacobian is a constant matrix or depends on the state.
If the Jacobian matrix is sparse and non-constant but maintains a constant sparsity pattern, specify the sparsity pattern.
Mass matrix, specified as a constant matrix or a function of time and state.
Specify whether the mass matrix is singular. Accepted values include
"yes"
, "no"
, "maybe"
.
Maximum order of formula.
Maximum time step value.
Specify whether the mass matrix depends on the state or only on time.
If the mass matrix is sparse and non-constant but maintains a constant sparsity pattern, specify the sparsity pattern. Note: This option is not yet implemented.
Specify elements of the state vector that are expected to remain nonnegative during the simulation.
Control error relative to the 2-norm of the solution, rather than its absolute value.
Function to monitor the state during the simulation. For the form of
the function to use see odeplot
.
Indices of elements of the state vector to be passed to the output monitoring function.
Specify whether output should be returned only at the end of each time step or also at intermediate time instances. The value should be a scalar indicating the number of equally spaced time points to use within each timestep at which to return output. Note: This option is not yet implemented.
Relative error tolerance.
Print solver statistics after simulation.
Specify whether odefun
can be passed multiple values of the
state at once.
Field names that are not in the above list are also accepted and added to the result structure.
See also: odeget.
Query the value of the property field in the ODE options structure ode_opt.
If called with two input arguments and the first input argument ode_opt is an ODE option structure and the second input argument field is a string specifying an option name, then return the option value val corresponding to field from ode_opt.
If called with an optional third input argument, and field is not set in the structure ode_opt, then return the default value default instead.
See also: odeset.
Open a new figure window and plot the solution of an ode problem at each time step during the integration.
The types and values of the input parameters t and y depend on the input flag that is of type string. Valid values of flag are:
"init"
The input t must be a column vector of length 2 with the first and
last time step ([tfirst tlast]
. The input y
contains the initial conditions for the ode problem (y0).
""
The input t must be a scalar double specifying the time for which the solution in input y was calculated.
"done"
The inputs should be empty, but are ignored if they are present.
odeplot
always returns false, i.e., don’t stop the ode solver.
Example: solve an anonymous implementation of the
"Van der Pol"
equation and display the results while
solving.
fvdp = @(t,y) [y(2); (1 - y(1)^2) * y(2) - y(1)]; opt = odeset ("OutputFcn", @odeplot, "RelTol", 1e-6); sol = ode45 (fvdp, [0 20], [2 0], opt);
Background Information:
This function is called by an ode solver function if it was specified in
the "OutputFcn"
property of an options structure created with
odeset
. The ode solver will initially call the function with the
syntax odeplot ([tfirst, tlast], y0, "init")
. The
function initializes internal variables, creates a new figure window, and
sets the x limits of the plot. Subsequently, at each time step during the
integration the ode solver calls odeplot (t, y, [])
.
At the end of the solution the ode solver calls
odeplot ([], [], "done")
so that odeplot can perform any clean-up
actions required.