Chapter2 - M-Review of Octave
Chapter2 - M-Review of Octave
Department
Introduction
− Octave is a simple language developed as a quick tool to perform numerical tasks.
− It is to a large extent compatible with Matlab, which is very popular among engineers.
− Octave can be considered as the "open-source Matlab"
− There are minor differences in syntax
Some common tasks in the scientific community that Octave can perform are:
− Simple algebra
− Trigonometry
− Linear algebra
− Differential equations
− Numerical integration
− Statistics
− Optimization
− Image processing
− Visualization
It is always strongly recommended to install and use the most recent version of GNU Octave from:
https://octave.org/download
You can find an extensive manual for Octave at
http://www.gnu.org/software/octave/doc/interpreter/
1
1. Octave Graphical User Interface (GUI)
2
2. Some basic commands and functions
− To start Octave, double-click Octave
− In case of problems, interrupt Octave by typing Ctrl-C.
− Quit or exit
To exit Octave
− help
Display the help text for NAME.
− Doc + FUNCTION_NAME
Display documentation for the function FUNCTION_NAME
− lookfor + Keyword
Search for the string “Keyword” in the documentation of all functions.
− clc
Clear the command window
− ans
The most recently computed result.
− who
List currently defined variables matching the given patterns.
− whos
Provide detailed information on currently defined variables matching the given patterns.
Clear
− disp (x), disp “Hello World!”
display value of x or an input text
− ...
Type … (3 dots) to continue a line in the next line
− , or ;
Use commas (,) or semicolons (;) to enter more than one statement at once.
Not that the semicolons (;) prevents printing the variable before it in the output
Example
3
3. Basic operations in Octave
a. Simple algebra (calculations)
Octave Arithmetic Operators for addition, subtraction, multiplication, division, and exponentiation.
The arithmetic operators symbols are as follows:
Table 1: Octave Arithmetic Operators.
Example (1 + 2 x 3)
>> 1+2*3
ans =
7
>> x = 1+2*3
x=7
>> t = 5;
>> t = t+1
t=6
o Complex numbers are the sum of a real number and an imaginary number, and the numbers
which are not real numbers are called imaginary numbers (i.e. √−1), Complex numbers are stored
by the variables i, j, I and J in Octave.
4
Examples
>> x = i;
x = 0 + 1i
>> y = 3+4*i;
y = 3 + 4i
2. Matrices
Vectors
An array of dimension 1 × 𝑛 (single-row matrix) is called a row vector
An array of dimension 𝑚 × 1 (single-column matrix) is called a column vector.
The elements of vectors are enclosed by square brackets [] and are separated by spaces or by commas.
Examples
Definition of a row vector
>> v = [1 4 7 10 13]
v=
1 4 7 10 13
>> v = [1,4,7,10,13]
v=
1 4 7 10 13
Column vectors are created in a similar way, however, semicolon (;) must separate the components of a column
vector.
Examples
Transpose
On the other hand, a row vector is converted to a column vector using the transpose operator.
The transpose operation is denoted by an apostrophe or a single quote (').
5
Examples
>> >> z = w’
z=
1 4 7 10 13
Or, all elements from the third through the last elements
>> z(3:end)
ans =
7 10 13
where end signifies the last element in the vector.
If z is a vector, writing z(:) produces a column vector from all elements in z,
>> z(:)
ans =
1
4
7
10
13
>> z(1:end)
ans =
1 4 7 10 13
6
Matrices
Example
Define the following matrix:
1 2 3
𝐴 = [4 5 6]
7 8 9
>> A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
We can then access a particular element in a matrix by specifying its location. We write,
>> A(2,1)
ans =
4
A(2,1) is an element located in the second row and first column. Its value is 4.
>> x = 0:1:5
x=
0 1 2 3 4 5
− Linear spacing
y = linspace(a,b)
This generates a row vector y of 100 points (default number) linearly spaced between and including a and b, while
y = linspace(a,b,n)
a row vector y of n points linearly spaced between and including a and b.
7
Example
>> theta = linspace(0,2*pi,101)
divides the interval [0; 2𝜋] into 100 equal subintervals, then creating a vector of 101 elements.
Colon operator in a matrix
The statement A(m:n,k:l) specifies rows m to n and column k to l from the matrix A.
To pick up a specific row (here the second):
>> A(2,:)
ans =
456
>> A(:,3)
ans =
3
6
9
Generally, a colon (:) as the row or column specifier covers all entries in that row or column; thus
>> A(:,2:3)
ans =
2 3
5 6
8 0
>> A(:,2)=[]
A=
1 3
4 6
7 9
8
Extending the matrix
Exercise: Add the deleted column in a matrix C using matrix operations so that C is equal to the original matrix
A
C= 1 2 3
4 5 6
7 8 9
Horizontal Appending
>> A = [1 2; 3 4]
>> B = [5 6; 7 8]
>> C = [A, B]
C=
1 2 5 6
3 4 7 8
Vertical Appending
>> A = [1 2; 3 4]
>> B = [5 6; 7 8]
>> C = [A; B]
C=
1 2
3 4
5 6
7 8
9
Examples
>> b=ones(3,1)
b=
1
1
1
Equivalently, we can define b as b=[1;1;1]
>> b =eye(3)
b=
1 0 0
0 1 0
0 0 1
>> c =zeros(2,3)
c=
0 0 0
0 0 0
>> a=rand(3,1)
a=
0.16028
0.80442
0.79403
10
Matrix Dimensions
Size
To determine the dimensions of a matrix or vector, use the command size. For example,
>> A=[1 2 3; 4 5 6; 7 8 9]
>> size(A)
ans =
3 3
Length (A)
The length is 0 for empty objects, 1 for scalars, and the number of elements for vectors.
For matrix or N-dimensional objects, the length is the number of elements along the largest dimension which is
equivalent to max(size (A)).
>> sc = sum(A)
sc =
5 7 9
>> sr = sum(A’)
sr =
6 15
>> ts=sum(sum(A))
ts = 21
11
Computing Column & Row Means
>> cm = sum(A)/rows(A)
cm =
2.5000 3.5000 4.5000
>> rm = sum(A’)/columns(A)
rm =
2 5
All the indexing operations that work for matrix objects also work for strings.
Example: Combining different strings in an array
>> s1 = 'Hello'
s1 = Hello
>> s2 = 'World!'
ss = World!
>> Combined = [s1, ' ', s2]
Combined = Hello World!
>> Combined = [s1 ' ' s2]
Combined = Hello World!
>> length(Combined)
ans = 12
12