Upload
others
View
14
Download
0
Embed Size (px)
Citation preview
University of Baghdad
College of Engineering
Department of Chemical Engineering
2016
Lecture No. 9
Dr. Mahmood Khazzal Hummadi
Samar Kareem
MATLAB for Chemical engineer
Basic and Applications
Department of Chemical Engineering Baghdad University
2 MKH
Lecture No. 9
Looping statements and descision statements:
The conditions are given below. The column of the left hand side is to compare two values and those on the right
hand side are to add multiple conditions:
1. LOOPING: Looping or iterative functions are extremely useful in engineering problem solving.
Department of Chemical Engineering Baghdad University
3 MKH
1. For statements loop a specific number of times, and keep track of each iteration with an
incrementing index variable.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.
For example,
preallocate a 10-element vector, and calculate five values:
x = ones(1,10);
for n = 2:6
x(n) = 2 * x(n - 1);
end
2. While statements loop as long as a condition remains true.
For example, find the first integer n for which factorial(n) is a 100-digit number:
n = 1;
nFactorial = 1;
while nFactorial < 1e100
n = n + 1;
nFactorial = nFactorial * n;
end
Each loop requires the end keyword.
Department of Chemical Engineering Baghdad University
4 MKH
It is a good idea to indent the loops for readability, especially when they are nested (that is, when one loop contains another loop):
A = zeros(5,100);
for m = 1:5
for n = 1:100
A(m, n) = 1/(m + n - 1);
end
end
You can programmatically exit a loop using a break statement, or skip to the next iteration of a loop using
a continue statement. Exit Loop before Expression Is False.
Example: Sum a sequence of random numbers until the next random number is greater than an upper limit. Then, exit the loop using a break statement.
limit = 0.8;
s = 0;
while 1
tmp = rand;
if tmp > limit
break
end
s = s + tmp;
end
Department of Chemical Engineering Baghdad University
5 MKH
2. Decision
Decision making structures require that the programmer should specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
1. if ... end statement
If the expression evaluates to true, then the block of code inside the if statement will be executed. If the expression evaluates to false, then the first set of code after the end statement will be executed.
Example: Create a script file and type the following code :
a= 10;
% check the condition using if statement
if a < 20
% if condition is true then print the following
fprintf('a is less than 20\n' );
end
fprintf('value of a is : %d\n', a);
Department of Chemical Engineering Baghdad University
6 MKH
2. if...else...end statement An if statement can be followed by an optional else statement, which executes when the expression is false.
If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed. If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
Example
Create a script file and type the following code −
a = 100;
% check the boolean condition
if a < 20
% if condition is true then print the following
fprintf('a is less than 20\n' );
else
% if condition is false then print the following
fprintf('a is not less than 20\n' );
end
fprintf('value of a is : %d\n', a);
3. if...elseif...elseif...else...end Statements.
An if statement can be followed by one (or more)
optional elseif... and anelse statement, which is very useful to test various conditions.
When using if... elseif...else statements, there are few points to keep in mind:
An if can have zero or one else's and it must come after any elseif's.
An if can have zero to many elseif's and they must come before the else.
Once an else if succeeds, none of the remaining elseif's or else's will be tested.
if <expression 1>
% Executes when the expression 1 is true
<statement(s)>
Department of Chemical Engineering Baghdad University
7 MKH
elseif <expression 2>
% Executes when the boolean expression 2 is true
<statement(s)>
Elseif <expression 3>
% Executes when the boolean expression 3 is true
<statement(s)>
else
% executes when the none of the above condition is true
<statement(s)>
end
Example:
a = 100;
%check the boolean condition
if a == 10
% if condition is true then print the following
fprintf('Value of a is 10\n' );
elseif( a == 20 )
% if else if condition is true
fprintf('Value of a is 20\n' );
elseif a == 30
% if else if condition is true
fprintf('Value of a is 30\n' );
else
% if none of the conditions is true '
fprintf('None of the values are matching\n');
fprintf('Exact value of a is: %d\n', a );
end
4. The Nested if Statements
It is always legal in MATLAB to nest if-else statements which means you can use one if or elseif statement inside another if or elseif statement(s).
Syntax
Department of Chemical Engineering Baghdad University
8 MKH
The syntax for a nested if statement is as follows −
if <expression 1>
% Executes when the boolean expression 1 is true
if <expression 2>
% Executes when the boolean expression 2 is true
end
end
You can nest elseif...else in the similar way as you have nested if statement.
Example Create a script file and type the following code in it −
a = 100;
b = 200;
% check the boolean condition
if( a == 100 )
% if condition is true then check the following
if( b == 200 )
% if condition is true then print the following
fprintf('Value of a is 100 and b is 200\n' );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );
5. The switch Statement
A switch block conditionally executes one set of statements from several choices. Each choice is covered by a case statement.
An evaluated switch_expression is a scalar or string.
An evaluated case_expression is a scalar, a string or a cell array of scalars or strings.
Answer Value of a is 100 and b is 200 Exact value of a is : 100
Exact value of b is : 200
Department of Chemical Engineering Baghdad University
9 MKH
The switch block tests each case until one of the cases is true. A case is true when −
For numbers, eq(case_expression,switch_expression).
For strings, strcmp(case_expression,switch_expression).
For objects that support theeq(case_expression,switch_expression).
For a cell array case_expression, at least one of the elements of the cell array matches switch_expression, as defined above for numbers, strings and objects.
When a case is true, MATLAB executes the corresponding statements and then exits the switch block.
The otherwise block is optional and executes only when no case is true.
Syntax
The syntax of switch statement in MATLAB is −
switch <switch_expression>
case <case_expression>
<statements>
case <case_expression>
<statements>
...
...
otherwise
<statements>
end
Example Create a script file and type the following code in it −
grade = 'B';
switch(grade)
case 'A'
fprintf('Excellent!\n' );
Department of Chemical Engineering Baghdad University
10 MKH
case 'B'
fprintf('Well done\n' );
case 'C'
fprintf('Well done\n' );
case 'D'
fprintf('You passed\n' );
case 'F'
fprintf('Better try again\n' );
otherwise
fprintf('Invalid grade\n' );
end
When you run the file, it displays −
Well done
6. The Nested switch Statements
It is possible to have a switch as part of the statement sequence of an outer
switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.
Syntax
The syntax for a nested switch statement is as follows −
switch(ch1)
case 'A'
fprintf('This A is part of outer switch');
switch(ch2)
case 'A'
fprintf('This A is part of inner switch' );
case 'B'
fprintf('This B is part of inner switch' );
end
Department of Chemical Engineering Baghdad University
11 MKH
case 'B'
fprintf('This B is part of outer switch' );
end
Example Create a script file and type the following code in it −
a = 100;
b = 200;
switch(a)
case 100
fprintf('This is part of outer switch %d\n', a );
switch(b)
case 200
fprintf('This is part of inner switch %d\n', a );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );
When you run the file, it displays −
This is part of outer switch 100
This is part of inner switch 100
Exact value of a is : 100
Exact value of b is : 200
Department of Chemical Engineering Baghdad University
12 MKH
!
!00
N
ZY
Q
N
i
i
N
i
i
Problems
Q1) Find the value of y . Use function .
A)
B)
Q2)Write the code required to determine the value of Q. The arrays Z and Y are generated as following:
Zi=i+1 , Yi=i2-i .Use Subroutine
Q3)Solve the second order algebraic equation with one variable ax2 + bx + c =0 . The solution . Note use
Function.
Q4) Calculate the kinetic and potential energies of an object at different velocities and heights respectively. . Use velocities and heights ranged from 1 to 1000 m ( step 1). Q5) Make an array for the numbers from 0 to 100 , then make a second one represents the results of multiplying each sequential two numbers in the first array . Q6) The one dimension arrays A and B ,and the number for the each array are 20 value . calculate the summation of array A and the summation of array B and find the y value , where A(i)=i and B(i)= i^2 , where the value of y is calculate as following :
Where MB is the average of B array.
a
acbbx
2
42
Department of Chemical Engineering Baghdad University
13 MKH
Q7) Design windows application to find the densities of different six gases ( O2 ,H2 , N2 , CO2 , CH4 ,C3H8 , C2H2) at temperatures ranged from 0 to 300 oC (step 10 ) . The user should enter pressure in atm . Q8)Use loop to find the bubble point of ternary system (Ethanol 40 mol%, Water 20 mol% and Benzene 40 mol%). Knowing that the vapor pressure for three components are calculated by: Ethanol Po e=exp(18.5242-3578.91/(T-50.5)) Water Po w=exp(18.3036-3816.44/(T-46.13)) Benzene Po b=exp(15.9008-2788.51/(T-52.36)) Where Ki= Po
i /Pt ,Pt=760 ,yi =Kixi , At Bubble point Σyi=ΣKi×xi =1
Q9)Given that the vapor pressure of methyl chloride at 333.15 K is 13.76 bar, write a code to calculate the
molar volume of saturated vapor at these conditions using Redlich/Kwong equation. Knowing;
a=0.42748*R2T
c
2.5 /P
c
b=0.08664*RTc /Pc
Vi+1
=(RT/P)+b- (a*(Vi-b))/(T
1/2PV
i(V
i+b))
R=83.14 , Tc= 416.3 k , Pc= 66.8 bar Q10) A simple force balance on a spherical particle reaching terminal velocity in a fluid is given by; Where: V
t : Terminal velocity in m/s
g : Acceleration of gravity p
p: Particle density
Dp: The diameter of the spherical particle in m
CD: Dimensionless drag coefficient.
The drag coefficient on a spherical particle at terminal velocity varies with Reynolds number (Re) as followings: C
D=24/Re for Re< 0.1
CD=24*(1+0.14* Re^0.7)/Re for 0.1=<Re=< 1000
CD=0.44 for 1000<Re=< 350000 CD=0.19-8*10^4/Re for 350000 <Re Q11)Write a computer program to calculate molar volume and compressibility factor for gaseous ammonia at a pressure values (1-30 atm) by using of the Redlich-Kwong equation of state. The equations and variables are listed below. R= gas constant (R = 0.08206 atm·L/g-mol·K) ,Tc= the critical temperature (405.5 K for ammonia) Pc= the critical pressure (111.3 atm for ammonia) Compressibility factor is given by Z= PV/RT
Department of Chemical Engineering Baghdad University
14 MKH
Q12)Calculate pressure drop (N/m2) across a pipe of diameter d (m) and length L (m). The fluid properties
are velocity u (m/s), density ρ (kg/m3), and viscosity µ (kg/m.s).
duRe
For laminar flow: Re < 2100: Re
8fj
For turbulent flow: 105 > Re > 2100: 4/1Re
0396.0fj
24 ud
LjP f
Q13) write the code required to calculate the density in g/lit of 50 mol% propane (C3H8)-50 mol% isobutene
(i-C4H8) gaseous mixture.The pressure in atm, temperature in K and critical constants Pc and Tc for propane
and iso-butane.(Note: Use function ).
RTz
PM
zyz
pTz
T
TT
p
pyp
MM
mix
iimix
ririi
ci
ri
c
i
ri
i
019.02.0
,
y i
Q14) Estimate the heat capacity of difference gases (O2 ,H2 , N2 , CO2 , CH4 ,C3H8 , C2H2) at different temperatures ( from 0 to 1000 oC step 10 ) .
, Cp J/mol K , T deg K.
component a b c d
O2 28.106 -3.680E-06 17.459E-06 -1.065E-08
H2 27.143 92.738E-04 -1.381E-05 76.451E-10
N2 31.150 -1.357E-02 26.796E-06 -1.168E-08
CO2 19.795 73.436E-03 -5.602E-05 17.153E-09
CH4 19.251 52.126E-03 11.974E-06 -1.132E-08
C3H8 -4.224 30.626E-02 -1.586E-04 32.146E-09
C2H2 26.821 75.781E-03 -5.007E-5 14.122E-09
Comp. Pc, atm Tc, K M, g/gmol
C4H8 36 408 56
C3H8 42 370 44 kgmol
atmlitR
.
.08256.0