(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.0' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 14555, 478]*) (*NotebookOutlinePosition[ 15325, 504]*) (* CellTagsIndexPosition[ 15281, 500]*) (*WindowFrame->Normal*) Notebook[{ Cell["Lab 4: Where's the Solution?", "Title", Background->RGBColor[0, 0, 1]], Cell["Math 421\tFall 2004", "Subtitle"], Cell[CellGroupData[{ Cell["Introduction", "Section"], Cell["\<\ Today, we'll look at a wee bit more constrained multivariable optimization...\ \ \>", "Text", TextAlignment->Left, TextJustification->0], Cell["...It's not a religion, it's just a technique...", "Commentary"] }, Closed]], Cell[CellGroupData[{ Cell["My Little Problem...", "Section"], Cell[TextData[{ "Let's say we want to solve the following constrained multivariable problem \ with a two inequality constraints. There are several steps that we go \ through, so let's see if we can understand ", StyleBox["why", FontWeight->"Bold"], " as we work on the logic below. Suppose we start with something like: \n\t\ minimize ", Cell[BoxData[ \(TraditionalForm\`f(x, y) = \((x - 1)\)\^2 + y\^2 + 1\)]], "\n\tsubject to: ", Cell[BoxData[ \(TraditionalForm\`g(x, y) = x - \(1\/4\) y\^2 \[GreaterEqual] 2\)]], "\n\t\t ", Cell[BoxData[ \(TraditionalForm\`h(x, y) = 2 x - y\^2 \[GreaterEqual] 3\)]], "\nWhat can we do with it all? Let's graph the contour diagram of the \ function ", StyleBox["f", FontSlant->"Italic"], ", and graph the ", StyleBox["equality", FontWeight->"Bold"], " form of the constraints:\n", Cell[BoxData[ \(TraditionalForm\`g(x, y) = \(x - \(1\/4\) y\^2 = 2\)\)]], " or ", Cell[BoxData[ \(TraditionalForm\`g(x, y) = \(x - \(1\/4\) y\^2 - 2 = 0\)\)]], " and ", Cell[BoxData[ \(TraditionalForm\`h(x, y) = \(2 x - y\^2 = 3\)\)]], " or ", Cell[BoxData[ \(TraditionalForm\`h(x, y) = \(2 x - y\^2 - 3 = 0\)\)]] }], "Text"], Cell[BoxData[{ \(Clear[f, g, h, x, y]\), "\[IndentingNewLine]", \(Off[General::"\"]\), "\[IndentingNewLine]", \(Off[General::"\"]\), "\[IndentingNewLine]", \(\(f[x_, y_] = \((x - 1)\)^2 + y^2 + 1;\)\), "\[IndentingNewLine]", \(\(g[x_, y_] = x - y^2/4 - 2;\)\), "\[IndentingNewLine]", \(\(h[x_, y_] = 2 x - y^2 - 3;\)\)}], "Input"], Cell[TextData[{ "The function CPLabel basically overlays an array of f[x,y]values on top of \ the standard ", StyleBox["Mathematica ", FontSlant->"Italic"], "ContourPlot command..." }], "Text"], Cell[BoxData[ \(?? ContourPlot\)], "Input"], Cell["Here we declare it...", "Text"], Cell[BoxData[ \(CPLabel[f_, xlimit_, \ ylimit_, \ xclabl_, \ yclabl_, \ opts___] := \n\t CompoundExpression[\ Clear[p1c, p1t], \n\t\tp1c = ContourPlot[f, xlimit, ylimit, opts, ContourShading -> False, DisplayFunction -> Identity], \ p1t = Table[Text[N[f, 2], {xlimit[\([1]\)], ylimit[\([1]\)]}], xclabl, yclabl], \n\t\tShow[{p1c, Graphics[p1t]}, \ DisplayFunction -> $DisplayFunction]]\)], "Input"], Cell[TextData[{ "... and here we use it...\n\t\tThe first argument is the function\n\t\tThe \ second argument set is the range of ", StyleBox["x", FontSlant->"Italic"], " values\n\t\tThe third argument set is the range of ", StyleBox["y", FontSlant->"Italic"], " values\n\t\tThe fourth argument set is the min, max, and step size for \ function values as you move across the ", StyleBox["x", FontSlant->"Italic"], "-axis\n\t\tThe fifth argument set is the min, max, and step size for \ function values as you move up and down the ", StyleBox["y", FontSlant->"Italic"], "-axis\n\t\t(the other arguments I've used below are just options on a \ standard ContourPlot command... " }], "Text"], Cell[BoxData[ \(\(fplot = CPLabel[f[x, y], {x, 0, 10}, {y, \(-5\), 5}, {x, 0, 10, 2}, {y, \(-5\), 5, 2}, Axes -> True, AxesLabel -> {"\", "\"}];\)\)], "Input"], Cell["\<\ We'll need to do some implicit plots... so load the package... \ \>", "Text"], Cell[BoxData[ \(Needs["\"]\)], "Input"], Cell["... remember how to use it?", "Text"], Cell[BoxData[ \(\(?ImplicitPlot\)\)], "Input"], Cell["Here is constraint g:", "Text"], Cell[BoxData[ \(\(gplot = ImplicitPlot[g[x, y] \[Equal] 0, {x, 0, 10}, {y, \(-5\), 5}, PlotStyle \[Rule] RGBColor[1, 0, 0]];\)\)], "Input"], Cell["Here is constraint h:", "Text"], Cell[BoxData[ \(\(hplot = ImplicitPlot[h[x, y] \[Equal] 0, {x, 0, 10}, {y, \(-5\), 5}, PlotStyle \[Rule] RGBColor[0, 0, 1]];\)\)], "Input"], Cell["Put it all together:", "Text"], Cell[BoxData[ \(\(Show[fplot, gplot, hplot];\)\)], "Input"], Cell[TextData[StyleBox["Question 1:\tWhere is the feasible region?", FontWeight->"Bold"]], "Text", Background->RGBColor[0, 1, 1]], Cell[TextData[{ "Let's see if there is a critical point in the feasible region!\nJust to be \ different, I won't declare the ", StyleBox["grad", FontWeight->"Bold"], " functions we've been using (feel free to use them if you want...)" }], "Text"], Cell[BoxData[ \(sol = Solve[{\[PartialD]\_x f[x, y] \[Equal] 0, \[PartialD]\_y\ f[x, y] \[Equal] 0}, {x, y}]\)], "Input"], Cell[BoxData[ \(soln1 = {sol[\([1, 1, 2]\)], sol[\([1, 2, 2]\)]}\)], "Input"], Cell[TextData[{ "Using ", StyleBox["ListPlot", FontWeight->"Bold"], ", ", "I can plot this point on the graphics I've created above..." }], "Text"], Cell[BoxData[ \(\(?ListPlot\)\)], "Input"], Cell[BoxData[ \(\(solnplot = ListPlot[{soln1}, PlotStyle \[Rule] PointSize[0.02]];\)\)], "Input"], Cell["... and then I can display it all!", "Text"], Cell[BoxData[ \(\(Show[fplot, gplot, hplot, solnplot];\)\)], "Input"], Cell[TextData[StyleBox["Question 2:\tSo... is the critical point in the \ feasible region?", FontWeight->"Bold"]], "Text", Background->RGBColor[0, 1, 1]], Cell[TextData[StyleBox["Question 3:\tIf not, what constraint(s) is/are \ violated?", FontWeight->"Bold"]], "Text", Background->RGBColor[0, 1, 1]], Cell[TextData[StyleBox["Question 4:\tCan we ever have an optimal solution \ (remember, we are minimizing!) that is strictly in the interior of the \ feasible region?", FontWeight->"Bold"]], "Text", Background->RGBColor[0, 1, 1]], Cell["What to do next?", "Subsubsection"], Cell["Let's form the LaGrangian equation:", "Text"], Cell[BoxData[ \(Leqn = f[x, y] - \[Lambda]1\ g[x, y] - \[Lambda]2\ h[x, y]\)], "Input"], Cell["\<\ ... and see if we have any critical points when both constraints are \ binding...\ \>", "Text"], Cell[BoxData[ \(soln = Solve[{\[PartialD]\_x Leqn \[Equal] 0, \[PartialD]\_y\ Leqn \[Equal] 0, g[x, y] \[Equal] 0, h[x, y] \[Equal] 0}, {x, y, \[Lambda]1, \[Lambda]2}] // N\)], "Input"], Cell[TextData[{ "Looks like we've got 2 of them! Let's strip off the ", StyleBox["x", FontSlant->"Italic"], " and ", StyleBox["y", FontSlant->"Italic"], " values:" }], "Text"], Cell[BoxData[{ \(bothsoln1 = {soln[\([1, 3, 2]\)], soln[\([1, 4, 2]\)]}\), "\[IndentingNewLine]", \(bothsoln2 = {soln[\([2, 3, 2]\)], soln[\([2, 4, 2]\)]}\)}], "Input"], Cell["What is the function value for each critical point?", "Text"], Cell[BoxData[ \(f[x, y] /. {soln[\([1, 3]\)], soln[\([1, 4]\)]}\)], "Input"], Cell["...and ", "Text"], Cell[BoxData[ \(f[x, y] /. {soln[\([2, 3]\)], soln[\([2, 4]\)]}\)], "Input"], Cell["Let's plot them!", "Text"], Cell[BoxData[ \(\(solnplot = ListPlot[{soln1, bothsoln1, bothsoln2}, PlotStyle \[Rule] PointSize[0.02]];\)\)], "Input"], Cell["... and look at it all together again!", "Text"], Cell[BoxData[ \(\(Show[fplot, gplot, hplot, solnplot];\)\)], "Input"], Cell[TextData[StyleBox["Question 5:\tCan we do better? ", FontWeight->"Bold"]], "Text", Background->RGBColor[0, 1, 1]], Cell["Probably so, or the notebook would end here, eh?", "Commentary", FontColor->RGBColor[0, 0, 1]], Cell[TextData[{ "I'd guess that maybe we should be looking at the red constraint... which \ one is it?\nahhh... ", StyleBox["g", FontSlant->"Italic"], " ... ok... then let's make it binding, and we'll ignore constraint ", StyleBox["h:", FontSlant->"Italic"] }], "Text"], Cell[BoxData[ \(gsoln = Solve[{\[PartialD]\_x Leqn \[Equal] 0, \[PartialD]\_y\ Leqn \[Equal] 0, g[x, y] \[Equal] 0, \[Lambda]2 == 0}, {x, y, \[Lambda]1, \[Lambda]2}] // N\)], "Input"], Cell[TextData[{ "Yowsah! imaginary solutions... I don't imagine we'll want to do much with \ them...\nLet's strip off the ", StyleBox["x", FontSlant->"Italic"], " and ", StyleBox["y", FontSlant->"Italic"], " values on the ", StyleBox["real", FontWeight->"Bold"], " one:" }], "Text"], Cell[BoxData[ \(gsoln1 = {gsoln[\([3, 2, 2]\)], gsoln[\([3, 4, 2]\)]}\)], "Input"], Cell["What is the function value at this critical point?", "Text"], Cell[BoxData[ \(f[x, y] /. {gsoln[\([3, 2]\)], gsoln[\([3, 4]\)]}\)], "Input"], Cell["Let's plot it!", "Text"], Cell[BoxData[ \(\(solnplot = ListPlot[{soln1, bothsoln1, bothsoln2, gsoln1}, PlotStyle \[Rule] PointSize[0.02]];\)\)], "Input"], Cell["... and look at it all together again!", "Text"], Cell[BoxData[ \(\(Show[fplot, gplot, hplot, solnplot];\)\)], "Input"], Cell[TextData[StyleBox["Question 6:\tCan we do any better? Why or why not?", FontWeight->"Bold"]], "Text", Background->RGBColor[0, 1, 1]], Cell[TextData[StyleBox["Question 7:\tOh yeah... one last thing... what are \ the values of the LaGrange multipliers at the optimal solution?", FontWeight->"Bold"]], "Text", Background->RGBColor[0, 1, 1]], Cell[TextData[{ StyleBox["Question 8:\tInterpret their meaning with respect to the \ constraints they go with (\[Lambda]1 goes with ", FontWeight->"Bold"], StyleBox["g", FontWeight->"Bold", FontSlant->"Italic"], StyleBox[" and \[Lambda]2 goes with ", FontWeight->"Bold"], StyleBox["h", FontWeight->"Bold", FontSlant->"Italic"], StyleBox[")... which constraint(s) is/are binding at the optimal solution? \ Are the associated values of the LaGrange multipliers positive, negative, or \ 0? Why? ", FontWeight->"Bold"] }], "Text", Background->RGBColor[0, 1, 1]] }, Closed]], Cell[CellGroupData[{ Cell["Inside the Shadow Cabinet...", "Section"], Cell[TextData[{ "OK... now you've walked through a problem... so let's try one on your \ own!\nLet's minimize the function ", StyleBox["f", FontSlant->"Italic"], " defined below, along with the following constraints:\n\tsubject to: ", Cell[BoxData[ \(TraditionalForm\`g(x, y) = x \[LessEqual] 3\)]], "\n\t\t ", Cell[BoxData[ \(TraditionalForm\`h(x, y) = y \[LessEqual] 3\)]] }], "Text"], Cell[BoxData[{ \(Clear[f, g, h, x, y]\), "\[IndentingNewLine]", \(\(f[x_, y_] = y \((x - 2)\)^3 - \((x\ y - 3)\)^3 \((x + 4)\)^2 + 5;\)\), "\[IndentingNewLine]", \(\(g[x_, y_] = x - 3;\)\), "\[IndentingNewLine]", \(\(h[x_, y_] = y - 3;\)\)}], "Input"], Cell[BoxData[ \(\(fplot = CPLabel[f[x, y], {x, \(-4\), 4}, {y, \(-4\), 4}, {x, \(-4\), 4, 2}, {y, \(-4\), 4, 2}, Axes -> True, AxesLabel -> {"\", "\"}, Contours \[Rule] 50];\)\)], "Input"], Cell["Go ahead!", "Text"], Cell[BoxData[ \(\ \)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["The Other Side...", "Section"], Cell[TextData[{ "So we've played with some \"easy\" functions.. let's step into the beyond. \ What if you have a function like this:\n\tmaximize ", Cell[BoxData[ \(TraditionalForm\`f(x, y) = \(sin(x\^2)\) \(cos(y)\) - \(cos(x)\) \(sin(y\^2)\)\)]], "\n\tsubject to: ", Cell[BoxData[ \(TraditionalForm\`g(x, y) = x\^2 + \(1\/4\) y\^2 \[GreaterEqual] 2\)]], "\n\t\t ", Cell[BoxData[ \(TraditionalForm\`h(x, y) = x + y \[LessEqual] 6\)]], "\nSee what you can do with this one... first, maybe try graphing \ everything to look at it... here's a start:" }], "Text"], Cell[BoxData[{ \(Clear[f, g, h, x, y]\), "\[IndentingNewLine]", \(\(f[x_, y_] = Sin[x^2] Cos[y] - Cos[x] Sin[y^2];\)\), "\[IndentingNewLine]", \(\(g[x_, y_] = x^2 + y^2/4 - 2;\)\), "\[IndentingNewLine]", \(\(h[x_, y_] = x + y - 6;\)\)}], "Input"], Cell[BoxData[ \(\(fplot = CPLabel[f[x, y], {x, \(-Pi\), Pi}, {y, \(-Pi\), Pi}, {x, \(-Pi\), Pi, Pi/3}, {y, \(-Pi\), Pi, Pi/3}, Axes -> True, AxesLabel -> {"\", "\"}];\)\)], "Input"], Cell["You do the rest... ", "Text"], Cell[BoxData[ \(\ \)], "Input"], Cell["\<\ Maybe we need something else to solve problems like these, eh?\ \>", "Commentary", FontColor->RGBColor[0, 0, 1]] }, Closed]] }, FrontEndVersion->"5.0 for Microsoft Windows", ScreenRectangle->{{0, 1280}, {0, 936}}, AutoGeneratedPackage->None, WindowToolbars->{"RulerBar", "EditBar"}, CellGrouping->Manual, WindowSize->{1272, 904}, WindowMargins->{{0, Automatic}, {Automatic, 0}}, StyleDefinitions -> "DemoText.nb" ] (******************************************************************* Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. *******************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[1754, 51, 79, 1, 70, "Title"], Cell[1836, 54, 39, 0, 41, "Subtitle"], Cell[CellGroupData[{ Cell[1900, 58, 31, 0, 54, "Section"], Cell[1934, 60, 150, 5, 29, "Text"], Cell[2087, 67, 70, 0, 23, "Commentary"] }, Closed]], Cell[CellGroupData[{ Cell[2194, 72, 39, 0, 34, "Section"], Cell[2236, 74, 1249, 35, 208, "Text"], Cell[3488, 111, 377, 6, 140, "Input"], Cell[3868, 119, 203, 6, 29, "Text"], Cell[4074, 127, 47, 1, 40, "Input"], Cell[4124, 130, 37, 0, 29, "Text"], Cell[4164, 132, 474, 8, 120, "Input"], Cell[4641, 142, 721, 18, 215, "Text"], Cell[5365, 162, 204, 4, 40, "Input"], Cell[5572, 168, 87, 2, 29, "Text"], Cell[5662, 172, 68, 1, 40, "Input"], Cell[5733, 175, 43, 0, 29, "Text"], Cell[5779, 177, 50, 1, 40, "Input"], Cell[5832, 180, 37, 0, 29, "Text"], Cell[5872, 182, 163, 3, 40, "Input"], Cell[6038, 187, 37, 0, 29, "Text"], Cell[6078, 189, 163, 3, 40, "Input"], Cell[6244, 194, 36, 0, 29, "Text"], Cell[6283, 196, 63, 1, 40, "Input"], Cell[6349, 199, 133, 2, 45, "Text"], Cell[6485, 203, 256, 6, 60, "Text"], Cell[6744, 211, 145, 3, 40, "Input"], Cell[6892, 216, 81, 1, 40, "Input"], Cell[6976, 219, 159, 6, 29, "Text"], Cell[7138, 227, 46, 1, 40, "Input"], Cell[7187, 230, 110, 2, 40, "Input"], Cell[7300, 234, 50, 0, 29, "Text"], Cell[7353, 236, 73, 1, 40, "Input"], Cell[7429, 239, 157, 3, 45, "Text"], Cell[7589, 244, 149, 3, 45, "Text"], Cell[7741, 249, 232, 4, 45, "Text"], Cell[7976, 255, 41, 0, 44, "Subsubsection"], Cell[8020, 257, 51, 0, 29, "Text"], Cell[8074, 259, 91, 1, 40, "Input"], Cell[8168, 262, 105, 3, 29, "Text"], Cell[8276, 267, 222, 4, 40, "Input"], Cell[8501, 273, 194, 8, 29, "Text"], Cell[8698, 283, 186, 3, 60, "Input"], Cell[8887, 288, 67, 0, 29, "Text"], Cell[8957, 290, 80, 1, 40, "Input"], Cell[9040, 293, 23, 0, 29, "Text"], Cell[9066, 295, 80, 1, 40, "Input"], Cell[9149, 298, 32, 0, 29, "Text"], Cell[9184, 300, 143, 3, 40, "Input"], Cell[9330, 305, 54, 0, 29, "Text"], Cell[9387, 307, 73, 1, 40, "Input"], Cell[9463, 310, 124, 2, 45, "Text"], Cell[9590, 314, 102, 1, 23, "Commentary"], Cell[9695, 317, 285, 8, 60, "Text"], Cell[9983, 327, 220, 4, 40, "Input"], Cell[10206, 333, 309, 12, 60, "Text"], Cell[10518, 347, 86, 1, 40, "Input"], Cell[10607, 350, 66, 0, 29, "Text"], Cell[10676, 352, 82, 1, 40, "Input"], Cell[10761, 355, 30, 0, 29, "Text"], Cell[10794, 357, 151, 3, 40, "Input"], Cell[10948, 362, 54, 0, 29, "Text"], Cell[11005, 364, 73, 1, 40, "Input"], Cell[11081, 367, 142, 2, 45, "Text"], Cell[11226, 371, 207, 3, 45, "Text"], Cell[11436, 376, 599, 17, 64, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[12072, 398, 47, 0, 34, "Section"], Cell[12122, 400, 420, 11, 122, "Text"], Cell[12545, 413, 290, 6, 100, "Input"], Cell[12838, 421, 233, 4, 40, "Input"], Cell[13074, 427, 25, 0, 29, "Text"], Cell[13102, 429, 35, 1, 40, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[13174, 435, 36, 0, 34, "Section"], Cell[13213, 437, 617, 15, 156, "Text"], Cell[13833, 454, 274, 5, 100, "Input"], Cell[14110, 461, 226, 4, 60, "Input"], Cell[14339, 467, 35, 0, 29, "Text"], Cell[14377, 469, 35, 1, 40, "Input"], Cell[14415, 472, 124, 3, 23, "Commentary"] }, Closed]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)