Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

University of Lagos

Department of Chemical and Petroleum Engineering .


CHG 244: INTRODUCTION TO ENGINEERING COMPUTING
Daniel Ayo, PhD.

Numerical Computation with Octave


(1 0f 2)
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

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

 Octave - free equivalent of MATLAB


 Versions for Windows, Mac, or Linux
 Can be down-loaded from:
 http:/www.gnu.org/software/octave/
download.html
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

Introduction (continued)

 Option to work in Octave:


o Command Line;
o Integrated Development Environment
 Commands are case-sensitive:
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

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

+ - addition e for scientific notation:


- - subtraction 412345 = 4.12345e5
* - multiplication 0.12345 = 5e5
/ - division
^ - exponent
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

Arithmetic Operations (continued)

Order of execution : (PEMDAS)


 Parentheses
 Exponents
 Multiplications
 Division
 Addition
 Subtraction
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

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

Limits of Numerical data

 Largest number - 10+308


 larger number taken as infinity (inf)
 smallest number - 10-308
 smaller number taken as zero (0)
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

System displays 300000


University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

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

 input function to get value from user

>> AmntOwed = input ('Enter amount: ')


>> AmntOwed = input ('Enter your name: ', 's')

's' indicates input is a character


University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

Simple Input-Output (continued)


Example.
>> WhoOwes= 'Stephen owes me '
>> Amount = 200
>> CurrencyType = ' Naira'
>>disp(WhoOwes,num2str(Amount),CurrencyType)

System prints: 'Stephen owes me 200 Naira


University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

Arrays /Matrices
 MATLAB - MatrixLaboratory
 Array - made of elements in rows and
columns

e.g. 2X3 Array:


Row1,Column1 Row1,Column2 Row1,Column2
Row2,Column1 Row2,Column2 Row2,Column2
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

Arrays /Matrices (continued)


 MyArray= [10 ,20,30: 20,40,100]
 MyArray= [10 20 30: 20 40 100] or
• value of Element in Row1,Column1 = 10
• value of Element in Row1,Column2 = 20
• value of Element in Row1,Column3 = 30
• value of Element in Row2,Column1 = 20
• value of Element in Row2,Column2 = 40
• value of Element in Row2,Column3 = 100
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

Arrays /Matrices (continued)

>> MyArray= [10 20 30: 20 40 100]


>> NewArray=MyArray.*2
• multiplies every element by 2 hence
NewArray= [20 40 60: 20 80 200]

Similarly, with other arithmetic operators


University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

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

Programming - switch statements


Lecturer = 'Dr. Nwalor'
switch(Lecturer)
case 'Dr. Ayo' Course = 'Introduction to Chemical
Engineering Operation';
case 'Dr. Nwalor' Course = 'Thermodynamics';
case 'Dr. Tolu Ajayi' Course ='Optimization';
otherwise
disp('I dont know the course name');
endswitch
Clc

disp(['The course namne is ',Course]);


University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation

Programming - while loop statements


clear; clc
score=[20,50,70,61];
N=numel(score)
i=1
while (i<(N+1))
disp ([i,score(i)]);
i=i+1;
end
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming - User defined function
function returnedValue = maximumNumber(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 - 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

• Save Program to determine minimum number


as a function (minimumNumber.m)
• Save Program to determine minimum number
as a function (maximumNumber.m)
• Use another program to generate Random
Numbers
• Call determine minimumNumber and
maximumNumber to determine minimum
and maximum values respectively
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming - .m files (continued)

#Generate Random Numbers, determine Minimum an maximum

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

function returnedValue = minimumNumber(x)


returnedValue = x(1);
for i = 2:length(x),
if x(i) < returnedValue;
returnedValue=x(i);
end
end;
Endfunction
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Programming - .m files (continued)
maximumNumber.m

function returnedValue = minimumNumber(x)


returnedValue = x(1);
for i = 2:length(x),
if x(i) > returnedValue;
returnedValue=x(i);
end
end;
Endfunction
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Simultaneous Equation
• SET OF LINEAR EQUATIONS
• DESCRIBED BY Y = AX
• X = Y/A BUT TO AVOID DIVISION BY ZERO WE
WRITE: X = A\Y

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

clear all, close all, clc


A = [2,5,-1;4,1,-2;0,1,-1]
Y = [3;-3;,7]
X = A\Y
disp(X)
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: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

x = [0:0.1:10]; subplot(2,2,2) subplot(2,2,3)


subplot(2,2,1) y = cos(x); y = sin(x);
y = sin(x); plot(x, y,'-g+') plot(x, y)
plot(x, y,'rv') xlabel('x') hold on
xlabel('x') ylabel('cos(y)') y = cos(x);
ylabel('sin(y)') title('cos x plot alone') plot(x, y)
title('Sin x plot hold off
alone') xlabel('x')
ylabel('sin(x) and
cos(x)')
title('sin(x) and cos(x)
togethetr')
grid on
%axis equal
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph – Sub Plots
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph - bar chart

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 = [1, 3, 0.5, 2.5, 2,4];


pie(X
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph – Pie chart
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph - Surface Plot

[X,Y] = meshgrid(-2:.1:2,-2:.2:2);
f = -X.*Y.*exp(-2*(X.^2+Y.^2));

%mesh(X,Y,f), xlabel('x'), ylabel('y'), grid


mesh(X,Y,f), xlabel('x'), ylabel('y'),
zlabel('z'), grid
University of Lagos Department of Chemical and Petroleum Engineering .
CHG 244: INTRODUCTION TO ENGINEERING COMPUTING- Daniel Ayo, PhD.
Numerical Computation
Plotting Graph – Surface Plot

You might also like