GNU Octave: Matlab-compatible solvers

Up: Ordinary Differential Equations [Contents][Index]

#### 24.1.1 Matlab-compatible solvers

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:

- Runge-Kutta methods
  - `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.

: [t, y] = ode45 (fun, trange, init)
: [t, y] = ode45 (fun, trange, init, ode_opt)
: [t, y] = ode45 (…, par1, par2, …)
: [t, y, te, ye, ie] = ode45 (…)
: solution = ode45 (…)

  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 unless the integrate function specified is `integrate_n_steps`.

  By default, `ode45` uses an adaptive timestep with the `integrate_adaptive` algorithm. The tolerance for the timestep computation may be changed by using the option `"Tau"`, that has a default value of 1e-6. If the ODE option `"TimeStepSize"` is not empty, then the stepper called will be `integrate_const`. If, in addition, the option `"TimeStepNumber"` is also specified then the integrate function `integrate_n_steps` will be used.

  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 field x containing the time where the solution was evaluated and field y containing the solution matrix for the times in x. Use `fieldnames (solution)` to see the other fields and additional information returned.

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

  See also: odeset, odeget.

: [t, y] = ode23 (fun, trange, init)
: [t, y] = ode23 (fun, trange, init, ode_opt)
: [t, y] = ode23 (…, par1, par2, …)
: [t, y, te, ye, ie] = ode23 (…)
: solution = ode23 (…)

  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 unless the integrate function specified is `integrate_n_steps`.

  By default, `ode23` uses an adaptive timestep with the `integrate_adaptive` algorithm. The tolerance for the timestep computation may be changed by using the option `"Tau"`, that has a default value of 1e-6. If the ODE option `"TimeStepSize"` is not empty, then the stepper called will be `integrate_const`. If, in addition, the option `"TimeStepNumber"` is also specified then the integrate function `integrate_n_steps` will be used.

  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 field x containing the time where the solution was evaluated and field y containing the solution matrix for the times in x. Use `fieldnames (solution)` to see the other fields and additional information returned.

  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.

  This function can be called with two output arguments: t and y. Variable t is a column vector and contains the time stamps, instead y is a matrix in which each column refers to a different unknown of the problem and the rows number is the same of t rows number so that each row of y contains the values of all unknowns at the time value contained in the corresponding row in t.

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

  See also: odeset, odeget.

: odeset ()
: odestruct = odeset ("field1", value1, "field2", value2, …)
: odestruct = odeset (oldstruct, "field1", value1, "field2", value2, …)
: odestruct = odeset (oldstruct, newstruct)

  Create or modify an ODE options structure.

  When called without an input argument, return a new ODE options structure that contains all possible fields initialized to their default values.

  If called with string input arguments "field1", "field2", … identifying valid ODE options then return a new ODE options structure with all possible 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.

  See also: odeget.

: val = odeget (ode_opt, field)
: val = odeget (ode_opt, field, default)

  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.

: [ret] = odeplot (t, y, flag)

  Open a new figure window and plot the results from the variable y of type column vector over time while solving. The types and the values of the input parameter t and the output parameter ret depend on the input value flag that is of type string. If flag is

  ``"init"``

    then t must be a double column vector of length 2 with the first and the last time step and nothing is returned from this function,

  ``""``

    then t must be a double scalar specifying the actual time step and the return value is false (resp. value 0) for `"not stop solving"`,

  ``"done"``

    then t must be a double scalar specifying the last time step and nothing is returned from this function.

  This function is called by an ode solver function if it was specified in an options structure with the `odeset`. This function is an internal helper function therefore it should never be necessary that this function is called directly by a user. There is only little error detection implemented in this function file to achieve the highest performance.

  For example, solve an anonymous implementation of the `"Van der Pol"` equation and display the results while solving

      fvdb = @(t,y) [y(2); (1 - y(1)^2) * y(2) - y(1)];

      opt = odeset ("OutputFcn", @odeplot, "RelTol", 1e-6);
      sol = ode45 (fvdb, [0 20], [2 0], opt);

See also: odeset, odeget.

Up: Ordinary Differential Equations [Contents][Index]