0% found this document useful (0 votes)
149 views26 pages

2a GNU Octave Online Compiler Refresher Tutorial

This document provides a tutorial on using GNU Octave, which is a free and open-source alternative to MATLAB. It discusses why GNU Octave is useful, how to use it as a calculator to evaluate expressions and format outputs, how to declare variables and generate vectors and matrices, perform operations on vectors and matrices, plot functions, use conditional statements and loops, and create user-defined functions. The document contains examples of code to demonstrate these capabilities in GNU Octave.

Uploaded by

ernest
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)
149 views26 pages

2a GNU Octave Online Compiler Refresher Tutorial

This document provides a tutorial on using GNU Octave, which is a free and open-source alternative to MATLAB. It discusses why GNU Octave is useful, how to use it as a calculator to evaluate expressions and format outputs, how to declare variables and generate vectors and matrices, perform operations on vectors and matrices, plot functions, use conditional statements and loops, and create user-defined functions. The document contains examples of code to demonstrate these capabilities in GNU Octave.

Uploaded by

ernest
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/ 26

GNU OCTAVE ONLINE COMPILER:

REFRESHER / TUTORIAL
CE 007 | Numerical Solutions on CE Problems
Why GNU Octave?
▪ MATLAB is a very useful tool ▪ You can access it using:
for all fields of engineering. https://octave online.net/
▪ GNU Octave works like
▪ If you know how to code in one
MATLAB. language, you can easily learn
▪ But GNU Octave is free! other languages.

Output
Script
Command Line
GNU Octave as a Calculator
On the Command Line, calculate the following expressions:

1. 10 sin 0.5 + 𝜋 2. 𝜋 2 𝑒 − 𝑒+3

• Octave can recognize the • Octave can recognize the


word “pi” as a keyword. word “e” as a keyword.
• Octave treats trig function • Use “^” for exponentiation
arguments as radians by • Use parenthesis as much as
default. possible.
GNU Octave as a Calculator
On the Command Line, calculate the following expressions:

2
3. ln + cos −1 0.5
3𝜋

• Important: To implement the natural logarithm, use “log”.


• Note these related functions:

log10 10

log 2 8
GNU Octave as a Calculator
If you wish to format the answer differently:

format command Action


>> format returns to default format
>> format short sets format to short
>> format long scaled fixed point with 15 digits after
decimal point
>> format short e floating point short
>> format long e floating point long
>> format short g chooses between fixed or floating point
>> format long g chooses between fixed or floating point
>> format bank fixed dollars and cents
>> format rat ratio of small integers
Declaring Variables
If the lengths of 2 sides
of a right triangle are 20
and 21, find the length
of the hypotenuse.
Note: End each line with a
semicolon. If there is none, that
variable will be printed.

Find the volume of a


thick pizza with a radius
of 𝑧 = 5 inches and
height of 𝑎 = 0.5 inches.
Declaring Variables
Useful functions for beginners:

clear clears the value of all variables


clear x clears the value of variable x
who gives the names of the variables used
whos gives the variable name, size, byte and class
clc clears the command window
% allows user to make comments
help provides information on a function
doc opens online version of help manual
lookfor finds functions using a keyword
Vectors and Matrices
Generate the following sequences in GNU Octave:

1, 2, 3, …, 8, 9, 10 >> 1:10
1, 4, 7, 10, …, 22 >> 1:3:22

10, 9, 8, …, 2, 1, 0 >> 10:-1:0


-3, -2, -1, 0, 1, 2, 3 >> -3:3

0.001, 0.01, 0.1, 1, 10, 100, 1000 >> 10.^(-3:3)


-6, 4, 2, 0, 2, 4, 6 >> (-3:3)*2
Vectors and Matrices
Generate the following vectors and matrices in GNU Octave:

3, 1, 4, 1, 5, 9, 2, 6, 5 >> [3 1 4 1 5 9 2 6 5]
1, 2, 3, 7, 6, 5, 4 >> [1:3 7:-1:4]

3 0 >> [3 0; 2 1]
2 1

1 4 7 >> [1:3:7; 0:5:10; 1:3]


0 5 10 or
1 2 3 >> [1 4 7; 0 5 10; 1 2 3]
Vectors and Matrices
Save the following vector to variable A: 3, 1, 4, 1, 5, 9, 2, 6, 5
>> A = [3 1 4 1 5 9 2 6 5];
You can access subsets of this vector such as:
Get the 3rd element: >> A(3)
ans = 4
Get the last element: >> A(end)
ans = 5
Get the 2nd to 4th elements: >> A(2:4)
ans = 1 4 1
Get the last 3 elements: >> A((end-2):end)
ans = 2 6 5
Vectors and Matrices
Encode the following matrix A in GNU Octave, then perform the
following tasks:
1 0 2
A= 3 9 6 >> A = [1 0 2; 3 9 6; 7 5 8]
7 5 8
0
Output only the
>> A(:,2) Output: 9
second column
5
Output only the
>> A(3,:) Output: 7 5 8
third row
Take the diagonal
>> diag(A) Output: 1 9 8
elements
Vectors and Matrices
Encode the following matrix A in GNU Octave, then perform the
following tasks:
1 0 2
A= 3 9 6 >> A = [1 0 2; 3 9 6; 7 5 8]
7 5 8

Delete the first row >> A(1,:) = [] Output: 3 9 6


7 5 8
3 9 6
Append [1 2 3] after >> A = [A; 1 2 3]
Output: 7 5 8
the last row
1 2 3
Change the element 3 9 6
in row 2, column 3 >> A(2,3) = -1 Output: 7 5 −1
to “-1” 1 2 3
Vectors and Matrices
These are the common functions used for vectors and matrices.
You can use >> help ____ to learn how to use them.

ones length max sort mean triu


zeros numel min sortrows std tril
eye size cumsum find poly rank
rand reshape cumprod cross polyval flip
randi sum cummax dot polyder fliplr
magic diff cummin det eig flipud
vander prod transpose inv svd rot
Operations on Vectors and Matrices
1 0 2 1 0 1
A= 3 9 6 B= 0 1 0 C= 1 4 7 D= 4 2 0
7 5 8 1 0 1
You can perform these operations on vectors and matrices.
Addition Subtraction
Operations on Vectors and Matrices
1 0 2 1 0 1
A= 3 9 6 B= 0 1 0 C= 1 4 7 D= 4 2 0
7 5 8 1 0 1
You can perform these operations on vectors and matrices.
Multiplication
Element-wise Multiplication:

Matrix Multiplication: Inner Product: Outer Product:


Operations on Vectors and Matrices
1 0 2 1 0 1
A= 3 9 6 B= 0 1 0 C= 1 4 7 D= 4 2 0
7 5 8 1 0 1
You can perform these operations on vectors and matrices.
Division
Element-wise:

Left-divide: Right-divide:
Operations on Vectors and Matrices
1 0 2 1 0 1
A= 3 9 6 B= 0 1 0 C= 1 4 7 D= 4 2 0
7 5 8 1 0 1
You can perform these operations on vectors and matrices.
Exponentiation
Element-wise: Scalar:
Operations on Vectors and Matrices
If there is an operation performed between a row vector and a
column vector, then implicit expansion occurs:
Example 1:
4
Let’s add A = 1 2 3 and B = 5
6

Note: The apostrophe transposes the vector/matrix.


Operations on Vectors and Matrices
If there is an operation performed between a row vector and a
column vector, then implicit expansion occurs:
Example 2:
Take the average of each column
1 5 8
of numbers in A = 2 5 10
3 5 12
then subtract the average from
each column.
Plotting
Produce a plot of 𝑦 = 2 sin 𝑡 + 0.5𝜋 within the range of 𝑡 ∈
0, 2𝜋 .

The function
linspace(a,b,N)
creates a row vector of N
numbers that starts with
a and ends with b.
Plotting
Create a 3D depth image of the surface defined by:
𝑓 𝑥, 𝑦 = 𝑥 exp − 𝑥 − 2𝑦 2 2 − 2.5𝑦 2
Conditional Statements
Write a script that checks if a value stored in variable ‘x’ is odd or
even.
Conditional Statements
Relational operators compare 2 elements and return whether
the relationship is true or false.

Relational Operators What it means


== Determine equality
>= Determine greater than or equal to
> Determine greater than
<= Determine less than or equal to
< Determine less than
~= Determine “not” equal to
isequal Determine equality in objects
Loops
Given the following vector:
𝐴 = 9 1 0 21 5 13 2
Iterate through the elements and change the element to zero if
it is greater than or equal to 10.
Functions
Create a function that takes a temperature in ℃ as input, then
outputs its value but converted to ℉.
ENGR. ELIANDRE RUSSEL P. VISTAL
Faculty, Civil Engineering Department
College of Engineering and Architecture
Technological Institute of the Philippines-Quezon City
938 Aurora Blvd., Cubao, Quezon City
ervistal.ce@tip.edu.ph
+63975 825 1232

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy