Professional Documents
Culture Documents
Numerical Computation - Octave 2 0f 2
Numerical Computation - Octave 2 0f 2
Outline of Module
Introduction
Octave Commands
Arithmetic Operations
Variables
Limits of Numerical data
Built-in-Functions
input and out put
Arrays and Matrixes
Programming
Plotting
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Introduction
Introduction (continued)
Commands
dir - show contents of directories
cd - change directories
delete - remove file
copyfile - copy file
movefile - rename or move file
mkdir - make directory
rmdir - remove directory
clc - clear screen
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Commands (continued)
exit - exit program
edit - opens text editor
pwd - pathway to current directory
.. - change to parent directory
help - when you need help!
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Arithmetic Operations
Variables
must start with letters
may contain letters, numbers or
underscore
no space or special characters
not more than 63 characters
case-sensitive
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Variables (continued)
Helps if it's descriptive –
e.g. Tank1Volume to represent volume
of first tank
clear commands wipes all variables from
memory
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Variables types
Numerical - 4
Character - X
Logical - True, False
o X = 20; stores value 20 to X
o Y = 'Just do it!' ; stores 'Just do it'
in Y
o Note single quote
o command who, and whos display
variables created
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Example
>> MyIncome = 100000
>> MyExpenditure= 200000
>> MySavingsIn2018 = MyIncome + MyExpenditure
Built-In Functions
names case sensitive:
• sin, cos, tan, pi, etc
arguments in radians (not degrees)
• sin (pi/2) gives 1
• sqrt(9) gives 3
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Simple Input-Output
Arrays /Matrices
MATLAB - MatrixLaboratory
Array - made of elements in rows and
columns
Programming
Logical Variables
false (0)
true (1)
>> X=54
>> Y=98
>> X>Y returns false
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming (continued)
Flow Control
if Statements
switch statements
while loop statements
for loop statements
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming (continued)
Flow Control
if Statements
switch statements
while loop statements
for loop statements
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming (continued)
if and for Statements
x=rand(5,1)*10
ret = x(1);
for i = 2:length(x),
if x(i) < ret;
ret=x(i)
end
end;
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
clc
myValues=rand(5,1)*10
maximumNumber(myValues)
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming - User defined function (continued)
function returnedValue = minimumNumber(x)
returnedValue = x(1);
for i = 2:length(x),
if x(i) < returnedValue;
returnedValue=x(i);
end
end;
Endfunction
clc
myValues=rand(5,1)*10
maximumNumber(myValues)
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming - .m Files
myValues=rand(5,1)*10
disp ('Minimum value= '), disp (minimumNumber(myValues))
disp ('Maximum value= '), disp (maximumNumber(myValues))
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming - .m files (continued)
minimumNumber.m
2X + 5Y - Z = 3
4X + Y - 2Z = -3
Y-Z=7
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Simultaneous Equation
2X + 5Y - Z = 3
4X + Y - 2Z = -3
Y-Z=7
x = -10:1:10;
m=10;
y=0.5*m*x.^2;
plot (x, y);
xlabel ("velocity");
ylabel ("Kinetic Energy");
title ("Kinetic Energy variation");
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph – Linear Plots
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph – Linear Plots
x = -10:0.1:10;
plot (x, sin (x));
xlabel ("x");
ylabel ("sin (x)");
title ("Simple 2-D Plot");
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph – Linear Plots
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph - Sub plots
y=[25,30,55,34,52,35]
x=[1990,1991,1992,1993,1994,1995]
bar(x,y)
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph - bar chart
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph - Pie chart
[X,Y] = meshgrid(-2:.1:2,-2:.2:2);
f = -X.*Y.*exp(-2*(X.^2+Y.^2));