Course in Octave

Duration: Approximately 2 hours (2 *45 min).

Note: Octave is very similar to Matlab, so this course will also serve as a course in Matlab. However, the user interface in Matlab is richer than in Octave.

Exercises (in Norwegian) in the field of systems theory of dynamic systems.

About Octave

Information about Octave

Starting Octave

What is on the Start / GNU Octave menu?

We start Octave from the Start menu.

The command window with the command line

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.

a Shows the value of the variable a.

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

ans+4 The result is 7.

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, 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.

t=[0:0.1:100]; Vector with spacing 0.1.

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

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

Note: Windows Paste works in the command window using the Shift + Insert buttons.


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 Nothing in the workspace.


Command line help: help command. Example:

help sin

Functional overview:



An Octave script is a text file of name *.m containing Octave expressions. You can edit the script using any text editor, e.g. Notepad.

You run the script by typing the script name (without the file extension m) on the command line, causing all the expressions to be execued automatically as if they were written on the command line individually.

You should use scripts even for small tasks.

Do not use script names that are existing variables, commands or function names.


Enter the following expressions in the document test1.m using Notepad:





Save the script in the directory C:\Programfiler\GNU Octave 2.1.50\octave_files.

Run the script by typing test1 [Enter]

C:\Programfiler\GNU Octave 2.1.50\octave_files is the default current directory which is the directory where Octave looks for your scripts and functions (i.e., you m-files). To display the name of the current directory from within Octave, type

pwd Print working directory

Several directory commands are available:

dir Displays the contents of the current directory

mkdir('dir1') Creates a new directory of name dir1

cd dir1 Makes dir1 the current directory

Try to run test1 now! Fails, because test1.m is not in the current directory.

cd .. Moves one level up in the directory hierarchy

If your files are stored in a directory - say dir1 - different from the current directory, you can include dir1 in the search directory path of Octave by using the addpath command.

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:



Elementwise calculations:


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



Plots are generated with the plotting package Gnuplot. Plotting commands are automatically transferred to Gnuplot commands. A Gnuplot window is opened in the background.

Gnuplot may be a little ackward in the beginning, but behaves well if you do the following:

plot([]) To be executed in Octave

set term win To be executed from the Gnuplot command line. Sets terminal type to Windows.

Plotting with plot

The basic plotting command is plot. An example:

Download (to the current directory) and run the script plotintro.m.

To add information to the plot, execute the command followed by the replot command (on the Octave command line). Example:



Useful utility commands

Here are a few plot utility commands (try):

clearplot Clears plot. (Alternative: clg.)

closeplot Exits Gnuplot.

Plot editing commands and options

Here is an example showing several useful plot editing commands and options:

Download (to the current directory) and run the script plotedit.m.

Taking screen-shots of graphs

There are options of the print command for sending plots to a printer or to a graphics file (check the print command in the functions overview), but I have a little bad experience with the print command in this context (the graphics files does not look as expected ). Alternatively, you can simply press

Alt + PrntScrn

This copies the graph window to the Windows clipboard, for pasting into Windows programs, as Paint, Paint Shop Pro, Word etc. There you may add lines and text to the graph (you may alternatively add text using the text function). Then save to file or send to printer.

Function Packages

Many function packages are included in Octave, see

We browse the Control Theory package.

Example: Simulating the step response of a mass-spring-damper system (step in the applied force, and the response in the position is simulated):

The transfer function from F to y is

y(s)/F(s) = 1/(ms2+Ds+K)

Octave-script (name it e.g. mfdsim.m):






mfdsys=tf2sys(num,denum) Creates a system data structure (i.e. model) of name mfdsys. (In Matlab, you must write tf in stead of tf2sys.)

sysout(mfdsys,'tf') Displays the transfer function. (sysout is not defined in Matlab.)

step(mfdsys) Simulates and plots the unit step response. It may be wise to execute plot([]) in Octave, and execute set term win in Gnuplot before executing the step function.

September 28 2004. Finn Haugen (