Professional Documents
Culture Documents
CAICE FinaltermW4 PDF
CAICE FinaltermW4 PDF
Chemical Engineering
The name MATLAB stands for matrix laboratory. MATLAB was originally written
to provide easy access to matrix software developed by the LINPACK and EISPACK
projects, which together represent the state-of-the-art in software for matrix
computation.
5
Getting started with MATLAB
Starting MATLAB:
On Windows platforms, start MATLAB by double-clicking the MATLAB shortcut icon
on your Windows desktop
MATLAB Desktop:
6
MATLAB Windows
Command windows
Figure window
Editor window
Help window
Current directory
Work space
Command history
7
Getting started with MATLAB
8
Getting started with MATLAB
9
Working in the command window
10
The semicolon (;)
11
The %, clc, clear, clf commands
13
Order of Precedence
Precedence Operation
1st ()
2nd ^
3rd * And /
4th + and -
14
Class practice: MATLAB as Calculator
15
Display formats
Command Example
format short >>290/7 => 41.4286
format long >>290/7 =>41.42857142857143
format short e >>290/7 =>4.1429e+001
format long e >>290/7
=>4.142857142857143e+001
format short g >>290/7 =>41.429
format long g >>290/7 =>41.4285714285714
format bank 290/7 => 41.43
format compact Elimintes empty lines
format loose Adds empty lines
16
Built in math functions
Function Description
sqrt(x) Square root
exp(x) Exponential
abs(x) Absolute value
log(x) Natural lorithim
log10(x) Base 10 logrithim
factorial(x) Factorial function x!
17
Built in trigonometric functions
Function Description
18
Built in rounding functions
Function Description
round(x) Round to nearest integer
fix(x) Round towards zero
ceil(x) Round towards infinity
floor(x) Round towards –infinity
rem(x,y) Returns remainder of x is divided by
y
sign(x) Sign function, returns 1 if x>0, -1 if
x<0, and 0 if x=0
19
Pre defined variables
Variable Description
ans Name assigned to the last unassigned value by
matlab
pi The number Pi
eps The smallest difference between two numbers,
equal to 2^(-52)
inf Used for infinity
i Defined as (-1)^0.5
j Same as i
NaN Not a number, when matlab can not determine a
number value it assigns NaN to the variable, 0/0
20
Some useful commands
Command Outcome
21
Some useful points to remember
22
Creating Arrays in Matlab
23
Creating Arrays in Matlab
24
Class practice to create one dimensional array
25
Lab practice and assignment
Install MATLAB at your laptops, computers
Familiarize yourself with matlab interface
1: 2+3*9/6 = ?
2: 23-6/7+8.3 = ?
1 1 1
3: + −
(2−0.1)2 +0.3 (6−0.5)2 +0.6 (8−0.1)2 −10.3
27
Creating 2-dimensional array
a=[1 2 3 ; 4 5 6; 7 8 9]
a=[(1:1:3); linspace(4,5,3) ; 7 8 9]
a=zeros (rows, columns)
a = ones (rows, columns)
a= eye (rows, columns) (create I vector of given size of
rows and columns)
28
Array operations
29
Array operations
b(:,[1 3]) elements of 1st and 3rd column will be displayed.
b(5) 5th element will be displayed
diag(b) elements of diagonal will be displayed
>> a=[1:1:5];
Replace 3rd element of 1st row with zero
>> a(1,3)=0
a=
1 2 0 4 5
>> a([1,3])=0 Replace 1st and 3rd element with zero
a=
0 2 0 4 5
31
Changing and Deleting Array Elements
32
Manipulating Arrays
Transpose
Transpose of A = A’
fliplr(A) (Flip left/right)
flipud(A) (Flip up/down)
33
Some built in functions
• length(a)
• size(a)
• reshape(a,m,n)
• diag(v)
– More from help in Elementary Matrix and
Matrix Manipulation
34
Multiplication and addition
• Scalars A scalar can be added to or multiplied with each
element of an array; e.g.,
A = [1 3 4 5 7 8 ]
for 2 + A
ans will be ans= 3 5 6 7 9 10
35
Multiplication and addition
36
Multiplication and addition
37
Array division
If AB=BA=I then
B is inverse of A and A is inverse of B and I is an
identity matrix
All A, B, and I are square matrices
Inverse of A = A^(-1) or inv(A)
LEFT division \ : to solve matrix equation AX=B
In matlab X=A\B
or X = inv(A)*B
In MatLab X= A\B
RIGHT division /: is used to solve XC=D where X and
D are ROW vectors
X= D/C
38
Array determinant
39
Summation
40
Strings and string variables
‘abc’ is a string
‘a256cc’ or ‘matlab’ or ‘3%abc’ are all strings
Used
to display text messages
In formating commands of plots
Input argument of some functions
A=‘this is a string’
A=‘the numbers are 50 out of 100’
41
Built in functions
42
Lab practice
43
Polynomial equation
Polynomial equation is represented by single row vector where
coefficients are written in descending order.
𝑝 = 3𝑥 3 + 𝑥 2 + 𝑥 + 4
In Matlab it will be written as
p=[3 1 1 4]
44
Lab practice
45
2D and 3D Plotting
46
2D Plotting
Plot
Axis labels
Scale
Titles
Elements of 2D plot
Legend
Text
Line style
Marker
47
2D Plotting: Elements of 2D plot
48
2D Plotting: Elements of 2D plot
49
2D Plotting: Line specifiers
50
2D Plotting: Line color specifiers
Red r Magenta m
Green g Yellow y
Blue b Black k
Cyan b White w
51
2D Plotting: Element of graph
title('title')
xlabel('xlabel')
ylabel('xlabel')
xlim([minimum value max-value])
ylim([minimum value max-value])
legend(‘string1’, 'string2’)
axis equal
axis square
52
2D Plotting: axis
xlim([minimum value max-value])
ylim([minimum value max-value])
axis equal
axis square
axis tight
axis normal
grid on
grid off
53
2D Plotting: Element of graph
54
Plotting multiple graphs
plot (x,y,x,z)
or use the command of hold on and plot multiple
graphs on same figure
55
Class Activity
x=linspace(0,1,100);
56
Semi log and log-log plots
• semilogy(x,y)
• semilogx(x,y)
• loglog(x,y)
• Caution
– Log of 0 does not exist
– Log of –ve number is complex
57
Other common plots
• bar(x,y)
• barh(x,y)
• stairs(x,y)
• stem(x,y)
• pie(x)
• hist(x)
58
Polar plots
polar(theta,r)
Example
theta=linspace(0,2*pi,100)
r=3*sin(0.5*theta).^2+theta
polar(theta,r,'-r')
59
Lab Assignment
One of the most successful correlations is called Antoine equation, which
uses three coefficients, A, B, and C, which depend on the substance being
analyzed. Antoine equation is as follows:
Subplot(2,3,1), plot(x,y)
Subplot(2,3,2), plot(x,y)
62
3D-plot
plot3(x,y,z)
63
Surface plot(3-D plot)
64
Lab Assignment
One of the most successful correlations is called Antoine equation, which
uses three coefficients, A, B, and C, which depend on the substance being
analyzed. Antoine equation is as follows:
66
Scripts File
Script file is a sequence of Matlab commands
On RUN, commands executed one-by-one in the
order
Outputs are displayed in command window if ; is
not used at the end of the commands
Script files can be typed and edited in any editor
and then pasted in matlab editor
Contain .m extension and also called m-files or
programs
67
Scripts File
There are two different kinds of m-files. The simplest, a script file, is
a collection of MATLAB commands. When the script file is executed
by typing its name at the interactive prompt, MATLAB reads and
executes the commands within the m-file just as if these were
entering them manually. Executing a m-file is like cutting and
pasting the m-file contents into the MATLAB command window.
68
Writing and running m-file
Click the New Script button on the Home tab.
Use the edit function.
Create the file and write the sequence of commands
Save the file
And execute by typing the name of file in command window
% example
disp(‘example’)
x = 0:.1:1;
A = [x; exp(x)];
plot(x,exp(x))
Demonstration with MATLAB
69
Writing and running m-file
Script M-files are ideal for repeating a calculation,
but with some parameters changed.
70
Output commands
• disp
– disp(name of a variable)
– disp(‘text as string’)
71
fprintf command
Character Description
e 1.709098e+002
E 1.709098E+002
f Fixed point notation
g Shorter of e or f notation
G Shorter of E or f notation
i Integer
72
fprintf command
value Type Conversion Details
Integer, signed %d or %i Base 10
Integer, unsigned %u Base 10
%o Base 8 (octal)
%x Base 16 (hexadecimal), lowercase letters a–f
%X Same as %x, uppercase letters A–F
Floating-point %f Fixed-point notation (Use a precision operator to
number specify the number of digits after the decimal point.)
%e Exponential notation, such as 3.141593e+00 (Use a
precision operator to specify the number of digits after
the decimal point.)
%E Same as %e, but uppercase, such
as 3.141593E+00 (Use a precision operator to specify
the number of digits after the decimal point.)
%g The more compact of %e or %f, with no trailing zeros
(Use a precision operator to specify the number of
significant digits.)
%G The more compact of %E or %f, with no trailing zeros
(Use a precision operator to specify the number of
significant digits.)
Characters or %c Single character
strings %s Character vector or string array. 73
Fprintf command
74
fprintf to save output to a file
Open a file using fopen command
Write the out put to open file using fprintf command
Close the open file using fclose command
Example is as follows
75
fprintf to save output to a file
76
fprintf to save output to a file
77
fprintf to save output to a file: example
78
Lab Practice and Assignment
a) Using script file option and fprintf command
1: write a program to prepare a conversion table
for cm to inches. Values in cm are 1-10 with step
size of 1 cm.
79
Function files
80
Function files
81
Structure of Function files
1 function [output_var]=function_name(input_var)
2 Statement/(s)
3 Execution in command window
82
Structure of Function files
1 function [output_var]=function_name(input_var)
2 Statement/(s)
3 Execution in command window
83
Lab Practice-ODE solution Built-in Function
84
Programming: Conditional statements
And loops
85
Programming: Conditional statements
And loops
86
Programming: Conditional statements
And loops
87
Programming: Conditional statements
And loops
2: Decision statements (if statement)
If MATLAB has to make a decision; it must decide whether to
perform a function or not, the if construct, which is fundamental
to all computing languages, is the basis of such decision-
making.
Condition is usually a logical expression, i.e. an expression
containing a relational operator, and which is either true or false
88
Programming: Conditional statements
And loops
89
Programming: Conditional statements
And loops
2: (if statement)
90
Programming: Conditional statements
And loops
3: (if else statement)
91
Programming: Conditional statements
And loops
3: (elseif statement)
92
Class Activity: Euler Method
93
Class Activity: RK Method
94
Lab Practice Questions and Assignment
95
Aspen Hysys: Getting
Started
96
Recommended Literature
Nayef Ghasem, “Computer
Methods in Chemical Engineering”
CRC Press.
Hysys Tutorial Manuals
Hysys Help and case studies and
examples (available with Aspen
Hysys)
Encyclopedia
97
Introduction: Aspen Hysys
Aspen HYSYS is a chemical process simulator used to
mathematically model chemical processes, from unit
operations to full chemical plants and refineries.
V10
98
Advantages of Aspen Hysys
User friendly
Powerful data base
It can cover complex industrial processes
99
Getting started
Install Aspen Hysys
Open Aspen Hysys and start New Case
100
Getting started
Add Components
101
Getting started
Choose components and add to the
component list
102
Getting started
Add Fluid Package
103
Guide to select fluid package
104