Chap 12 Using Solver.pdf

Embed Size (px)

Citation preview

  • 7/30/2019 Chap 12 Using Solver.pdf

    1/17

    12Using Solve

    Excel has two tools, Goal Seek and Solver, that can save a gdeal of time with complex mathematics. From a practical poiview the simple tool Goal Seek is redundant. It has limited sand is far outpaced by Solver. Sowhy is it there? Simply becabeing easier to use, it is less intimidating for the mathematichallenged. So we shall spend a brief t ime on it.Solver, which is leased by Microsoft from Frontline Systemswas developed primarily for solving optimization (maximumminimum) problems. However, it can also be used to sequations, and that is where we shall start . You may wish towww.solver.com to learn more about this product andvariations. The site also has a tutorial for using the Excel Sobut it concentrates on optimization problems; we will do mwith Solver.In this chapter we will see examples where Solver is used (iequation solving, (ii) for curve fitting or regression analysis,(iii) some simple optimization problems.Solver needs to be installed on your computer and loadedExcel.It is likely thishappenedwhen Excelwas installed.To chopen the Data tab and look in the Analysis group for a SolverI fyou do not see it, use the Excel Help with the searchword sto get instructions on loading it. The Excel Help has nothing mabout Solver, as Solver has its own Help facility.

    Exercise 1:GoalSeek Suppose you have an equation such as Exp(-x) - Sin(x) = aandyou know (perhaps from making a simple plot) that this has aroot such that a

  • 7/30/2019 Chap 12 Using Solver.pdf

    2/17

    212 A Guide to Microsoft Excel 2007 for Scientists and Engineersyou altered AS until BSwas nearly a -o r you got tired of thegame !Well,GoalSeekworks the sameway. Let us see GoalSeekat work.

    Figure 12.1(a) On Sheetl of a new workbook, enter what you see in Figu

    12.1 without the text box. The formula in BS is =EXP(-A5IN(A5).

    Note that the To Valuemustbe a number; i t cannot be acell reference. 50 i f you wantD5 to equal D6, then you willneed a cell with =D5-D6, andthis will be your SetCellwithoas the To Value.

    (b) Use the commandData / Data Tools /What-IfAnalysis to opthe GoalSeek dialog.(c) Our formula is in B5, so this is the Se t Cell. We want to ma

    this 0, so that is what we type in the To Value box, Tvariable is the value in A5, so this is the By Changing CWhen these have been entered, click the OK button.

    (d) GoalSeek now displays its Status dialoggiving you the optito either accept what it has found or cancel the operatioClickOK.(e) Repeat steps (c) and (d) using different starting values (s0,1, and 0.5). Note howyouhave to reenter the problemeatime you call up Goal Seek.Note that the results vary slightly. Goal Seek quits when it hmade a certain number of trials (iterations), when a certain tiperiod has passed, or when two answers are within a certarange of each other (convergence limit). There is no waychanging these settings.(f) I f your starting value is 2, Goal Seek will find another roMake a quick plot and see if you understand why.(g) Save the workbook as Chap12.xlsx.

  • 7/30/2019 Chap 12 Using Solver.pdf

    3/17

    Exercise 2: Solver asRoot Finder

    Using SolverAs an introduction to Solver, we will use it to solve the sproblem as in Exercise 1.

    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 dshown in the top of Figure 12.2. You can see this is mmore detailed than Goal Seek.

    (c) For this problem: The Se t Target is B5,with Value Ofseleand set to 0, and By Changing Cells is A5. Click the Sbutton in the top right corner.

    (d) Solver finds an answer, and the Results dialog pops up.that you can accept the answer or return to the orivalues. The reports are relevant only for optimizaproblem, not for a Value Ofproblem. Click OK.

    (e) SetAS to 0 and try again. Note (i) that Solver rememberproblem and (ii) the results are more consistent.

    (f) Open Solver again butbeforeyou click Solve, open the Opdialog; see Figure 12.3. We shall not make any adjustm

  • 7/30/2019 Chap 12 Using Solver.pdf

    4/17

    214 A Guide to Microsoft Excel 2007for Scientists and Engineers

    Note that Solver has it s ownHelp feature.

    Solving Equationswith ConstraintsTHIS IS IMPORTANT: ManyExcel texts do not useSolvercorrectly. It is notnecessaryto havea Set Target Cell.Wecan find roots of equationsusing Changing Cells andConstraints. This method canmake the problem easier toset up and will often givebetter results.

    for this problem, but you may wish to use Solver's Helplearn a little about the first few optional settings.

    (g) Save the workbook.

    Figure 12.3

    If,in the lastExercise, you did look at Solver's Help and read abothe Precision setting, you saw that it said: Controls the precisionsolutions by using the numberyou enter to determine whether tvalue ofa constraint cellmeetsa target or satisfies a lower or uppbound. This might sound irrelevant to the problem, but it is noWith a startingvalue of 1 in AS and the default value of Precisiat 0.000001 (that's five zeros after the decimal), Solver's answmadeB5avalue of-2.1E-08 (your resultcould differ slightly). Bwhen Precision is set to 0.00000000001 (ten zeros after tdecimal), the value was 1.8E-12. Higher precision leads toanswer closer to zero.This is because: when the Value O[model is used in Solver, itreated as a constraint problem. Indeed, to solve the lproblem, we could have cleared the Set Target Cell and enteconstraint in the Form $B$5 = O.This becomes very importantwhen you want more than one cto take on a certainvalue. Suppose yourmodel requires all cellsD1:D10 to become zero. Ifwe insiston using the Value afmethowe need a single cell, and so write =SUMSQ(Dl:DlO) in Dll ause it as the Set Target Cell. Ofcourse, =SUM(D1:D10)would nwork since cells with positive and negative values could sum

  • 7/30/2019 Chap 12 Using Solver.pdf

    5/17

    Exercise 3: FindingMultiple Roots

    Using Solverzero. A far better way is to use the constraint setting ofD1:DO. This is the method we use in the next exercise.In solving the simple cubic x3+8 x2 - 9 x - 7 2=O , we will see howconstraints method works and gives superior results. The gof this function (or simple factorization) shows the roots to-3, and -8. Solver, like Goal Seek, homes in on the root thclosest to the initial value (sometimes called the guess) witpassing through a minimum or maximum of the function. Swill be careful with our starting values. In more complex cone needs to experiment to find the multiple roots.

    12 Figure 12.4(a) On Sheet2 ofChap12.xlsx, copy from Figure 12.4 the texvalues in columns A and B.

    There is an analyticalmethod for solving cubicequations. This is shown inthe workbook CubicEqn.xlsxon the companion website.

    (b) The formula in CS is =B5"'3+8*B5"'2-9*B5-72, and thcopied down to C7. In C9 we have =SUMSQ(C5:C7).entries in E:F are for the next exercise.

    We begin by using the "traditional" method of having a cellwSUMSQ formula as our target cell.(c) Use Solver as in Exercise 2 with TheSet Target as C9,Val

    selected and set to 0, and By Changing Cells as B5:B7.the Solve button in the top right corner. We get resultsare reasonably close to the known roots.

    Now we will solve the same problem with no target cell buta constraint.(d) Reenter 4, -4, and -8 in BS:B7.Open Solver and clear th

    Target box.

  • 7/30/2019 Chap 12 Using Solver.pdf

    6/17

    216 A Guide to Microsoft Exce/2007for Scientists and Engineers(e) In the Subject to Constraints area, click the Add button

    bring up the Add Constraints dialog; see Figure 12.4. Enthe constra int $C$5:$C$7 = O. You can type the ranreference with or without the $ symbols, or use the pointimethod. Clickthe OKbutton. The Add button here is usedadd additional constraints.

    (f) Use the Solve button to have Solver seek a solution.(g) Save the workbook.The results are summarized in the table that follows.

    Solver with SUMSQ cellInitial Final f(x)4 3.0000015 9.67E-05-4 -3.0000044 0.000131-10 -8.0000038 -0.00021

    Solver with constraintsInitial Final f(x)

    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; tthird value is -7.99999999968599. Clearly, the constraintmethgave superior results.

    Exercise 4: SavingSolverModels

    In Exercise 2we saw thatSolver remembers the lastused settinIn Exercise 3 we had two Solver models so only the last one usis stored. We can save information that allows us to reconstrucSolver model.(a) Return to Sheet2 of Chap12.xlsx and copy from Figure 1the text in E3:F4. The terms SUMSQ and CONSTR are us

    to remind us of the features of the two models.(b) Set Solver up to use C9as the target cell.With E5 as the actcell, open Solver's Option dialog and click on Save ModSolver highlights a 3-by-1 range; click OK. To load the modwe shall need to know that three cells were used to solveSomark these with borders/and color fills.

  • 7/30/2019 Chap 12 Using Solver.pdf

    7/17

    Exercise 5: Systems ofNonlinear Equations

    Using Solver(c) Se t Solver up to us e the constraint an d no target cell. Wias th e active cell, open Solver's Option dialog an d clic

    Save Model. Solver highlights a S-by-1 range; click OK. Mthese with borders/and color fills.

    (d) Nowyou cans wi tch f rom one model to t he n ex t using Op/ LoadModel an d selecting with appropriate block of ce

    (e) Save th e workbookIn Chapter 4 we s aw t he use of Excel's matrix functions to ssystems of linear equations. Figures 12.5 an d 12.6 showo rks h ee t a n d Solver dialog used to solve a system of nonliequations. Th e starting values for x an d y w ere bo th 1.answers ar e no t perfect; they should be integer 2 an d 3, b uprecision of t he m et ho d is generally acceptable for real-wproblems.

    Figure 12.5

    Figure 12.6

  • 7/30/2019 Chap 12 Using Solver.pdf

    8/17

    218 A Guide to Microsoft Exce/2007 for Scientists and EngineersCurve Fitting withSolver

    In Chapter 7 we used various Excel functions (such as SLOPINTERCEPT, LINEST, and LOGEST) to fit experimental datavarious mathematical models (linear, polynomial, exponentietc.). We saw that the theory behind these fitting functions wbased on the principle of minimizing the sum ofthe squares of tresiduals. Solver was designed to perform maximization aminimization operations, and so lends itself to curve-fittiproblems.To demonstrate this method, we will do a simple linear fit wsome testdata taken from the NISTwebsite (www.nist.gov). NIoffers many data sets, together with their fitting parametersenable others to test their regression programs. We shall use tNorris data set. Figure 12.7 shows a worksheet used to fit tNorris data to y =mx + b. The Norris data set is shown in Figu12.8.

    Figure 12.7

    The heading s, y, and yfit in the data set were used to name tcolumns of data. Cells BS and CS were named as m andrespectively. The formula in each cell inyfit is =mx+b. Note hExcel lets us use x to refer to just a single cell in this formula. Tformula In B3 is =SUMXMY2(y,yfit). This function conveniengeneratesthe sum of the squares of the residuals. CellsB6:C6hathe formula =LINEST(y,x), while B7:C7 are copies of the valufrom the NISTwebsite.With initial values of m and b as 1, the Solver model used The STarget is C5,with Min box selected andByChanging Cellsas B5:CWhile the LINEST results are much closer to the accepted NIvalues, the Solver answers are quite acceptable.

  • 7/30/2019 Chap 12 Using Solver.pdf

    9/17

    E F G1 Norris Data Set2 x y yfit3 0.2 0.1 -0.14 0.3 0.3 0.05 0.3 0.6 0.06 0.4 0.3 0.17 0.5 0.2 0.28 0.6 0.1 0.39 10.1 9.2 9.910 11.1 10.2 10.911 11.6 10.8 11.412 118.2 118.1 118.213 118.3 117.6 118.314 120.2 119.6 120.215 226.5 228.1 226.716 228.1 228.3 228.317 229.2 228.9 229.418 337.4 338.8 337.919 338.0 339.3 338.520 339.1 339.3 339.621 447.5 448.9 448.222 448.6 449.1 449.323 448.9 449.2 449.624 556.0 557.7 556.925 556.8 557.6 557.726 558.2 559.2 559.127 666.3 668.5 667.428 666.9 668.8 668.029 669.1 668.4 670.330 775.5 778.1 776.931 777.0 778.9 778.432 779.0 778.9 780.433 884.6 888 886.234 887.2 888 888.835 887.6 888.8 889.236 995.8 998 997.637 996.3 998.5 998.138 999.0 998.5 1000.9Figure 12.8

    UsingSolverA B C

    1 Gaussian Fit- 23 Before After4 1600 h 155 0.255 mu 0.256 0.005 sig 0.007 0 base 40.18 SSR 114~ 10 x y-11 0.239 25 4-12 0.240 24 4-13 0.241 39 4-14 0.242 49 4-15 0.243 56 4-16 0.244 84 4-17 0.245 66 4-18 0.246 97 5-19 0.247 158 8-.12- 0.248 244 121 0.249 353 29-22 0.250 444 52-23 0.251 773 86-24 0.252 1196 122-25 0.253 1677 151-26 0.254 1654 162-27 0.255 1341 149-28 0.256 1173 119-29 0.257 933 83-* 0.258 550 500.259 220 27-32 0.260 101 1-33 0.261 97 7-34 0.262 39 5-35 0.263 26 4-36 0.264 11 4-37 0.265 16 4-38 0.266 10 4-39 0.267 13 4-40 0.268 8 4-41 0.269 5 4Figure 12.9

  • 7/30/2019 Chap 12 Using Solver.pdf

    10/17

    220 A Guide to Microsoft Exce/2007 for Scientists and EngineersExercise 6: GaussianCurve Fit

    Having demonstrated that this is a viable method of performiregression analysis, we will use it in some more challengexamplesFigure 12.9 shows, inA10:B41, some experimental data that isbe fitted to a Gaussian curve. The function is given by:

    ( ( )2Ji - JLYi =hexp - - ( J - -bwhere:

    Yi = the predicted valueh = the peak height above the baselineXi = the value of the independent variablefl = the position of the maximuma = the standard deviation andb = the baseline offset

    (a) On Sheet5 of Chap12.xlsx,start aworksheet similar to thaFigure 12.9. Begin by entering all the text and values excthe values in C4.(b) In C4:C7 use the same values as inA4:A7. Use B4:B7 to na

    the cells in C4:C7. We are going to vary the C4:C7cells wSolver but will kept the A4:A7 values to remind us of ostarting values.

    (c) The formula in en is =h*EXP(-(((All-mu)/sig)"2))+basThere may appear to be an extra pair of parentheses in thbutthat is not the case; we need to allow for the fact that tnegation operator has the highest priority.

    (d) Construct a chart of the data in A10:C41. This will resembthe chart in Figure 12.10 where the markers are they-valuand the line the yfit-values .

    You may have been wondering where the starting values for th. tnu, and sig parameters came from. The chart will answer tquestion. The height appears to be about 1600; the midposeems to be in the range 0.25 and 0.26 so we use 0.255 for mu. Tstartingvalue for sig is found by experimentation. Try 1 in C6asee the effect onyfit. Now try 0.5 and again see the effect onyYou will find that 0.005 gets yfit to more or less fitthe y-valuThe tails of the curve are notfar from zero, so a starting valueofor bwould be appropriate. Sonowwe have reasonable starti

  • 7/30/2019 Chap 12 Using Solver.pdf

    11/17

    Using Solverparameters.(e) To get ready for Solver we need a target cell holding theof the squares of the residuals. In C8 enter the form=SUMXMY2(Bll:B41,Cll:C41).(f) Use Solver to complete the task. The target cell is C8, w

    we wish to minimize by changingC4:C7. The resultingvaare shown in Figure 12.9, while Figure 12.10 shows beand after fitting plots.

    (g) Save the workbook.Before After

    18001600140012001000800600400200

    18001600140012001000800600400200

    0.230 0.240 0.250 0.260 0.270 0.280

    Figure 12.100.230 0.240 0.250 0.260 0.270

    Exercise 7: AMinimization ProblemScenario: Anopen-top tank is to be made from a sheet of metbending and welding (Figure 12.11). The specifications arethe volume is to be 1.0 m' using the minimum sheet area. Yoto find the dimensions a and b.

    a

    a

    Figure 12.11

    The worksheet to solve this problem, together with the Sodialog, are shown in Figure 12.12.

  • 7/30/2019 Chap 12 Using Solver.pdf

    12/17

    222 A Guide to Microsoft Excel 2007for Scientists and Engineers

    Figure 12.12(a) On Sheet 7 ofChap12.xlsx, enter the text shown in the figuEnter the values in B4, BS,and HS.(b) SelectA4:BS, thenwhile holding!Ctrl] down, selectD4:E4 a

    G4:HS. UseFormula /DefinedNames /Create from Selectionname the cells to the right of each text entry.

    (c) Enter these formulas: in E4 =(0"'2) + (4*0*b) and in=0"'2*b. The parentheses in the first formula are justimprove its readability.

    (d) Set Solver up as shown in the figure and press the Sobutton. The results should be 1.26 for a and 0.63 for b.(e) Save the workbook.

    Exercise8: AnOptimization ProblemSandbagger, Inc., processes sand to make semi-pure silica to sto computer chip manufacturers at $SO/ton. The company hPlantAand PlantB,in different locations. PlantAcan process 4tons/day atacostof$2S/ton,while PlantB does SSO tons/day$20/ton.There are three suppliers: Alpha, Beta, and Gamma. Today, Alphas 200 tons of sand; theywant $10/ton plus shipping of $2/tto PlantAor $2.50/ton to PlantB,Beta's figures are 300 tons at

  • 7/30/2019 Chap 12 Using Solver.pdf

    13/17

  • 7/30/2019 Chap 12 Using Solver.pdf

    14/17

    224 A Guide to Microsoft Excel 2007 for Scientists and Engineers(b) Enter the values of 50 into G6:H8 as our starting valuesSolver toworkwith. These are summed in row 9 and columI with formulas such as =5UM(G6:G8).(c) In G13 enter =G6*($C14+B20) and copy this across and doto fill G13:H16. Sum these values in column I and rowClearly, 117 gives the total of all expenses.(d) Enter in G19 =I9*B5 (total income) and in G21 =G19-(profit). This last item is our objective function.

    Figure 12.14(e) All that remains is to run Solver; the settings are shownFigure 12.14. The maximized profit comes out as $15,3using all available supplies. Save the workbook.

    TKSolver" Oneway to test results from a computer program is to set up tsame problem in two applications. Some programmers use Exto test results from a C# program. For the test to be valid, ymust totally rethink the problem. It is no good just programmithe same algorithm into the second application. Wewant to tboth the algorithm and its implimentation in the compuapplication.Anumber of the problems in this book have been reworked inSolver. For many problems, this can be a delightfully eaapplication to use: you enter rules on one sheet and variablesthe other. Then you tell TK Solver to find the unknown variabVisitthe author'swebsite atpeople.stfx.cajbliengmejTKSolverlocate the files and a link for a free trial of TK Solver. Tapplication, which is used in many Engineering schools, can abe interfaced with Excel to expand its capabilities.

  • 7/30/2019 Chap 12 Using Solver.pdf

    15/17

    Problems

    's. Carnahan and J. O.Wilkes, Digital Computingand Numerical Methods,Wiley, New York, 1973(page 435).

    Using Solver1. Download testdata from www.nistgovforaGaussianfit

    the method in Exercise 6 to fit the data. How do your vacompare with the accepted values?2. Redo Exercise 3 of Chapter 11 using Solver.3. *The vapor pressure (pO in torr) ofa pure liquid as a funof the absolute temperature can be expressed

    loglo(pO) = a - b / T. The total vapor pressure of a thcomponentmixture isgiven by:P = x1pt + X 2 P ~ + X 3 P ~ wXi is the mole fraction of component i. Set up a worksheuse with Solver to find the normal boiling pointtemperature at which P = 760 torr) of a three compomixture. For a working example, use the following data.

    a b XBenzene 7.84125 1750 0.5Toluene 8.08840 1985 0.3

    Ethyl Benzene 8.11404 2129 0.24. *Achemical plane uses the following procedure: Avolu

    fe of solution of compoundAhaving a concentration ofmoles/f t" is al lowed to react for t, hours; the vat isemptied, cleaned and recharged for another cycle. It cashown that the yield per unit time is given by:

    . ld Vao(1 - exp( -kt r )yze = --"--------'--t, + t,Make a worksheet using Solver to find the values of t,maximize the yield in each simulation shown in the follotable. Test the statement that the value of t, for maximyield is the solution to the equation:

    t, -In(trk + tk + 1) / k = 0V 10 20 10 10 10ao 0.1 0.1 0.2 0.1 0.1k 1 1 1 0.5 1t c 0.5 0.5 0.5 0.5 1

    S. *Whatwill be the x, y-coordinates for the top right cornthe rectangle in the accompanying figure such that it touthe curve 3y = 18 - 2x 2 and the area of the rectangmaximized?

  • 7/30/2019 Chap 12 Using Solver.pdf

    16/17

    226 A Guide to Microsoft Exce/2007 for Scientists and Engineers

    3y=18-2x2

    Area A

    0.5 1.5 2.5 3.5

    2V. G. Jensen and G. V.Jeffreys, MathematicalMethods in ChemicalEngineering 2nd ed.,Academic Press,SanDiego,1977, (page 570).

    6. *Acompany has four sources of crude oil." Crude from easource can produce specific amounts of various producThus from column B in Figure 12.16 we see that crudemakes 60% gasoline, 20% heating oil, and so on. Tcompany has amarket for certainamounts ofeach producaweek (column G)and fixed supplies from each source (r10). The profit pe r barrel is given in row 11. How mabarrels of each type should be processed to maximize tprofit?

    Figure 12.167. *For a change of pace, solve this magazine puzzle. Whithree-digit number, when you divide it by the sum of

    digits, gives you the sum of its digits plus one?8. The Langmuir equation relates the amount of gas

    absorbed on a surface to the pressure (p) of the gas.S = KSrnax

    l+KpFit the data in the following table to find K and Smax'

  • 7/30/2019 Chap 12 Using Solver.pdf

    17/17

    P5

    4.729.29

    18.4518.02

    48.0125.08

    Using Solver79.34 162.31 253.029.86 38.45 43.4

    t (0e)11 (expt)

    9. InProblem 15 of Chapter 9, this equationwas used to finsurface area of a cylinderwith a conical base.2V 2( 2 )S = ---;:-+ J [ r esce- "3 cote

    We can show by calculus thatS is a minimum when the aof the cone is given by 8 =cos1(2/ 3). Use Solver to conthatwe did the differentiationcorrectly. How close is Solvalue to the expected one? Can you improve on this?

    10. Sutherland's equations can be used to derive the dynviscosity of an ideal gas as a function of temperature:

    - T a + C ( ~ J %17 - 170 T +C Tawhere 11 is the viscosity (Pa-s) at temperature T, 110 isviscosity, T is the input temperature in Kelvin, To isreference temperature, andCis Sutherland's constant fospecified gas. The following table lists some measviscosity values for air. Given that for air, 110 = 18.27 XPa-s at 291.15K, find Cfor air.

    10 20 30 40 50 60 70 80 90 117.87 18.37 18.86 19.34 19.82 20.29 20.75 21.21 21.66 211. Refer to Problem 9 in Chapter 2. Make a new worksbeginningwith something similar Figure 12.17. Use Solvfind the n values that maximize the profit

    Figure 12.17Do not use the UDFyou may have coded in Problem