# 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.

Lodable Function: lsode (fcn, x0, t_out, t_crit)
The first argument is the name of the function to call to compute the vector of right hand sides. It must have the form

```xdot = f (x, t)
```

where xdot and x are vectors and t is a scalar.

The second argument specifies the initial condition, and the third specifies a vector of output times at which the solution is desired, including the time corresponding to the initial condition.

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 two differential equations using `lsode`. The function

```function xdot = f (x, t)

r = 0.25;
k = 1.4;
a = 1.5;
b = 0.16;
c = 0.9;
d = 0.8;

xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);

endfunction
```

is integrated with the command

```x = lsode ("f", [1; 2], (t = linspace (0, 50, 200)'));
```

producing a set of 200 values stored in the variable x. Note that this example takes advantage of the fact that an assignment produces a value to store the values of the output times in the variable t directly in the function call The results can then be plotted using the command

```plot (t, x)
```

Lodable Function: lsode_options (opt, val)
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 this family of ODE solvers.

## Differential-Algebraic Equations

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

Loadable Function: [x, xdot] = dassl (fcn, x_0, xdot_0, t_out, t_crit)
The first argument is the name of the function to call to compute the vector of residuals. 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.