0% found this document useful (0 votes)
50 views12 pages

Chapter2 - M-Review of Octave

This document provides an overview of the GNU Octave programming environment and some basic operations that can be performed. It begins with an introduction to Octave, noting that it is open-source and compatible with MATLAB. It then describes Octave's graphical user interface. The remainder of the document outlines some common numerical tasks Octave can perform and provides details on basic commands, data types including numbers, matrices and vectors, and operations such as arithmetic, accessing elements, defining matrices, and appending matrices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views12 pages

Chapter2 - M-Review of Octave

This document provides an overview of the GNU Octave programming environment and some basic operations that can be performed. It begins with an introduction to Octave, noting that it is open-source and compatible with MATLAB. It then describes Octave's graphical user interface. The remainder of the document outlines some common numerical tasks Octave can perform and provides details on basic commands, data types including numbers, matrices and vectors, and operations such as arithmetic, accessing elements, defining matrices, and appending matrices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Faculty of Engineering (Elmattaria) Mechanical Power Engineering

Department

Course: Programming applications in mechanical engineering


Course Code: ICT301
Lecturer: Dr. Abouelmagd Abdelsamie & Dr. Michael Mansour

Chapter 2: Introduction to GNU Octave

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)

Figure 1: Details of the graphical user interface.

The main Octave window components shown in Figure 1 are:


1. Current directory
It is the directory on the computer where Octave script files will be saved.
2. File Browser
It displays the directories and files in the current directory. The current directory can be changed
using the Gear icon in the file browser window.
3. Command Window
Here Octave commands are executed, and results are displayed to the user.
The command prompt symbol is denoted by >>
4. Workspace
It displays all the variables that are currently being used by the Octave, including details like the
variable name, dimension, and value.
5. Command History
It displays the previous Octave commands entered in the Command Window.
6. Documentation
Help and Documentation for all commands and built-in function
7. Variable Editor
It displays all the values of variables. Editing is possible
8. Editor
Here you can write the code, save script files, execute, debug and run the Octave scripts.

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

>> a=2; b=2*a, c=3*a


b=4
c=6

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.

Operation Operator Symbol Example


Addition + a+b
Subtraction – a–b
Multiplication * a*b
Right Division / a/b
Left Division \ a\b
Power/Exponentiation ** or ^ a ** b or a ^b

Example (1 + 2 x 3)

>> 1+2*3
ans =
7

Example (1 + 2 x 3, Assigning the output value to a variable)

>> x = 1+2*3
x=7

Example (Overwriting variable)

>> t = 5;
>> t = t+1
t=6

b. Data types in Octave


1. Numbers: integers, real numbers and complex numbers
o Real numbers include integers, whole, natural, rational, and irrational numbers
▪ An Integer number is a positive or negative whole number with no fractional or decimal part.
▪ A whole number is a positive whole number with no fractional or decimal part.
▪ A natural number or "a counting number" is a whole number but zero is not included.
▪ A rational number is a number that can be written as a ratio of two integers (a simple fraction)
▪ An irrational number is a number that cannot be written as a ratio of two integers (not a simple
fraction)

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

>> >> w = [1;4;7;10;13]


w=
1
4
7
10
13

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

>> >> w = v'


w=
1
4
7
10
13

>> >> z = w’
z=
1 4 7 10 13

Access of array elements


Access of a single elements
z(1) is the first element of vector v = 1, z(2) its second element =4, and so on.

Access of blocks of elements


to access blocks of elements, we use colon notation (:).

For example, to access the first three elements of z, we write


>> z(1:3)
ans =
1 4 7

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

whereas writing z(1:end) produces a row vector from all elements in z.

>> z(1:end)
ans =
1 4 7 10 13

6
Matrices

A matrix is an array of numbers. To define a matrix


− begin with a square bracket, [
− separate elements in a row with spaces or commas (,)
− use a semicolon (;) to separate rows
− end the matrix with another square bracket, ]

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.

Defining a too large vector matrix

− Colon operator (:)


x = Minimum : Step : Maximum;
Define x, where x is a row vector x consisting of points starting from 0 to 5 with a step of 1 (0, 1, 2, 3, 4, 5)

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

To pick up the third column:

>> 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(:,j) is the jth column of A, while


− A(i,:) is the ith row, and
− A(end,:) picks out the last row of A

Extracting sub-matrix from a matrix


The colon operator can also be used to extract a sub-matrix from a matrix A.

>> A(:,2:3)
ans =
2 3
5 6
8 0

A(:,2:3) is a sub-matrix with the second and the third columns of A.


Deleting elements

>> A(:,2)=[]
A=
1 3
4 6
7 9

8
Extending the matrix

>> B = [A, [2;5;8]]


B=
1 3 2
4 6 5
7 9 8

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

Elementary matrices in Octave

Table 2: Elementary matrices in Octave


Elementary matrices
eye(m,n) Returns an m-by-n matrix with 1 on the main diagonal
eye(n) Returns an n-by-n square identity matrix
zeros(m,n) Returns an m-by-n matrix of zeros
ones(m,n) Returns an m-by-n matrix of ones
diag(A) Extracts the diagonal of matrix A
rand(m,n) Returns an m-by-n matrix of random numbers

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

Array vs Matrix operations


Array arithmetic operations or array operations for short, are done element-by-element

Table 3: Summary of matrix and array operations.

Operation Matrix operation Array operations (element-by-element)


Addition + +
Subtraction - -
Multiplication * .*
Division / ./
Left division \ .\
Exponentiation ^ .^

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

means 3 rows and 3 columns. Or more explicitly with


>> [m,n]=size(A)
m=3
n=3

Number of Rows and Columns

>> A=[1 2 3; 4 5 6];


>> r = rows(A)
r=2
>> c = columns(A)
c=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)).

>> A=[1 2 3; 4 5 6];


>> length(A)
ans = 3
>> A=[1 2 3; 4 5 6; 7 8 9; 10 11 12];
>> length(A)
ans = 4

Computing Column and Row Sums

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

4. Strings (Text Objects):


A character string consists of a sequence of characters (text) enclosed in either double-quote “…” or single-
quote ‘…’ marks. Internally, Octave stores strings as matrices of characters.

>> string = 'some text'


string = some test
>> s = "some text"
s = some text

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

You might also like