Chap 12 Using Solver PDF
Chap 12 Using Solver PDF
Using Solver
Excel has two tools, Goal Seek and Solver, that can save a great deal of time with complex mathematics. From a practical point of view the simple tool Goal Seek is redundant. It has limited scope and is far outpaced by Solver. So why is it there? Simply because, being easier to use, it is less intimidating for the mathematically challenged. So we shall spend a brief time on it. Solver, which is leased by Microsoft from Frontline Systems Inc., was developed primarily for solving optimization (maximum and minimum) problems. However, it can also be used to solve equations, and that is where we shall start. You may wish to visit www.solver.com to learn more about this product and its variations. The site also has a tutorial for using the Excel Solver, but it concentrates on optimization problems; we will do more with Solver. In this chapter we will see examples where Solver is used (i) for equation solving, (ii) for curve fitting or regression analysis, and (iii) some simple optimization problems. Solver needs to be installed on your computer and loaded into Excel. It is likely this happened when Excel was installed. To check, open the Data tab and look in the Analysis group for a Solver icon. If you do not see it, use the Excel Help with the search word solver to get instructions on loading it. The Excel Help has nothing more about Solver, as Solver has its own Help facility.
Suppose you have an equation such as Exp(-x) - Sin(x) = a and you know (perhaps from making a simple plot) that this has a root such that a <=x <=1. You could set up a worksheet similar to Figure 12.1 (please ignore the Goal Seek dialogs for now), and by altering the value in AS and watching BS you could find whatvalue ofx makes the function zero. Think for a moment of what strategy you would adopt. You could confirm that there was a root within (0,1) by making AS first a then 1 and observing that f(x) changes sign. Next, you might next try the midpoint O.S and then 0.6. As the sign and magnitude of BS change, you would modify the direction and amount by which
you altered AS until BS was nearly a-or you got tired of the game ! Well, Goal Seek wo rks the same way. Let us see Goal Seek at work.
Figure 12.1 (a) On Sheetl of a new workbook, enter what you see in Figure 12.1 without the text box. The formula in BS is =EXP(-A5)5IN(A5). (b) Use the command Data / Data Tools / What-IfAnalysis to open the Goal Seek dialog. (c) Our formula is in B5, so this is the Set Cell. We want to make this 0, so that is what we type in the To Value box, The variable is the value in A5, so this is the By Changing Cell. When these have been entered, click the OK button. (d) Goal Seek now displays its Status dialog giving you the option to either accept what it has found or cancel the operation. Click OK. (e) Repeat steps (c) and (d) using different starting values (say 0,1, and 0.5). Note howyouhave to reenter the problem each time you call up Goal Seek. Note that the results vary slightly. Goal Seek quits when it has made a certain number of trials (iterations), when a certain time period has passed, or when two answers are within a certain range of each other (convergence limit). There is no way of changing these settings.
(f)
If your starting value is 2, Goal Seek will find another root.
Note that the To Value must be a number; it cannot be a cell reference. 50 if you want D5 to equal D6, then you will need a cell with =D5-D6, and this will be your SetCellwith as the To Value.
Make a quick plot and see if you understand why. (g) Save the workbook as Chap12.xlsx.
Figure 12.2
(a) Open Sheetl of Chap12.xlsx. In AS enter the value 1. (b) Use the command Data / Analysis / Solver to open the dialog shown in the top of Figure 12.2. You can see this is much more detailed than Goal Seek. (c) For this problem: The Set Target is B5, with Value Of selected and set to 0, and By Changing Cells is A5. Click the Solve button in the top right corner. (d) Solver finds an answer, and the Results dialog pops up. Note that you can accept the answer or return to the original values. The reports are relevant only for optimization problem, not for a Value Of problem. Click OK. (e) SetAS to 0 and try again. Note (i) that Solver remembers the problem and (ii) the results are more consistent. (f) Open Solver again but before you click Solve, open the Options dialog; see Figure 12.3. We shall not make any adjustments
214 A Guide to Microsoft Excel 2007 for Scientists and Engineers for this problem, but you may wish to use Solver's Help to learn a little about the first few optional settings. (g) Save the workbook.
Figure 12.3
If,in the last Exercise, you did look at Solver's Help and read about the Precision setting, you saw that it said: Controls the precision of solutions by using the numberyou enter to determine whether the value ofa constraint cell meets a target or satisfies a lower or upper bound. This might sound irrelevant to the problem, but it is not. With a starting value of 1 in AS and the default value of Precision at 0.000001 (that's five zeros after the decimal), Solver's answer made B5 avalue of-2.1E-08 (your result could differ slightly). But when Precision is set to 0.00000000001 (ten zeros after the decimal), the value was 1.8E-12. Higher precision leads to an answer closer to zero. This is because: when the Value O[model is used in Solver, it is treated as a constraint problem. Indeed, to solve the last problem, we could have cleared the Set Target Cell and enter a constraint in the Form $B$5 = O. This becomes very important when you want more than one cell to take on a certain value. Suppose your model requires all cells in D1:D10 to become zero. Ifwe insist on using the Value afmethod, we need a single cell, and so write =SUMSQ(Dl:DlO) in Dll and use it as the Set Target Cell. Of course, =SUM(D1:D10) would not work since cells with positive and negative values could sum to
zero. A far better way is to use the constraint setting ofD1:D10 = O. This is the method we use in the next exercise.
In solving the simple cubic x 3+8x2-9x-72=O, we will see how the constraints method works and gives superior results. The graph of this function (or simple factorization) shows the roots to be 3, -3, and -8. Solver, like Goal Seek, homes in on the root that is closest to the initial value (sometimes called the guess) without passing through a minimum or maximum of the function. So we will be careful with our starting values. In more complex cases, one needs to experiment to find the multiple roots.
12
Figure 12.4
(a) On Sheet2 ofChap12.xlsx, copy from Figure 12.4 the text and values in columns A and B. (b) The formula in CS is =B5"'3+8*B5"'2-9*B5-72, and this is copied down to C7. In C9 we have =SUMSQ(C5:C7). The entries in E:F are for the next exercise. We begin by using the "traditional" method of having a cell with a SUMSQ formula as our target cell. (c) Use Solver as in Exercise 2 with The Set Target as C9, Value Of selected and set to 0, and By Changing Cells as B5:B7. Click the Solve button in the top right corner. We get results that are reasonably close to the known roots. Now we will solve the same problem with no target cell but with a constraint. (d) Reenter 4, -4, and -8 in BS:B7. Open Solver and clear the Set Target box.
There is an analytical method for solving cubic equations. This is shown in the workbook CubicEqn.xlsx on the companion website.
(e) In the Subject to Constraints area, click the Add button to bring up the Add Constraints dialog; see Figure 12.4. Enter the constraint $C$5:$C$7 = O. You can type the range reference with or without the $ symbols, or use the pointing method. Click the OK button. The Add button here is used to add additional constraints.
(f)
(g) Save the workbook. The results are summarized in the table that follows. Solver with SUMSQ cell f(x) Initial Final 9.67E-05 4 3.0000015 -4 -3.0000044 0.000131 -10 -8.0000038 -0.00021 Solver with constraints f(x) Initial Final 4 3.0000000 0 -4 -3.0000000 0 -10 -8.0000000 1.73E-08 The first two Constraint results are integer values of 4 and -4; the third value is -7.99999999968599. Clearly, the constraint method gave superior results.
In Exercise 2 we saw that Solver remembers the last used settings. In Exercise 3 we had two Solver models so only the last one used is stored. We can save information that allows us to reconstruct a Solver model. (a) Return to Sheet2 of Chap12.xlsx and copy from Figure 12.4 the text in E3:F4. The terms SUMSQ and CONSTR are used to remind us of the features of the two models. (b) Set Solver up to use C9as the target cell. With E5 as the active cell, open Solver's Option dialog and click on Save Model. Solver highlights a 3-by-1 range; click OK. To load the model we shall need to know that three cells were used to solve it So mark these with borders/and color fills.
(c) Set Solver up to use the constraint and no target cell. With FS as the active cell, open Solver's Option dialog and click on Save Model. Solver highlights a S-by-1 range; click OK. Mark these with borders/and color fills. (d) Nowyou can switch from one model to the next using Options / Load Model and selecting with appropriate block of cells. (e) Save the workbook
In Chapter 4 we saw the use of Excel's matrix functions to solve systems of linear equations. Figures 12.5 and 12.6 show a worksheet and Solver dialog used to solve a system of nonlinear equations. The starting values for x and y were both 1. The answers are not perfect; they should be integer 2 and 3, but the precision of the method is generally acceptable for real-world problems.
Figure 12.5
Figure 12.6
In Chapter 7 we used various Excel functions (such as SLOPE, INTERCEPT, LINEST, and LOGEST) to fit experimental data to various mathematical models (linear, polynomial, exponential, etc.). We saw that the theory behind these fitting functions was based on the principle of minimizing the sum of the squares of the residuals. Solver was designed to perform maximization and minimization operations, and so lends itself to curve-fitting problems. To demonstrate this method, we will do a simple linear fit with some test data taken from the NISTwebsite (www.nist.gov). NIST offers many data sets, together with their fitting parameters to enable others to test their regression programs. We shall use the Norris data set. Figure 12.7 shows a worksheet used to fit the Norris data to y = mx + b. The Norris data set is shown in Figure 12.8.
Figure 12.7
The heading s, y, and yfit in the data set were used to name the columns of data. Cells BS and CS were named as m and b, respectively. The formula in each cell inyfit is =mx+b. Note how Excel lets us use x to refer to just a single cell in this formula. The formula In B3 is =SUMXMY2(y,yfit). This function conveniently generates the sum of the squares of the residuals. Cells B6:C6 have the formula =LINEST(y,x), while B7:C7 are copies of the values from the NIST website. With initial values of m and b as 1, the Solver model used The Set Target is C5,with Min box selected and By Changing Cells as B5:C5. While the LINEST results are much closer to the accepted NIST values, the Solver answers are quite acceptable.
F
Norris Data Set
G
y
yfit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
x
0.2 0.3 0.3 0.4 0.5 0.6 10.1 11.1 11.6 118.2 118.3 120.2 226.5 228.1 229.2 337.4 338.0 339.1 447.5 448.6 448.9 556.0 556.8 558.2 666.3 666.9 669.1 775.5 777.0 779.0 884.6 887.2 887.6 995.8 996.3 999.0
0.1 0.3 0.6 0.3 0.2 0.1 9.2 10.2 10.8 118.1 117.6 119.6 228.1 228.3 228.9 338.8 339.3 339.3 448.9 449.1 449.2 557.7 557.6 559.2 668.5 668.8 668.4 778.1 778.9 778.9 888 888 888.8 998 998.5 998.5
-0.1 0.0 0.0 0.1 0.2 0.3 9.9 10.9 11.4 118.2 118.3 120.2 226.7 228.3 229.4 337.9 338.5 339.6 448.2 449.3 449.6 556.9 557.7 559.1 667.4 668.0 670.3 776.9 778.4 780.4 886.2 888.8 889.2 997.6 998.1 1000.9
After
1580.69 0.253959 0.003654 40.11852 114867.2 yfit 40.12 40.12 40.12 40.15 40.31 41.06 44.00 53.89 82.19 150.81 290.84 528.99 860.77 1226.11 1515.68 1620.61 1497.53 1197.10 830.85 505.37 275.79 142.89 78.70 52.59 43.59 40.95 40.29 40.15 40.12 40.12 40.12
x
0.239 0.240 0.241 0.242 0.243 0.244 0.245 0.246 0.247 0.248 0.249 0.250 0.251 0.252 0.253 0.254 0.255 0.256 0.257 0.258 0.259 0.260 0.261 0.262 0.263 0.264 0.265 0.266 0.267 0.268 0.269
.12-
*
-32 -33 -34 35 -36 -37 -38 39 -40 -41
Figure 12.8
y 25 24 39 49 56 84 66 97 158 244 353 444 773 1196 1677 1654 1341 1173 933 550 220 101 97 39 26 11 16 10 13 8 5
Figure 12.9
Having demonstrated that this is a viable method of performing regression analysis, we will use it in some more challenging examples Figure 12.9 shows, inA10:B41, some experimental data that is to be fitted to a Gaussian curve. The function is given by:
Xi -
Yi
= hexp (= = = = = =
JL
-(J-
J -b )2
Yi
h
Xi
fl
a
b
where: the predicted value the peak height above the baseline the value of the independent variable the position of the maximum the standard deviation and the baseline offset
(a) On Sheet5 of Chap12 .xlsx, start a worksheet similar to that in Figure 12.9. Begin by entering all the text and values except the values in C4. (b) In C4:C7 use the same values as inA4:A7. Use B4:B7 to name the cells in C4:C7. We are going to vary the C4:C7 cells with Solver but will kept the A4:A7 values to remind us of our starting values. (c) The formula in en is =h*EXP(-(((All-mu)/sig)"2))+base. There may appear to be an extra pair of parentheses in this, but that is not the case; we need to allow for the fact that the negation operator has the highest priority. (d) Construct a chart of the data in A10:C41. This will resemble the chart in Figure 12.10 where the markers are they-values and the line the yfit-values . You may have been wondering where the starting values for the h. tnu, and sig parameters came from. The chart will answer this question. The height appears to be about 1600; the midpoint seems to be in the range 0.25 and 0.26 so we use 0.255 for mu. The starting value for sig is found by experimentation. Try 1 in C6 and see the effect onyfit. Now try 0.5 and again see the effect onyfit. You will find that 0.005 gets yfit to more or less fitthe y-values. The tails of the curve are notfar from zero, so a starting value of ofor b would be appropriate. So now we have reasonable starting
parameters. (e) To get ready for Solver we need a target cell holding the sum of the squares of the residuals. In C8 enter the formula =SUMXMY2(Bll:B41,Cll:C41).
(f)
Use Solver to complete the task. The target cell is C8, which we wish to minimize by changing C4:C7. The resulting values are shown in Figure 12.9, while Figure 12.10 shows before and after fitting plots.
After
1600
1400
1400
1200
0.230
0.240
0.250
0.260
0.270
0.280
0.230
0.240
0.250
0.260
0.270
0.280
Figure 12.10
Scenario: An open-top tank is to be made from a sheet of metal by Exercise 7: A bending and welding (Figure 12.11). The specifications are that Minimization Problem the volume is to be 1.0 m' using the minimum sheet area. You are to find the dimensions a and b.
a
Figure 12.11
The worksheet to solve this problem, together with the Solver dialog, are shown in Figure 12.12.
Figure 12.12
(a) On Sheet 7 ofChap12.xlsx, enter the text shown in the figure. Enter the values in B4, BS, and HS. (b) SelectA4:BS, then while holding! Ctrl] down, selectD4:E4 and G4:HS. Use Formula / Defined Names / Create from Selection to name the cells to the right of each text entry. (c) Enter these formulas: in E4 =(0"'2) + (4*0*b) and in H4 =0"'2*b. The parentheses in the first formula are just to improve its readability. (d) Set Solver up as shown in the figure and press the Solve button. The results should be 1.26 for a and 0.63 for b. (e) Save the workbook.
Sandbagger, Inc., processes sand to make semi-pure silica to sell to computer chip manufacturers at $SO/ton. The company has Plant A and Plant B,in different locations. PlantAcan process 4S0 tons/day atacostof$2S/ton, while PlantB does SSO tons/day for $20/ton. There are three suppliers: Alpha, Beta, and Gamma. Today, Alpha has 200 tons of sand; they want $10/ton plus shipping of $2/ton to PlantAor $2.50/ton to Plant B, Beta's figures are 300 tons at$9
plus $1 or $1.50 while Gamma has 400 tons at $8 plus $5 or $3 for shipping. Develop a business plan for Sandbagger's operation today. This is a typical Solver optimization problem. There are three groups of data to be processed; (i) the constants, (ii) the independent variables (called the decision variables), and (iii) the dependent variables leading to a problem objective function subjectto some constraints. Our constants relate to the two plants and the three suppliers. The independentvariables are how much sand from each supplier goes to each plant The dependent variables are the expenses and income, with the profit being the objective function. The constraints are the finite amount each supplier has and the processing limit of each plant With this in mind we plana worksheet with different areas for the three groups of data. The constraints are placed in the Solver dialog. Figure 12.13 shows our final worksheet (a) On Sheet 8 of Chap12.xlsx enter all the text shown in the figure. Enter the values shown in columns Band C.
Figure 12.13
224 A Guide to Microsoft Excel 2007 for Scientists and Engineers (b) Enter the values of 50 into G6:H8 as our starting values for Solver to work with. These are summed in row 9 and column I with formulas such as =5UM(G6:G8). (c) In G13 enter =G6*($C14+B20) and copy this across and down to fill G13:H16. Sum these values in column I and row 16. Clearly, 117 gives the total of all expenses. (d) Enter in G19 =I9*B5 (total income) and in G21 =G19-I17 (profit). This last item is our objective function.
Figure 12.14
(e) All that remains is to run Solver; the settings are shown in Figure 12.14. The maximized profit comes out as $15,375 using all available supplies. Save the workbook.
TK Solver"
One way to test results from a computer program is to set up the same problem in two applications. Some programmers use Excel to test results from a C# program. For the test to be valid, you must totally rethink the problem. It is no good just programming the same algorithm into the second application. We want to test both the algorithm and its implimentation in the computer application. A number of the problems in this book have been reworked in TK Solver. For many problems, this can be a delightfully easy application to use: you enter rules on one sheet and variables on the other. Then you tell TK Solver to find the unknown variable. Visitthe author's we bsite at people.stfx.cajbliengmejTKSolver to locate the files and a link for a free trial of TK Solver. This application, which is used in many Engineering schools, can also be interfaced with Excel to expand its capabilities.
Problems
1.
Download test data from www.nistgovforaGaussianfit Use the method in Exercise 6 to fit the data. How do your values compare with the accepted values? Redo Exercise 3 of Chapter 11 using Solver. *The vapor pressure (pO in torr) ofa pure liquid as a function of the absolute temperature can be expressed as: loglo(pO) = a - b / T. The total vapor pressure of a threecomponent mixture is given by: P = x1pt + X2P~ + X3P~ where Xi is the mole fraction of component i. Set up a worksheet to use with Solver to find the normal boiling point (the temperature at which P = 760 torr) of a three component mixture. For a working example, use the following data.
a 7.84125 8.08840 8.11404
b
X
2. 3.
's.
*A chemical plane uses the following procedure: Avolume V fe of solution of compound A having a concentration of 00 lb moles/ft" is allowed to react for t, hours; the vat is then emptied, cleaned and recharged for another cycle. It can be shown that the yield per unit time is given by:
. ld yze
o r = --"--------'-t, + t,
)
Va (1 - exp( -kt
Make a worksheet using Solver to find the values of t, that maximize the yield in each simulation shown in the following table. Test the statement that the value of t, for maximum yield is the solution to the equation:
t,
V
-In(trk + tk + 1) / k
10 0.1 1 0.5 20 0.1 1 0.5 10 0.2 1 0.5
=0
10 0.1 1 1
ao k
tc
S.
*What will be the x, y-coordinates for the top right corner of the rectangle in the accompanying figure such that it touches the curve 3y = 18 - 2x 2 and the area of the rectangle is maximized?
3y=18-2x2
Area A
0.5
1.5
2.5
3.5
6.
*A company has four sources of crude oil." Crude from each source can produce specific amounts of various products. Thus from column B in Figure 12.16 we see that crude A makes 60% gasoline, 20% heating oil, and so on. The company has a market for certain amounts of each product in a week (column G)and fixed supplies from each source (row 10). The profit per barrel is given in row 11. How many barrels of each type should be processed to maximize the profit?
Figure 12.16
7. *For a change of pace, solve this magazine puzzle. Which three-digit number, when you divide it by the sum of its digits, gives you the sum of its digits plus one? The Langmuir equation relates the amount of gas (5) absorbed on a surface to the pressure (p) of the gas.
8.
S = KSrnax l+Kp
Fit the data in the following table to find K and Smax'
9.
InProblem 15 of Chapter 9, this equation was used to find the surface area of a cylinder with a conical base.
2V + J[ r S = ---;:-
We can show by calculus that S is a minimum when the angle of the cone is given by 8 =cos 1 (2/ 3). Use Solver to confirm that we did the differentiation correctly. How close is Solver's value to the expected one? Can you improve on this? 10. Sutherland's equations can be used to derive the dynamic viscosity of an ideal gas as a function of temperature:
17 - 170 T + C
- Ta+C(~J% Ta
where 11 is the viscosity (Pa-s) at temperature T, 110 is the viscosity, T is the input temperature in Kelvin, To is the reference temperature, and Cis Sutherland's constant for the specified gas. The following table lists some measured viscosity values for air. Given that for air, 110 = 18.27 X 10. 6 Pa-s at 291.15K, find C for air.
t (0e)
11 (expt)
10 20 30 40 50 60 70 80 90 100 17.87 18.37 18.86 19.34 19.82 20.29 20.75 21.21 21.66 22.10
11. Refer to Problem 9 in Chapter 2. Make a new worksheet beginning with something similar Figure 12.17. Use Solver to find the n values that maximize the profit
Figure 12.17
Do not use the UDF you may have coded in Problem 7 of Chapter 9 but compute the ml values with an Excel function.