Integrating Differential Equations (GNU Octave (version 11.1.0))

#### 1.2.5 Integrating Differential Equations ¶

Octave has built-in functions for solving nonlinear differential equations of the form

    dx
    -- = f (x, t)
    dt

with the initial condition

    x(t = t0) = x0

For Octave to integrate equations of this form, you must first provide a definition of the function `f(x,t)`. This is straightforward, and may be accomplished by entering the function body directly on the command line. For example, the following commands define the right-hand side function for an interesting pair of nonlinear differential equations. Note that while you are entering a function, Octave responds with a different prompt, to indicate that it is waiting for you to complete your input.

    octave:1> 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

Given the initial condition

    octave:2> x0 = [1; 2];

and the set of output times as a column vector (note that the first output time corresponds to the initial condition given above)

    octave:3> t = linspace (0, 50, 200)';

it is easy to integrate the set of differential equations:

    octave:4> x = lsode ("f", x0, t);

The function `lsode` uses the Livermore Solver for Ordinary Differential Equations, described in A. C. Hindmarsh, "ODEPACK, a Systematized Collection of ODE Solvers", Scientific Computing, R. S. Stepleman et al. (Eds.), North-Holland, Amsterdam, 1983, pp. 55–64.