# Differential Equations

Octave has two built-in functions for solving differential equations. Both are based on reliable ODE solvers written in Fortran.

## Ordinary Differential Equations

The function `lsode` can be used Solve ODEs of the form

using Hindmarsh's ODE solver LSODE.

Loadable Function: lsode (fcn, x0, t, t_crit)
Return a matrix of x as a function of t, given the initial state of the system x0. Each row in the result matrix corresponds to one of the elements in the vector t. The first element of t corresponds to the initial state x0, so that the first row of the output is x0.

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)];
```

When called with two arguments, this function allows you set options parameters for the function `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`.

## Differential-Algebraic Equations

The function `dassl` can be used Solve DAEs of the form

using Petzold's DAE solver DASSL.

Loadable Function: [x, xdot] = dassl (fcn, x0, xdot0, t, t_crit)
Return a matrix of states and their first derivatives with respect to t. Each row in the result matrices correspond to one of the elements in the vector t. The first element of t corresponds to the initial state x0 and derivative xdot0, so that the first row of the output x is x0 and the first row of the output xdot is xdot0.

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.

When called with two arguments, this function allows you set options parameters for the function `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.