Using Octave - Introduction to Octave

A. Miyoshi

A. Miyoshi

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.

- Official site instructs downloading and installing Octave for various operating systems (GNU/Linux. macOS, BSD, and Windows).

- Official Manual The number of users has been increased in the last decade, and the search on the web has become useful to find some answers.

- Octave can be invoked from a shortcut or by typing
in the command line.`octave↵`

GNU Octave, version 4.2.2 Copyright (C) 2018 John W. Eaton and others. : >>

- Octave will terminate by typing
or`exit↵`

on the prompt of Octave. (`quit↵`

`>>`

).__Underline__denotes the key-in part, and`"↵"`

denotes [Enter] key.>>

__exit↵__ - If the program happens to go into an infinite loop,
or, in the case that the calculation is taking unexpectedly long time,
press
(press [c] while pressing [Ctrl]) to terminate the execution.__[Ctrl]-c__

- Start Octave, and type a numerical expression like below.
>>

__2.3 * exp(-0.23 * 2)↵__ans = 1.4520- Like Command Prompt on Windows or shells on UNIX, the history of typing can be recalled by cursor keys [↑] and [↓] and can be edited and reexecuted.
- The last answer is stored in a variable named
*ans*and can be used for further calculations. - type
to increase the significant digits for output. It also can be specified by the number of significant digits with`format long↵`

.`output_precision(12)↵`

>>

__5 * ans↵__ans = 7.2598 >>__format long↵__>>__ans↵__ans = 7.25976192332965 >>__output_precision(12)↵__>>__ans↵__ans = 7.25976192333 - Type as below.
You will find a graph like below is created.
It is one of the important features of Octave that user can
proceed step-by-step
by checking the values of numerical expressions and shape of the functions.
>>

__x = 0:0.01:2;↵__>>__plot (x, sin(pi*x));↵__

- Of course, Octave can execute program codes
written in a file at once. Create a text file,
`mysource.m`

, with the following contents in the current directory of the running Octave.x = 1:10 sum(x)

- Then, the program is executed by typing as follows on
the Prompt of Octave.
>>

__mysource↵__x = 1 2 3 4 5 6 7 8 9 10 ans = 55 - It can be done also by using
`source`

function. The only one difference from above is that the extension of the file name should not be "`.m`

".>>

__source mysource.m↵__ - You may start the text editor from Octave prompt by using
`edit`

function.>>

__edit mysource.m↵__ - You may use your typing history in Octave for the creation of
script files. The history is stored in a file,
`~/.octave_hist`

(UNIX including WSL) or`C:\Users\`

(Windows).*user_name*\.octave_hist

- A matrix or a vector is represented by numbers in
a square bracket
`[ ]`

in which semicolon`";"`

is used as a line delimiter and a white space`" "`

or a comma`","`

is used as a column delimiter. In the following example, a linear equation system,**Ax**=**b**, is solved.>>

__A = [2, 1; 1, 3]↵__A = 2 1 1 3 >>__b = [3; 4]↵__b = 3 4 >>__x = A \ b↵__x = 1.00000 1.00000 - Various linear operations are possible, for example by
using the
`inv()`

function to calculate inverse matrix, or by using the postfix operator`'`

to denote transposition.>>

__inv(A)↵__ans = 0.60000 -0.20000 -0.20000 0.40000 >>__b'↵__ans = 3 4 >>__b' * b↵__ans = 25 >>__det(A)↵__ans = 5.0000 - The following example calculates the eigenvalues and
eigenvectores of a matrix. Resultunt eigenvalues are stored in
a matrix
`lambda`

as diagonal elements in the descending order, and eigenvectors are stored in a matrix`vec`

as vertical vectors in the same order as eigenvalues.>>

__A = [-1, 1; 1, -1]↵__A = -1 1 1 -1 >>__[vec, lambda] = eig(A)↵__vec = 0.70711 0.70711 -0.70711 0.70711 lambda = Diagonal Matrix -2 0 0 0

- LSODE is implemented in Octave as a solver for
ordinary differential equations (ODE). The example shown below
solves the following ODE system for the consecutive reactions,
A → B (1) and B → C (2).
d[A]/d
*t*= −*k*_{1}[A]

d[B]/d*t*=*k*_{1}[A] −*k*_{2}[B]

d[C]/d*t*=*k*_{2}[B]

- 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↵__ - 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)';↵__ - Obtain a numerical solution by calling
`lsode`

.>>

__xout = lsode("f", x0, t);↵__ - 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 - Plot the numerical result.
>>

__plot(t, xout)↵__

- The
`fsolve`

function is a solver for nonlinear equations in Octave. The example below solves the following nonlinear equation.*f*(*x*) = 1×10^{13}*x*^{−1.39}exp(−17921 /*x*) − 1 = 0

- 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↵__ - 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

- The following is an Octave program which solves a
reaction-diffusion equation called
*Gray-Scott model*by a simple explicit method. Download it by right-clicking, place it into the current directory, and execute it.>>

If you do not know where is the current directory, try typing__gray_scott↵__.`pwd↵`

>>

__pwd↵__ans = C:\Users\myaccount