0% found this document useful (0 votes)
106 views36 pages

Octave Tutorial GEN0001, Spring 2019

This document provides an overview and tutorial of the open-source mathematical programming environment Octave. It begins with an introduction comparing Octave to the proprietary software Matlab, noting they are both used for tasks like visualization, programming, and numerical computation. The document then covers various topics needed to use Octave, including starting and quitting the program, getting help, defining variables and data types, working with matrices, plotting, writing functions and scripts, and some examples of Octave in practice.

Uploaded by

Nourhan Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
106 views36 pages

Octave Tutorial GEN0001, Spring 2019

This document provides an overview and tutorial of the open-source mathematical programming environment Octave. It begins with an introduction comparing Octave to the proprietary software Matlab, noting they are both used for tasks like visualization, programming, and numerical computation. The document then covers various topics needed to use Octave, including starting and quitting the program, getting help, defining variables and data types, working with matrices, plotting, writing functions and scripts, and some examples of Octave in practice.

Uploaded by

Nourhan Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 36

Octave Tutorial

GEN0001, Spring 2019


Contents
 Overview
 Start, quit, getting help
 Variables and data types
 Matrices
 Plotting
 Programming
Octave
 Functions and scripts
 Misc
 Octave and Matlab in practice

Matlab
Overview
Octave is the "open-source Matlab"
Octave is a great gnuplot wrapper
 www.octave.org
 www.mathworks.com

Octave and Matlab are both, high-level languages and


mathematical programming environments for:
 Visualization
 Programming, algorithm development
 Numerical computation: linear algebra, optimization,
control, statistics, signal and image processing, etc.
Overview
Matlab-Octave comparison:
 Matlab is more flexible/advanced/powerful/costly
 Octave is for free (GPL license)
 There are minor differences in syntax

This tutorial:
 This tutorial applies to Octave *and* Matlab
unless stated otherwise!

Current versions (spring 2018):


 Octave 4.2.2
 Matlab 9.4
Contents
 Overview
 Start, quit, getting help
 Variables and data types
 Matrices
 Plotting
 Programming
 Functions and scripts
 Misc
 Octave and Matlab in practice
Octave
• Download from:
• https://ftp.gnu.org/gnu/octave/windows/octave-4.4.1-w64-64-
installer.exe
Octave Desktop

Workspace
Command
Window

Command
History

Explore the Octave Desktop


Start, Quit, Getting Help
 To start Octave from command line, type the
shell command octave, double-click Octave.app or
whatever your OS needs.
You should see the prompt:

octave:1>

 If you get into trouble, you can interrupt Octave


by typing Ctrl-C.

 To exit Octave, type quit or exit.


Start, Quit, Getting Help
 To get help, type help or doc

 To get help on a specific command (=built-in


function), type help command

 Examples: help size, help plot, help figure,


help inv, ...

 Type q to exit help mode


Contents
 Overview
 Start, quit, getting help
 Variables and data types
 Matrices
 Plotting
 Programming
 Functions and scripts
 Files I/O
 Misc
 Octave and Matlab in practice
 librobotics
Variables and Data Types
 Matrices (real and complex)
 Strings (matrices of characters)
 Structures

➛ Vectors? It's a matrix with one column/row


➛ Scalars? It's a matrix of dimension 1x1
➛ Integers? It's a double (you never have to worry)
➛ Boolean? It's an integer (non-null=true, 0=false)

Almost everything is a matrix!


Matlab has more types, e.g. OO-classes
Variables and Data Types
Creating a Matrix
 Simply type:
octave:1> A = [8, 2, 1; 3, -1, 4; 7, 6, -5]

Octave will respond with a matrix in pretty-print:


A =
8 2 1
3 -1 4
7 6 -5

➛ More on matrices, further down this tutorial.


Variables and Data Types
Creating a Character String
 Simply type:
octave:4> str = 'Hello World'

Octave can also deal with double quotes. For


compatibility reasons, use single quotes.
Variables and Data Types
Display Variables
 Simply type its name:
octave:1> a
a = 4

Suppress Output
 Add a semicolon:
octave:2> a;
octave:3> sin(phi);

Applies also to function calls.


Variables and Data Types
 Variables have no permanent type.
s = 3 followed by s = 'octave' is fine

 Use who (or the more detailed whos ) to list the


currently defined variables. Example output:
Variables in the current scope:

Attr Name Size Bytes Class


==== ==== ==== ===== =====
A 3x3 72 double
a 1x1 8 double
ans 21x1 168 double
s 1x5 5 char
v 1x21 24 double
Variables and Data Types
Numerical Precision
Variables are stored as double precision numbers in
IEEE floating point format.

 realmin Smallest positive floating point


number: 2.23e-308
 realmax Largest positive floating point
number: 1.80e+308
 eps Relative precision: 2.22e-16
Variables and Data Types
Control Display of Float Variables
 format short Fixed point format with 5
digits
 format long Fixed point format with 15
digits
 format short e Floating point format, 5 digits
 format long e Floating point format, 15
digits
 format short g Best of fixed or floating point
with 5 digits (good choice)
 format long g Best of fixed or floating point
with 15 digits
Variables and Data Types
Talking about Float Variables...
 ceil(x) Round to smallest integer
not less than x
 floor(x) Round to largest integer
not greater than x
 round(x) Round towards nearest integer
 fix(x) Round towards zero

If x is a matrix, the functions are applied to each


element of x.
Contents
 Overview
 Start, quit, getting help
 Variables and data types
 Matrices
 Plotting
 Programming
 Functions and scripts
 Misc
 Octave and Matlab in practice
 librobotics
Matrices
Creating a Matrix
 Simply type:
octave:1> A = [8, 2, 1; 3, -1, 4; 7, 6, -5]

 To delimit columns, use comma or space


 To delimit rows, use semicolon

The following expressions are equivalent:


A = [8 2 1;3 -1 4;7 6 -5]
A = [8,2,1;3,-1,4;7,6,-5]
Matrices
Creating a Matrix
 Octave will respond with a matrix in pretty-print:
A =
8 2 1
3 -1 4
7 6 -5

 Alternative Example:
octave:2> phi = pi/3;
octave:3> R = [cos(phi) -sin(phi); sin(phi) cos(phi)]
R =
0.50000 -0.86603
0.86603 0.50000
Matrices
Creating a Matrix from Matrices
octave:1> A = [1 1 1; 2 2 2]; B = [33; 33];
 Column-wise
octave:2> C = [A B]
C =
1 1 1 33
2 2 2 33

 Row-wise:
octave:3> D = [A; [44 44 44]]
D =
1 1 1
2 2 2
44 44 44
Matrices
Indexing
Always "row before column"!
 aij = A(i,j) Get an element
 r = A(i,:) Get a row
 c = A(:,j) Get a column
 B = A(i:k,j:l) Get a submatrix

 Useful indexing command end :


octave:1> data = [4 -1 35 9 11 -2];
octave:2> v = data(3:end)
v =
35 9 11 -2
Matrices
Colon ':', two meanings:
 Wildcard to select entire matrix row or column
A(3,:), B(:,5)

 Defines a range in expressions like


indices = 1:5Returns row vector 1,2,3,4,5
steps = 1:3:61 Returns row vector 1,4,7,...,61
t = 0:0.01:1 Returns vector 0,0.01,0.02,...,1

start increment stop

 Useful command to define ranges: linspace


Matrices
Assigning a Row/Column
 All referenced elements are set to the scalar value.
octave:1> A = [1 2 3 4 5; 2 2 2 2 2; 3 3 3 3 3];
octave:2> A(3,:) = -3;

Adding a Row/Column
 If the referenced row/colum doesn't exist, it's added.
octave:3> A(4,:) = 4
A =
1 2 3 4 5
2 2 2 2 2
-3 -3 -3 -3 -3
4 4 4 4 4
Matrices
Deleting a Row/Column
 Assigning an empty matrix [] deletes the
referenced rows or columns. Examples:
octave:4> A(2,:) = []
A =
1 2 3 4 5
-3 -3 -3 -3 -3
4 4 4 4 4

octave:4> A(:,1:2:5) = []
A =
2 4
2 2
-3 -3
4 4
Matrices
Get Size
 nr = size(A,1) Get number of rows of A
 nc = size(A,2) Get number of columns of
A
 [nr nc] = size(A) Get both (remember
order)
 l = length(A) Get whatever is bigger
 numel(A) Get number of elements in
A
 isempty(A) Check if A is empty matrix
[]

Octave only:
Matrices
Matrix Operations
 B = 3*A Multiply by scalar
 C = A*B + X - D Add and multiply
 B = A' Transpose A
 B = inv(A) Invert A
 s = v'*Q*v Mix vectors and matrices

 d = det(A) Determinant of A
 [v lambda] = eig(A) Eigenvalue
decomposition
 [U S V] = svd(A) Sing. value
decomposition
Matrices
Vector Operations
With x being a column vector
 s = x'*x Inner product, result is a scalar
 X = x*x' Outer product, result is a
matrix
 e = x*x Gives an error

Element-Wise Operations (for vectors/matrices)


 s = x.+x Element-wise addition
 p = x.*x Element-wise multiplication
 q = x./x Element-wise division
 e = x.^3 Element-wise power operator
Matrices
Useful Vector Functions
 sum(v) Compute sum of elements of v
 cumsum(v) Compute cumulative sum of
elements of v
 prod(v) Compute product of elements of
v
 cumprod(v) Compute cumulative product of
elements of v
 diff(v) Compute difference of
subsequent elements [v(2)-v(1) v(3)-
v(2) ...]
 mean(v) Mean value of elements in v
 std(v) Standard deviation of elements
Matrices
Useful Vector Functions
 min(v) Return smallest element in v
 max(v) Return largest element in v

 sort(v,'ascend') Sort in ascending order


 sort(v,'descend') Sort in descending order

 find(v) Return vector of indices of all


non- zero elements in v. Great in
combi- nation with vectorized
conditions. Example:
ivec = find(datavec == 5).
Matrices
Special Matrices
 A = zeros(m,n) Zero matrix of size m x n
 B = ones(m,n) Matrix of size m x n with all
1's
 I = eye(n) Identity matrix of size n
 D = diag([a b c]) Diagonal matrix of size 3 x 3
with a,b,c in the main
diagonal

Just for fun


 M = magic(n) Magic square matrix of size
n x n. (All rows and columns
sum up to the same
number)
Matrices
Random Matrices and Vectors
 R = rand(m,n) Matrix with m x n uniformly
distributed random numbers
from interval [0..1]
 N = randn(m,n) Row vector with m x n
normally
distributed random numbers
with zero mean, unit
variance
 v = randperm(n) Row vector with a random
permutation of the numbers
1 to n
Matrices
Multi-Dimensional Matrices
Matrices can have more than two dimensions.
 Create a 3-dimensional matrix by typing, e.g.,
octave:1> A = ones(2,5,2)

Octave will respond by


A =
ans(:,:,1) =
1 1 1 1 1
1 1 1 1 1
ans(:,:,2) =
1 1 1 1 1
1 1 1 1 1
Matrices
Multi-Dimensional Matrices
 All operations to create, index, add, assign,
delete and get size apply in the same fashion

Examples:
 [m n l] = size(A)
 A = rand(m,n,l)
 m = min(min(min(A)))
 aijk = A(i,j,k)
 A(:,:,5) = -3
Assessment 1
1. Create the following matrix
2. Find its size
3. Find its transpose matrix A.
4. Find the second row of A and store it in vector B.
5. Add first column in F to B (element-wise addition)

You might also like