Using Octave - Introduction to Octave
A. Miyoshi

Using Octave - Introduction to Octave

GNU Octave is a high-level programming language (freeware) for scientific computation developed by John W. Eaton and others.   This instruction is based on the Octave 4.2.2 as of March 2018.   Please understand that some information may not be up-to-date due to the improvement and/or update.

Obtaining and Installing Octave

Manual

Starting and Quitting

Interactive Usage

Execution of Scripst

Matrices and Vectors

Differential Equations

  1. Define the ODE sytem to be solved as a function f.
    >> function xdot = f(x, t)↵
      xdot(1) = -5 * x(1);↵
      xdot(2) =  5 * x(1) - 1 * x(2);↵
      xdot(3) =             1 * x(2);↵
    endfunction↵
  2. Set the initial condition, x0, and the output times, t.   linspace is a function which generates a sequence of numbers with a constant interval as a row vector.
    >> x0 = [1; 0; 0];↵
    >> t = linspace(0, 3, 31)';↵
  3. Obtain a numerical solution by calling lsode.
    >> xout = lsode("f", x0, t);↵
  4. Output the result with time, t, appended as the first column.   format is a function to define output format.   [t, xout] is a combined matrix of column vector t and the matrix, xout, with the same number of rows.
    >> format short;↵
    >> [t, xout]↵
    ans =
       0.00000   1.00000   0.00000   0.00000
       0.10000   0.60653   0.37288   0.02059
         :         :         :         :
       3.00000   0.00000   0.06223   0.93777
  5. Plot the numerical result.  
    >> plot(t, xout)↵

Nonlinear Equations

  1. Define a function f so that the nonlinear equation to be solved is f(x) = 0.
    >> function y = f(x)↵
    >   y = 1e13 * x^(-1.39) * exp(-17921/x) - 1;↵
    > endfunction↵
  2. Obtain the solution by calling fsolve with the initial guess of the solution.
    >> [x, fval, info] = fsolve("f", 800)↵
    x =  873.34
    fval = 9.4820e-007
    info =  1

Example : Gray-Scott model