# Octave

http://octave.org is a free software tool for numerical analysis and visualization. The function and command syntax is very similar to MATLAB.

Note that there is no SIMULINK-like tool in Octave, but there are many simulation functions (as in Control System Toolbox in MATLAB).

Extra packages for Octave are documented on https://octave.sourceforge.io/, for example the control package which contains functions for modeling, analysis, simulation, and control of dynamic systems (similar to the Control System Toolbox in MATLAB). Information about installation and loading of packages is given at the bottom of this tutorial.

## Windows in the Octave IDE (integrated development environment)

Three windows are available by default (they can be opened either via the Window menu or from tabs at the bottom of the Octave main window).

·         Command Window

·         Editor

·         Documentation

## The command window

We try the following simple commands from the command line (the command are executed by pressing Enter):

a = 1+1 The response is shown in the command window.

a = 2+2; The response is not shown in the command window (due to the semicolon).

a Shows the value of the variable a.

1+2 The last result is assigned to the inbuilt variable ans.

ans+4 The result is 7.

d = 1+2+...[Enter]
3+4 Three dots means that the command line continues.

e = 1+1, f = 2+2 One or more expressions - separated by semicolon or comma - can be written on one line.

f, F Octave separates between ordinary and capital letters. f exists (it was defined above), while F does not exist.

0.1, 1e-1, 2^3, exp(1), pi Various ways to enter numbers.

format long; 1/3, format short; 1/3 Controlling number of digits shown, but the internal representation of the number is not influenced.

Octave functions are vectorized, i.e. functions can be called with vectorial arguments and the function is applied to each of the elements of the vector:

t = [0:0.1:100]; Vector with interval 0.1 between the elements.

f = 2; %Hz Note the percent sign. It means comments.

y = sin(2*pi*f*t) y becomes a vector of same length as vector t.

## Workspace

All variables generated during the session are stored in the workspace. The workspace is cleared when you quit Octave. We try a few commands:

who Shows the contents (variables) of the workspace.

clear Clears the workspace.

who Should be nothing in the workspace due to the clear command.

## Help!

Command line help: help command. Example:

help sin

Documentation:

·         Web: http://octave.org -> docs

## Scripts

An Octave script is a user-written text file of name *.m containing any Octave expressions. When you run the script, all the expressions are execued automatically as if they were written on the command line individually.

You can edit a script using the inbuilt editor available in the Editor window (via the Eitor tab), or using any other text editor, e.g. Notepad.

You can run a script in several ways:

·         F5 button

·         Via the Run menu in the Editor window

·         By typing the script name (without the file extension m) on the command line

You should use scripts even for small tasks!

Avoid using script names that are existing variables, commands or function names!

Example:

Enter the following expressions in the document my_script_1.m:

clear;
a = 1;
b = 2;
c = a+b

Save the script in an appropriate folder.

Run the script

Just after tryiing to run a script, you may be asked by Octave to change the current directory of Octave to where the script is stored. Normally, you can accept the suggested change.

## Matrix (or array) operations

### Creating matrices. Getting matrix information:

A = [1,2;3,4] Cretaes matrix (or array) A.

r = [0:0.2:10] Row vector (or array)

c = [0:0.2:10]' Column vector (or array)

r(1) Returns the first element of r. Note that 1 (not 0) is the first index.

r(end) Returns the last element of r.

x = [1,2,3]'; y = [4,5,6]'; B = [x,y] Creates matrix B from given column vectors.

size(x) Size of x.

B(:,2) Returns column no. 2 in B.

### Special matrices:

C = eye(3) Identity matrix

D = ones(3,2) Matrix of ones

E = diag([1,2,3]) Diagonal matrix

### Matrix calculations:

Matrices can be used in matrix calculations. A few examples:

F = C+E

A*inv(A)

### Elementwise calculations:

The dot operator implements elementwise calculations, as .* and ./ and .^.

Example:

x = [1,2,3]';
y = [4,5,6]';
z = x.*y
If you omit the operator, you get an error since multiplication of two column vectors is not legal.

## Plotting

Examples:

1.      Single diagram with multiple curves:
octave_script_single_diagram_with_multiple_curves.m

2.      Multiple plots as subplots:
octave_script_multiple_plots_with_subplots.m

## Extra function packages

Extra function packages for Octave are available at

Information about listing of already installed packages and installation of new packages are given on the above web page.

As an example, open the home page of the Control package. The list of functions is opened via the function reference link on the web page.

At the start of a session, the package must be loaded into Octave with the command load package_name at the Octave command line.

## Example scripts with the Control package

The following scripts assumes that the Control package has been loaded into Octave.

1.      Simulation and calculation and plotting of poles and zeros of transfer functions:
octave_script_transfer_function_sim_poles_zeros.m

2.      Frequency response:
octave_script_frequency_response.m

24 Oct 2018. Finn Haugen (finn.haugen@usn.no)