You are on page 1of 20

1

INTRODUCTION TO PERSONAL COMPUTERS FOR CHEMICAL


ENGINEERS
CHAPTER 1

COMPLEX CONSECUTIVE CALCULATIONS

Mordechai Shacham`
Department oI Chemical Engineering
Ben-Gurion University oI the Negev
Beer-Sheva 84105, Israel

Introduction
Engineers spend a lot oI time doing calculations. Calculations are required, Ior
example, Ior the design and operation oI chemical processes, assessing
proIitability oI a new product and diagnosing Iaulty operation oI a particular unit
or equipment. The calculations' results must be clearly documented where every
number must be accompanied by explanation Ior what it represents and what are
the units that used. The results oI the calculations must oIten be presented in
tabular or graphical Iorms. These tasks can be carried out most eIIectively using
soItware packages.
There are many soItware packages, some oI them may be better suited to a
particular task then others. In this course we will learn to use there such packages,
namely, POLYMATH
1
, the MicrosoIt Excel
2
spreadsheet and the MATLAB
3

program.

1
POLYMATH is copyrighted by M. Shacham, M. B. Cutlip and M. Elly (http://www.polymath-
soItware.com/ )
2
Excel is a trademark oI MicrosoIt Corporation (http://www.microsoIt.com)

3
MATLAB is a trademark oI The Math Works, Inc. (http://www.mathworks.com)
2
Table 1. Types oI Problems Encountered in the ChE Practice
PART
NO.
MATHEMATICAL MODEL PROCESS MODELING
EXAMPLE
1 Complex Consecutive Calculations Peng-Robinson Equation oI State
2 Iterations (Successive Substitution) Calculation oI the Flow Rate in a
Pipeline.
3 One Nonlinear Algebraic Equation Adiabatic Flame Temperature in
Natural Gas Combustion
4 System oI Nonlinear Algebraic Equations Water Distribution Network
5 Systems oI Linear Algebraic Equations Steady-State Material Balances on a
Separation Train
6 Simultaneous Ordinary DiIIerential Equations
with Known Initial Conditions
Heat Exchange in a Series oI
Agitated Tanks
7 Multiple Linear Regression Fitting Riedel's Equation to Vapor
Pressure Data
7 Polynomial Regression Fitting Polynomial to Heat Capacity
Data
7 Non-linear Regression Fitting Antoine Equation to Vapor
Pressure Data

In table 3 the types oI problems encountered in the chemical engineering practice
are shown. In this chapter the solution oI problems involving complex consecutive
calculations, using POLYMATH, Excel and MATLAB will be demonstrated.
Example 1.1: Isothermal Compression of Gas Using the Peng-Robinson Equation
of State.
One mol oI gaseous carbon dioxide is being compressed isothermally Irom the initial
volume oI J 25 liters to the Iinal volume oI V 0.03 liter at various temperatures.
(a) Calculate the pressure in the container at temperature oI T 305 K and
volumes oI 25, 0.1 and 0.04 liters by the Peng-Robinson and the ideal gas
equations oI state, using the nonlinear equation solver oI POLYMATH.
Calculate also the reduced pressure (P
R
) reduced temperature (T
R
) and
compressibility Iactor (:) at the same conditions. Compare the results with
generalized charts oI : versus T
R
and P
R
that can be Iound in various
textbooks.


3
(b) Carry out the same calculations using Excel Ior the temperature and volume
values listed in Table 1. Prepare two separate tables and plots oI the calculated
pressure versus T and J. One when the values are calculated using the Peng-
Robinson equation oI state and the other when using the ideal gas assumption.
Show the pressure values in the Tables with three decimal digits aIter the
decimal point. Prepare the plots in a logarithmic scale.
(c) Repeat part (b) using MATLAB. Prepare an additional table and an additional
plot oI compressibility Iactor versus T and P package. Use linear scale Ior
this plot.
Table 1-1 Volume and Temperature Values Ior Example 1.1
Molar Volume
(liters/g-mol) 25 20 15 10 5 1 0.8 0.6 0.4 0.2 0.1 0.08 0.06 0.04 0.03
Temperature (K) 305 335 400 520 610

Equations and Numerical Data
The Peng-Robinson (P-R) equation is usually written (Cutlip and Shacham, 1999
4
)
( )
( ) ( )
|
|

\
|
+ +

=
b J b b J J
T a
b J
RT
P
` ` ` `
(1-1)
where
C
C
P
RT
b 07780 . 0 = (1-2)
) ( 45724 . 0 ) (
2 2
T
P
T R
T a
C
C
D = (1-3)
| ]
2
5 . 0
) ) / ( 1 ( 1 ) (
C
T T k T + = D (1-4)
2
26992 . 0 54226 . 1 37464 . 0 Z Z + = k (1-5)


4
Cutlip, M. B., and M. Shacham, "Problem Solving In Chemical Engineering with Numerical
Methods," Prentice-Hall, Upper Saddle River, New-Jersey, 1999.

4
P pressure in atm
J
`
molar volume in liters/g-mol
T temperature in K
R gas constant (R 0.08206 (atmliter/g-molK))
T
c
critical temperature in K (304.2 K Ior CO
2
)
P
c
critical pressure in atm (72.9 atm Ior CO
2
)
the acentric Iactor (0.225 Ior CO
2
)
The compressibility Iactor (z) is deIined
J P
RT
:
`
= (1-6)
and the reduced pressure P
R
(P/P
c
) and the reduced temperature T
R
(T/T
c
).
Solution
Part (a). The solution oI the problem Ior one set oI speciIied values oI T and
J
`
involves introduction oI the numerical values oI the various variables and constants
into equations (1-1) to (1-6). This can be accomplished using the POLYMATH
nonlinear equation (NLE) solver program.
AIter opening the POLYMATH program the Iollowing options are presented:

Click on the NLE button to open the nonlinear equation entry window shown below
5

The option oI interest to us Ior solving the problem presented is the Add EE (explicit
equation or auxiliary equation) option. The additional options available in this
window will be discussed later. Clicking on the Add EE button opens the Iollowing
dialog box.

Enter equation (1-1) noting the Iollowing principles:
1. The name oI the variable to be calculated is entered on the leIt side oI the sign,
the equation to calculate its value on the right.
2. A variable name must start with an English letter and may contain English letters,
numbers and the underscore ( _ ) sign. Note that no special characters, Greek and
Hebrew letters, parentheses and arithmetic operators (such as , / etc.) are allowed.
Because oI that J should be used Ior molar volume instead oI J
`
and a should be used
instead oI a(T).
3. The multiplications sign (*) must be explicitly typed everywhere it is needed. For
division use the / operator and exponentiation use the operator.
6
4. Use only round parentheses: ( ). Limit the use oI square brackets: | ] only to cases
where you are specially instructed to use them. Make sure to use enough pairs oI
parentheses (especially when division is involved) to obtain the correct sequence oI
calculations.
5. For clarity oI the documentation enter the name oI the calculated variable and its
units in the Comment Iield.

Using the above principles equation (1-1) is typed in as shown in the next page.
The additional equations can be entered and the constant values can be speciIied
similarly. The Iollowing points need special attention.
1. The notation (T) must be replaced by alpha (Ior example) and must be
replaced by omega, because oI the variable naming rules mentioned earlier.


2. The square root sign ( ) is not accepted by POLYMATH. It must be
replaced by the intrinsic Iunction sqrt(), where the variable name (or the
number) must be put in between the parentheses. Another option is to use 0.5
Ior square root.
3. Note that as long as there are undeIined variables the traIIic light is red and the
system oI equations cannot be solved. A list oI undeIined variables appears at
the bottom oI the screen and a more detailed list can be obtained by clicking
on the yellow question mark. Existence oI undeIined variables may oIten be
result oI misspelling the name oI the variables (using the letter l instead oI the
number 1 or using the letter O instead oI zero 0, Ior example).
The complete set oI equations as entered into POLYMATH is shown below.
7


BeIore attempting to solve the system oI equations the Iile must be "saved". It is
recommended to use the variable naming rules oI POLYMATH to name Iiles also to
prevent unnecessary, unpredictable diIIiculties which may occasionally show up
otherwise.
AIter saving the Iile the problem is solved by clicking on the blue arrow. This brings
up the "solution report" window, a part oI is shown below in a slightly rearranged
Iorm.


8
It can be seen that POLYMATH rearranged the equations in a correct calculation
sequence, where every variable is deIined and a numerical value is assigned to it
beIore it is being used Ior calculation oI an additional variable.
It can be seen that the speciIied molar volume (J
`
27 liters/g-mol) and temperature
(T 305) represent conditions where the ideal gas equation oI state is valid. Thus
1 ~ : and . 1 ~ ~
iaeal
P P The pressure and the compressibility Iactor are calculated at
two additional volume values and the compressibility Iactor values are compared with
the values obtained Irom generalized :-charts (see Table 1-2). The good agreement oI
the results shows that the POLYMATH set oI equations is correct.
Table 1-2 Comparison oI Compressibility Factor Values From a Generalized Chart
with Values Obtained Using the Peng-Robinson Equation Ior T
r
1.
Volume (liters/g-mol) 25 0.1 0.04
Pr 0.013 1.02 7.99
z (Peng-Robinson) 0.995 0.297 0.93
z (Chart) 1 0.3 0.97

Part (b). AIter the correctness oI the POLYMATH equation set has been veriIied it
can be converted into a set oI Excel Iormulas in the Iollowing steps:
1. Copy the ordered explicit equations Irom the POLYMATH solution report.
2. Paste the equations into a column oI an Excel worksheet.
3. Copy the right hand side oI the equations into the adjacent cell and replace
the variable names by variable addresses. To ensure error Iree variable name
replacement, use the "pointing" option, thus, click on the cell whose address
should be entered aIter marking the original variable name.
4. Check the numerical value obtained in a particular cell by comparison to the
POLYMATH solution.
AIter the correct solution Ior one set oI parameters was obtained, the set oI equations
can be copied as many times as needed, or the "Table" option can be used to obtain
solutions Ior additional parameter sets. The use oI this procedure Ior solving Part b oI
the example discussed Iollows.
9

The Iigure above shows part oI the Excel worksheet where the ordered POLYMATH
equation set was copied into column B and the numbers or expressions on the right
hand side oI the equations were copied into column D starting with cell D6. The
Iigure shows the operation oI replacing the second omega in cell C12 by the address
oI omega (C9) by marking the name omega and clicking on cell C9. The complete set
oI Excel Iormulas is shown below.

Once the Excel Iormula Ior a particular equation is complete, its numerical value is
displayed. Comparing the numerical value with the POLYMATH solution enables
correcting the mistakes immediately, saving the time wasted on "debugging" when the
errors have already been propagated.
10
One complete set oI numerical values oI the various variables and constants and the
complete problem documentation in the Excel Worksheet are shown below.

The numerical results are identical to those obtained by POLYMATH, thus the
correctness oI the Excel Iormulas has been veriIied.
II you did not save your Excel Iile yet, this is a good time to save it. Use a meaningIul
name (do not retain the deIault book1.xls ) preIerably in English without using special
characters.
Once a well documented and veriIied Excel solution Ior one set oI parameter values is
available, you can proceed to the preparation oI the required tables and graphs oI the

11
pressure versus temperature and molar volume.. The tables can be very conveniently
prepared using the "Two-Input Data Table" option oI Excel. The preparation oI such a
Table is demonstrated in with reIerence to the Figure shown above..
The J values listed in Table 1 are entered in separate rows in column G and the T
values are entered into separate columns in the 5
th
row. The address oI the calculated
value oI the pressure (C17) is entered in the upper corner on the leIt side oI the table
(cell G5). Note that the headings entered in the 4
th
row are not required parts oI the
table but used as "Legend" in the graph to be prepared.
AIter entering the J and T values and the address oI the target result, the whole area
oI the table is selected and the Table option Irom the Data menu is chosen. The
address oI the parameter T is speciIied as the Row Input Cell, since the T values are
entered in a row, and the address oI the parameter J is speciIied as the Column Input
Cell. AIter clicking on the OK button, the Table is Iilled with the compressibility
Iactor values corresponding to the respective reduced temperature and pressure
values. To present the calculated pressure values with three decimal digits aIter the
decimal point, select the part oI the table that contains the pressure results and change
the Iormat oI the Cells to numbers with 3 Decimal aigits. The resultant table Iollows.

To plot the results included in this Table it should be marked exluding the row that
includes the temperature values. To accomplish that, mark the pertinent part oI the
12
Iorth row (cells G4 to L4) Iirst. AIter that mark the rest oI the Table (cells G6 to L20)
while holding down the Ctrl key. While the Table is marked, open the Chart hi:ara
and select the XY (Scatter) Chart Tvpe. Add a Chart Title and Titles to the X and Y
axes. At Iirst a very strange looking plot is obtained. This is because the Volume and
Pressure values change over several orders oI magnitude and logarithmic scale Ior the
graph should be used. To change to logarithmic scale click on the x axis with the right
button oI the mouse. Select Jalue (x) axis Format axis and mark Logarithmic
Scale. Repeat the same process Ior the v axis. The Iollowing plot is obtained.

Pressure vs VoIume - Peng Robinson Equation
0.1
1
10
100
1000
10000
100000
0.01 0.1 1 10 100
VoIume (L/g-moI)
P
r
e
s
s
u
r
e

(
a
t
m
)
T=305
T=335
T=400
T=520
T=610

The graph oI Ideal Gas Pressure versus Volume and Temperature is obtained
similarly. It should be pointed out that log(Pideal) versus log(V) under isothermal
conditions yields a straight line, as can be predicted Irom the ideal gas equation oI
state.

Part (c). The POLYMATH equation set can be used as the basis Ior a MATLAB
program. The conversion should be carried out in the Iollowing steps.
5. Copy the ordered explicit equations Irom the POLYMATH solution report.
6. Paste the equations into the MATLAB editor.
13
7. Delete the equation numbers and the brackets around them. Add the
comments in the same line with the equation, putting the percent () beIore
the comment.
8. Save the Iile (m-Iile) that you have created and run it. Check the numerical
value obtained Ior a particular variable by comparing it to the POLYMATH
solution.
9. Convert the Iile to a MATLAB Iunction. Prepare a main program to run this
Iunction with the various parameter values and to prepare the requested tables
and plots.
The use oI this procedure Ior solving Part b oI the example discussed Iollows.
First open the MATLAB command window and select New M-File Irom the File
dropdown menu. Carry out steps 1, 2 and 3 oI the procedure shown above. The
Iollowing display should be obtained.

Save the Iile. Once again use a meaningIul name, using English letters and numbers
without using special characters. A name as PR_eqs.m is adequate.
Run this Iile by selecting Run Irom the options under the Debug dropdown menu. The
results will show up in the MATLAB command window and they will include
variable name and numerical value Ior all the variables. The results are printed so that
the variable name and its numerical value appear in separate lines and there may be
one or more extra lines in between. This is the most elementary Iorm oI the
presentation oI the results, but it is good enough to veriIy the correctness oI the
equations by comparing the numerical values with the values.
14
To calculate the values oI P, P
iaeal
and : Ior diIIerent values oI J and T the script Iile
that contains the set oI equations must be converted to a function Iile and an
additional script Iile, which will act as a main program and will call the Iunction with
various J and T values, should be prepared. To convert the set oI equations into a
function a function statement should be added as the Iirst statement in the Iile.
function |P,z,Pideal]PRIun(T,V)
where function is a MATLAB keyword which indicates that this is a Iunction Iile, T
and V are the input arguments, names variables whose numerical values are passed
Irom the main program into the Iunction, P, z and Pideal are output arguments whose
numerical values are passed Irom the Iunction to the main program and PrIun is a
name selected by the user Ior the Iunction. The input arguments are separated by
commas and put inside round parentheses and the output arguments are put inside
square brackets. Note that the Iunction must be saved with the same name, thus as
PRIun.m. All the variables that are deIined inside the Iunction, except the input and
output arguments, are local variables thus they are not recognized outside oI the
Iunction.
To prevent printing the values oI all the variables semi-colons (;) must be added at the
end oI all the equations (beIore the sign oI the comment).
The script Iile (main program) Ior calculating P, : and P
iaeal
Ior T 305 and 15
diIIerent values oI V is shown below.


The program starts with commands that non speciIic to the problem at hand and are in
the category oI "good programming practice".
clear clears the workspace Irom any leItover variables or Iunctions Irom previous
runs
clc clears the command window
15
format compact - suppresses extra line-Ieeds
format short g prints numbers with the best oI Iixed or Iloating point Iormat with 5
digits
Note that two commands in the same line are separated by a comma or a semi-colon.
Additional inIormation about any oI the commands can be obtained by typing help
commana (help format, Ior example) in the command window.
In the second line the single value Ior T is deIined. In the 3
rd
line a row vector V_list
which contains 15 molar volume values is speciIied. Square brackets are used to
speciIy vectors and matrices, where the various numerical entries are separated by at
least one space. Semi-colon is put at the end oI the command in order not to print the
vector in the command window.
In the 3
rd
line a for statement is started. The purpose oI this statement is to repeat
statements a speciIic number oI times.
The general Iorm oI a for statement is:
for variable expr, statement, ..., statement ena
The values oI the expression are stored one at a time in the variable and then the
Iollowing statements, up to the ena, are executed. In the particular case shown the
variable / obtains the values 1, 2, 3 . up to 15 and the two statements in lines 5 and 6
are executed Ior all these values oI /.
The statement in line 5 is used solely Ior printing the current value oI J. In line 6 the
Iunction PRIun is called with the input arguments T and V_list(j). T contains the
unique value oI the temperature and V_list(j) contains the j
th
element oI the V_list
vector. There is no semi-colon at the end oI this statement so that the values oI the
return arguments P, : and P
iaeal
will be printed.
The screen copy oI a section oI MATLAB's command window shown in the next
page shows the results printed Ior the Iirst two values oI J. The value oI T is printed
once while the values oI V_pres (present value oI J), P, : and P
iaeal
are printed every
time the function PR_Iun is called.
16

AIter checking the results to veriIy that the main program Iunction connection
yields the correct results the main program should be extended so as to carry out the
calculations Ior the Iull set oI V and T values and to present the results in tabular and
graphic Iorms.

The Iigure above shows the part oI the main program that carries out the calculations
Ior the Iull set oI J and T values. Instead oI the single T value that was speciIied in
17
the preliminary version oI the main program, now a row vector, T_list containing Iive
temperature values is deIined in line 2. An additional, outer for statement is added in
line 4 and it is closed with an ena in line 8. This way the call to the PRIun will be
executed 155 75 times Ior diIIerent i and / (and V and T) values. The index oI the
internal Ior loop changes Iirst, thus Ior i1 (Ior example) the index / will change Irom
1 to 15 and only then i will be increased to 2. T_list(i) (thus the i
th
element oI the
T_list vector) and V_list(j) (the jth element oI the V_list vector) are used as input
arguments Ior PRIun. The calculated P, : and P
iaeal
values are stored in two
dimensional matrices, P, z and Pideal. Each one oI these matrices has 15 rows (one
Ior every J value) and 5 columns (one Ior every T value). The Iirst index inside the
parentheses reIers to the row number the second index to the column number.
In line 13 a new for loop, displaying Iive tables oI results Ior the 5 diIIerent
temperature values, starts. The Iunction aisp() is used to organize and display the
results. This Iunction displays arrays (matrices or vectors) without displaying the
name oI the array. It also displays strings. Text strings should be surrounded by single
quotes.
In line 14 an empty string is displayed in order to skip one line. In line 15 the text
"T(K) " Iollowed by the temperature value is displayed. The num2str() Iunction is
used to convert the numerical value oI the temperature into a text string. Such strings
are stored in vectors and in line 15 the vector containing the string "T(K) " and the
vector containing the text string oI the temperature value are concatenated to one
vector and displayed together.
In line 17 the column headings oI the table are displayed. In line 18 the result columns
associated with the temperature stored in the position T_list(i) (deIined as P(:,i),
Pideal(:,i), and z(:,i) )are concatenated to create one matrix named Res. This matrix is
displayed in line 19.
The pause command in line 20 causes the program to wait Ior a key-press beIore
continuing to display an additional table.
The Iirst table corresponding to the value oI T 305 K as displayed in the MATLAB
command window, is shown in the next page.

18


The commands used Ior plotting the compressibility Iactor versus the temperature and
the pressure are shown below. Note that the pressure is put on the x axis (horizontal
axis) the compressibility Iactor on the y axis (vertical axis) and the temperature is a
parameter.

The command plot(x,v,s) plots vector y versus vector x where the string s is used to
deIine the line type, symbol and color. The triples can be repeated to put several
diIIerent curves on the same plot. In lines 26 and 27 oI the MATLAB code shown
above separate curves oI z versus P are being plot Ior the diIIerent temperature values.
In the Iirst triplet the Iirst column oI the pressure vector (corresponding to T 305) is
put on the x axis, the Iirst column oI the : vector (corresponding to the same
temperature) is put on the v axis and the string '-' indicates that solid line should be
used to plot this curve. There are Iive such triplets are deIined Ior the Iive temperature
19
values. Note that the three dots (.) at the end oI line 26 are used to indicate that the
same command continues in the next line. The commands legena, title, xlabel and
vlabel are selI explanatory. The plot obtained using this set oI commands is shown
below.


In this plot the details at the low pressure range cannot be seen very well because the
pressure changes over several orders oI magnitude. To obtain a graph where the
details can be better distinguished the pressure axis scale must be changed to a
logarithmic scale. This is accomplished by using the semilogx command instead oI the
plot command. The two commands have the same syntax the only diIIerence is that
the x axis is plotted in logarithmic scale.
To plot log(P) versus log(J) and log(P
iaeal
) versus log(J) the command loglog is used
instead oI the command plot. The Iollowing script is used, Ior example, to plot
log(P
iaeal
) versus log(J)

The plot obtained is shown in the next page.

20

You might also like