Octave has two built-in functions for solving differential equations. Both are based on reliable ODE solvers written in Fortran.
The function lsode
can be used Solve ODEs of the form
dx -- = f (x, t) dt
using Hindmarsh's ODE solver LSODE.
The first argument, fcn, is a string that names the function to call to compute the vector of right hand sides for the set of equations. It must have the form
xdot = f (x, t)
where xdot and x are vectors and t is a scalar.
The fourth argument is optional, and may be used to specify a set of times that the ODE solver should not integrate past. It is useful for avoiding difficulties with singularities and points where there is a discontinuity in the derivative.
Here is an example of solving a set of three differential equations using
lsode
. Given the function
function xdot = f (x, t) xdot = zeros (3,1); xdot(1) = 77.27 * (x(2) - x(1)*x(2) + x(1) \ - 8.375e-06*x(1)^2); xdot(2) = (x(3) - x(1)*x(2) - x(2)) / 77.27; xdot(3) = 0.161*(x(1) - x(3)); endfunction
and the initial condition x0 = [ 4; 1.1; 4 ]
, the set of
equations can be integrated using the command
t = linspace (0, 500, 1000); y = lsode ("f", x0, t);
If you try this, you will see that the value of the result changes dramatically between t = 0 and 5, and again around t = 305. A more efficient set of output points might be
t = [0, logspace (-1, log10(303), 150), \ logspace (log10(304), log10(500), 150)];
lsode
. Given one argument,
lsode_options
returns the value of the corresponding option. If
no arguments are supplied, the names of all the available options and
their current values are displayed.
See Alan C. Hindmarsh, ODEPACK, A Systematized Collection of ODE
Solvers, in Scientific Computing, R. S. Stepleman, editor, (1983) for
more information about the inner workings of lsode
.
The function dassl
can be used Solve DAEs of the form
0 = f (x-dot, x, t), x(t=0) = x_0, x-dot(t=0) = x-dot_0
using Petzold's DAE solver DASSL.
The first argument, fcn, is a string that names the function to call to compute the vector of residuals for the set of equations. It must have the form
res = f (x, xdot, t)
where x, xdot, and res are vectors, and t is a scalar.
The second and third arguments to dassl
specify the initial
condition of the states and their derivatives, and the fourth argument
specifies a vector of output times at which the solution is desired,
including the time corresponding to the initial condition.
The set of initial states and derivatives are not strictly required to be consistent. In practice, however, DASSL is not very good at determining a consistent set for you, so it is best if you ensure that the initial values result in the function evaluating to zero.
The fifth argument is optional, and may be used to specify a set of times that the DAE solver should not integrate past. It is useful for avoiding difficulties with singularities and points where there is a discontinuity in the derivative.
lsode
. Given one argument,
dassl_options
returns the value of the corresponding option. If
no arguments are supplied, the names of all the available options and
their current values are displayed.
See K. E. Brenan, et al., Numerical Solution of Initial-Value Problems in Differential-Algebraic Equations, North-Holland (1989) for more information about the implementation of DASSL.
Go to the first, previous, next, last section, table of contents.