Using the SOLVER tool of Excel to solve non

advertisement
Texas A&M University
Artie McFerrin Department of Chemical Engineering
Using the SOLVER tool of Excel to solve non-linear equations
CHEN 354: Fall 2008
Prof. Perla B. Balbuena
Excel contains a tool called the SOLVER that lets you maximize or minimize functions
subject to general constraints. This tool may be applied to solve a system of non-linear equations.
We will explain how it works solving the following example (Smith et al. Example 10.3 part d):
For the system methanol(1)/methyl acetate(2), the following equations provide a
reasonable correlation for the activity coefficients:
γ 1 = exp ( Ax22 )
γ 2 = exp ( Ax12 )
where
A = 2.771 − 0.00523T
In addition, the following Antoine equations provide vapor pressures:

P1Sat
3643.310 
= exp  16.59158 −

kPa
T K − 33.424 


P2Sat
2665.54 
= exp  14.25326 −

kPa
T K − 53.424 

Assuming the validity of
yi P = xi γ i Pi Sat
( i = 1, 2,… , N )
Calculate T and { xi } , for P = 101.33 kPa and y1 = 0.40 . Also, prepare T-x-y and x-y diagrams
at this pressure.
Solution
The solution basically consists in the following steps:
1) Write the equilibrium equation for each component, recognize that P and { yi } are
given. Therefore, the unknowns in the equations are T and { xi }
y1 P = x1γ 1 P1Sat
y2 P = x2γ 2 P2Sat
2) For the implementation in SOLVER, we rewrite these equations as
y1 P − x1γ 1 P1Sat = 0
y2 P − x2γ 2 P2Sat = 0
At the solution both equations are fulfilled. For any other values of T and { xi } the
previous equations are not zero, we will call these values the errors.
T.A. Rafael Callejas Tovar | jrc2267@chemail.tamu.edu
Page | 1
CHEN 354: Fall 2008
Prof. Perla B. Balbuena
0 At the solution
y1 P − x1γ 1 P1Sat = 
 Error1 Otherwise
0 At the solution
y2 P − x2γ 2 P2Sat = 
 Error1 Otherwise
Sat
and γ i we obtain the system of non-linear
3) Substituting the expressions for Pi
equations that will be solved with the SOLVER tool:

3643.310 
2
y1 P − x1 exp ( 2.771 − 0.00523T )(1 − x1 )  exp  16.59158 −
 = 0 ⇒ {Error1}


T K − 33.424 


2665.54 
(1 − y1 ) P − (1 − x1 ) exp ( 2.771 − 0.00523T )  x12 exp 14.25326 −
 = 0 ⇒ {Error2 }
T K − 53.424 

)
(
(
)
The unknowns are T and { xi } .
4) Provided initial approximations for the unknowns we can evaluate the total error as
2
{Total Error} = {Error1} + {Error2 }
2
Using the square of the individual errors will prevent to compensate the negative
deviations with the positive ones.
In order to implement the solution in Excel, we have to set up a worksheet that given the
initial approximations for the unknowns it evaluates the total error using the equations that we
want to solve. Then, we can use SOLVER to find the values of the unknowns that make
{Total Error} = 0 . The Excel worksheet is shown in the following page.
To open the SOLVER tool in EXCEL 2007 we go to the Data menu (you must have the
SOLVER tool enabled).
You have to provide the “Solver parameters” in the dialogue box. According to the EXCEL
worksheet, the objective function is located in K6 cell, whereas the unknowns are located in M6 and
N6. We want to find the value of zero for the total error, so we select the “Value of 0” option.
T.A. Rafael Callejas-Tovar
jrc2267@chemail.tamu.edu
Dew T calculation
A
1
2
B
C
D
A
B
C
3643.31
33.424
Methanol (1) 16.59158
Methyl
3 acetate (2)
CHEN 354
Prof. Perla B. Balbuena
Smith et al., Example 10.3
E
F
G
H
I
J
K
6
P/kPa
101.33
M
Objective cell
14.25326
2665.54
53.424
∑ ( Error ) + ( Error )
2
1
4
5
L
y1
y2
0.4
Psat1(T)/ Psat2(T)/
AMargules
kPa
kPa
γ1(T)
γ2(T)
0.6 64.62698 89.93762 1.062377 1.362837 1.252307
Error1
7.6E-05
Error2
-0.00024
2
2
Cells with the
initial guesses
that SOLVER
will change
x1
6.17E-08
N
T/K
0.460193 326.6966
7
8
Sat
1 1 1
0 At the solution
⇒ y1 P − x1γ 1 P1Sat = 
 Error1 Otherwise
Sat
2 2 2
0 At the solution
⇒ y2 P − x2γ 2 P2Sat = 
 Error2 Otherwise
9
10
y1 P = x γ P
11
12
13
14
15
16
y2 P = x γ P
Use of Excel Solver Tool to Solve Non-Linear Equations
TA: Rafael Callejas-Tovar jrc2267@chemail.tamu.edu
CHEN 354: Fall 2008
Prof. Perla B. Balbuena
We can manipulate the precision, tolerance, etc. in the “Solver Options”. In this case the
default values work well.
We are ready to solve the problem; we click on “Solve” in the “Solver parameters” dialogue
box. With the initial approximations x1 = 0.3 and T = 300 K we can converge to a solution.
Converging successfully to a solution depends on the initial approximation, in case of failure
change it and use SOLVER again.
T.A. Rafael Callejas-Tovar
jrc2267@chemail.tamu.edu
CHEN 354: Fall 2008
Prof. Perla B. Balbuena
Using a MACRO to call SOLVER recursively
Sometimes is required to solve a non-linear system for several sets of input values (e.g.
creating a T-x-y or x-y diagrams). In these cases, is very convenient to create a MACRO that calls
SOLVER recursively for each element of the set of input values. The following steps will guide you
through the creation of this MACRO using the Excel worksheet previously discussed.
1) Put in the K1 cell the number of times that SOLVER will be executed recursively. In this
example we will create a T-x-y and x-y diagrams with 21 points (from y1 = 0 to y1 = 1
with increments of 0.05 keeping P constant).
2) Copy Row 6 all the way down until Row 26. In this case there is an increment of 0.05 in
y1 for each row so you have to modify column B accordingly. The values provided to
cells M6 and N6 will be used as initial approximations to SOLVER for all the points.
3) Show the Developer tab in the ribbon (If not displayed). In Excel 2007 click on the Office
button > Excel Options > Popular > Check the “Show Developer tab in the Ribbon” box
T.A. Rafael Callejas-Tovar
jrc2267@chemail.tamu.edu
CHEN 354: Fall 2008
Prof. Perla B. Balbuena
4) Go to the Developer tab and click on the “Macros” button
5) Type a name for the macro and click on “Create”
6) In the Visual Basic window go to Tools > References and select SOLVER then OK
T.A. Rafael Callejas-Tovar
jrc2267@chemail.tamu.edu
CHEN 354: Fall 2008
Prof. Perla B. Balbuena
7) In the Visual Basic window type (or copy and paste) the following (be careful to avoid
syntax errors)
Sub Solver_Macro()
'
' Solver_Macro Macro
nPoints = Cells(1, 11)
For i = 1 To nPoints
SolverOK SetCell:=ActiveCell.Address, MaxMinVal:=3,
ValueOf:="0", _
ByChange:=ActiveCell.Offset(0, 2).Range("A1:B1")
SolverSolve UserFinish:=True
' Finish and keep the final results.
SolverFinish KeepFinal:=1
ActiveCell.Offset(1, 0).Range("A1").Select
Next
End Sub
Remarks: The lines starting with ' are comments. The macro works with relative
references corresponding to the worksheet we built. For example, the unknowns are
located two columns to the right of the objective cell, this is specified in the sentence:
ByChange:=ActiveCell.Offset(0, 2).Range("A1:B1").
8) Save and close the Visual Basic window.
9) Run the MACRO. In the worksheet select cell K6 (the objective cell of the first point).
10) Go to the Developer tab > Macros > Select the MACRO you just created and click on Run
T.A. Rafael Callejas-Tovar
jrc2267@chemail.tamu.edu
CHEN 354: Fall 2008
Prof. Perla B. Balbuena
11) If the initial approximations were reasonable good SOLVER will converge to a solution
for every point. If solver fails, put new approximations for the unconverged points and
return to step 9).
12) You have to save your worksheet as Macro-Enabled Workbook (.xlsm) in the “Save As.., “
dialogue box.
13) When you open a Excel worksheet with macros they are dissabled for security reasons.
Enable them whe opening a xlsm sheet.
T.A. Rafael Callejas-Tovar
jrc2267@chemail.tamu.edu
Dew T calculation
A
1
2
B
C
D
A
B
C
3643.31
33.424
Methanol (1) 16.59158
Methyl
3 acetate (2)
CHEN 354
Prof. Perla B. Balbuena
Smith et al., Example 10.3
E
F
G
H
I
J
K
points =
L
Objective cell
14.25326
2665.54
53.424
M
N
21
2
∑ ( Error ) + ( Error )
1
2
Cells with the initial
guesses that SOLVER
will change
2
4
5
P/kPa
y1
y2
Psat1(T)/ Psat2(T)/
AMargules
kPa
kPa
γ1(T)
Error1
γ2(T)
Error2
x1
T/K
6
101.33
0.00
1.00 74.46172 101.3296 1.044686
2.842502
1
-0.00011636
0.000426183
1.95171E-07
5.49754E-07
330.0792
7
101.33
0.05
0.95 72.22009 98.75681 1.048538
2.704304
1.000706
0.000219892
0.000577869
3.82285E-07
0.025940343
329.3427
8
101.33
0.10
0.90 70.13111 96.34693 1.052219
2.550768
1.003382
0.00037555
2.3406E-05
1.41586E-07
0.056642224
328.6389
9
101.33
0.15
0.85 68.23352 94.14726 1.055643
2.380532
1.009286
3.93982E-05
0.000536517
2.89403E-07
0.093574235
327.9842
10
101.33
0.20
0.80 66.58529
92.2282 1.058682
2.192841
1.020604
0.000482057
6.14704E-05
2.36158E-07
0.138794482
327.403
11
101.33
0.25
0.75 65.26693 90.68743 1.061159
1.988371
1.041262
0.000421285
0.000754857
7.4729E-07
0.195200154
326.9294
12
101.33
0.30
0.70 64.39696
13
101.33
0.35
0.65 64.12963 89.35402
89.6678 1.062817
14
101.33
0.40
0.60 64.62698 89.93762 1.062377
1.362837
15
101.33
0.45
0.55 65.95595 91.49335
1.220398
16
101.33
0.50
0.50 68.01026 93.88778 1.056051
1.13086
1.581345
0.000438334
6.03653E-05
1.95781E-07
0.658750674
327.9061
17
101.33
0.55
0.45 70.59227 96.87997 1.051398
1.078387
1.756807
0.0007972
-0.000156735
6.60094E-07
0.732086928
328.7958
329.7749
1.06333
1.05986
1.771631
1.078374
0.000594607
-0.000121087
3.68219E-07
0.266447848
326.6124
1.555025
1.143955
-0.000259484
0.000191441
1.03981E-07
0.355642087
326.5143
1.252307
7.6015E-05
-0.000236399
6.1663E-08
0.460192513
326.6966
1.405123
-3.38161E-06
0.000185494
3.44194E-08
0.56649377
327.1779
18
101.33
0.60
0.40 73.52866 100.2603 1.046277
1.047518
1.919221
-0.000350689
-0.00024289
1.81978E-07
0.789357288
19
101.33
0.65
0.35 76.69576 103.8806 1.040942
1.028857
2.06519
0.000430454
-0.000195587
2.23545E-07
0.834684404
330.795
20
101.33
0.70
0.30
1.017283
2.195163
-9.25665E-06
0.000100908
1.0268E-08
0.871362651
331.8282
80.0194 107.6525 1.035538
21
101.33
0.75
0.25 83.44828 111.5158 1.030157
1.010006
2.310741
-5.5258E-05
0.000238414
5.98945E-08
0.901692559
332.8572
22
101.33
0.80
0.20 86.94955 115.4326 1.024849
1.005437
2.413752
0.000532006
-0.00032655
3.89665E-07
0.92726325
333.8722
23
101.33
0.85
0.15 90.50186 119.3789 1.019643
1.002636
2.505935
0.000482878
-0.000132934
2.50842E-07
0.949191575
334.8676
24
101.33
0.90
0.10
94.0897 123.3376 1.014555
1.001022
2.588761
0.000263948
-0.000294941
1.56659E-07
0.968263197
335.8403
25
101.33
0.95
0.05
97.7016 127.2966 1.009594
1.000225
2.66351
0.000236628
-0.000309332
1.51679E-07
0.985056124
336.7889
26
101.33
1.00
0.00 101.3297
1
2.731253
0.000365141
-0.000317802
2.34326E-07
0.999999113
337.7127
131.248 1.004762
Use of Excel Solver Tool to Solve Non-Linear Equations
TA: Rafael Callejas-Tovar jrc2267@chemail.tamu.edu
Dew T calculation
O
CHEN 354
Prof. Perla B. Balbuena
Smith et al., Example 10.3
P
Q
R
S
T
U
V
W
X
Y
Z
AA
AB
AC
AD
1
2
3
4
5
T-x-y Diagram:
Methanol(1) - Methyl acetate(2)
6
7
8
x-y Diagram:
Methanol(1) - Methyl acetate(2)
340
1.00
9
0.90
338
10
11
0.80
336
12
15
16
17
18
19
334
332
Vapor
330
Liquid
0.60
0.50
0.40
0.30
328
0.20
20
21
y1, Methanol
14
0.70
Temperature/K
13
326
0.10
22
23
324
0.00
24
0.00 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00
25
x1, y1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
x1, Methanol
26
Use of Excel Solver Tool to Solve Non-Linear Equations
TA: Rafael Callejas-Tovar jrc2267@chemail.tamu.edu
Download