(************** Content-type: application/mathematica ************** 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[ 16354, 605]*) (*NotebookOutlinePosition[ 17122, 631]*) (* CellTagsIndexPosition[ 17078, 627]*) (*WindowFrame->Normal*) Notebook[{ Cell["Lab 5: Optimization", "Title", Background->RGBColor[0, 0, 1]], Cell[TextData[{ "Math 233\tFall 2001\t", StyleBox["Chapter 11, Section 7", FontFamily->"Arial", FontSize->16, FontWeight->"Bold"], " " }], "Subtitle", TextAlignment->Left, TextJustification->0], Cell[CellGroupData[{ Cell[TextData[StyleBox["Instructions!", FontColor->RGBColor[1, 0, 0]]], "Section"], Cell[TextData[{ "Work in ", StyleBox["groups of 2 or 3", FontWeight->"Bold"], " today, going through the entire notebook. If you have questions, ask me \ in class on Friday. Hand in answers to the question (complete sentences, \ please) on ", StyleBox["Monday (22 October)", FontWeight->"Bold"], ".\n\nAs a separate hand-in, you should ", StyleBox["each", FontWeight->"Bold"], " start on homework problems 1, 12, 27, 29, and 41 from section 11.7 \ (starting on page 944 of your text). These problems will also be due on ", StyleBox["Monday (22 October)", FontWeight->"Bold"], ".\n\nHave a great day, I'll see you Friday!" }], "Subsubtitle"] }, Closed]], Cell[CellGroupData[{ Cell["Introduction", "Section"], Cell[TextData[{ "OBJECTIVE: Learn to use ", StyleBox["Mathematica", FontSlant->"Italic"], " to:\n\t1. determine the critical points of a function\n\t2. analyze the \ critical points graphically\n\t3. determine whether the critical points are \ maximums, minimums, or saddle points\n\t4. visually look at finding maximums \ and minimums over bounded regions" }], "Text"], Cell[CellGroupData[{ Cell["Technology Guidelines", "Subsection", CellDingbat->"\[LightBulb]"], Cell[TextData[{ StyleBox["NOTE: If you have just finished a module, restart ", CellFrame->True, Background->None], StyleBox["Mathematica", CellFrame->True, FontSlant->"Italic", Background->None], StyleBox[" before executing a new module.\nTO OPEN CELLS, put your cursor \ on the right cell bracket and double click.", CellFrame->True, Background->None], "\nTO STOP AN EXECUTION\n\tSelect the ", StyleBox["Kernel", FontSlant->"Italic"], " pull-down menu and click on ", StyleBox["Abort Evaluation.\n", FontSlant->"Italic"], "ORDER OF EXECUTION\n\tExecute cells in the order given. Do not skip any \ Input cells within a given notebook.\nSAVING NOTEBOOKS\n\tYou can save \ anytime to any directory you choose, and it is wise to save often.\n\t\ However, before you do your final save, delete all your output by selecting \ the \n\t ", StyleBox["Delete All Output", FontSlant->"Italic"], " selection under the ", StyleBox["Kernel", FontSlant->"Italic"], " pull-down menu.\nEXPERIENCING MAJOR PROBLEMS\n\tSave if appropriate, and \ then shut down ", StyleBox["Mathematica", FontSlant->"Italic"], " and start it up again." }], "Text"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Part I: Finding Critical Points", "Section"], Cell[CellGroupData[{ Cell["Define the Gradient Function:", "Subsubsection"], Cell[TextData[{ "We can think of setting both partial derivatives equal to zero as the \ equivalent of setting the gradient of the function to 0. To make this \ easier, let's define a function called ", StyleBox["Grad", FontWeight->"Bold"], " that will find gradients for us!" }], "Text"], Cell["\<\ Note: I've defined this command with := rather than just an =... this \ postpones the assignment of the command until the function f (which occurs on \ both the right and left sides of the equation) has been defined.\ \>", "Commentary", FontColor->RGBColor[0, 0, 1]], Cell[BoxData[ \(grad[f_] := {\[PartialD]\_x\ f, \[PartialD]\_y\ f}\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Test it out!:", "Subsubsection"], Cell["Here is a simple function to test our gradient out on.", "Text"], Cell[BoxData[ \(f[x_, y_] = 5\ x\^2\ + \ 1\ x\ y\ + \ 5\ y\^2\)], "Input"], Cell["Let's find the gradient...", "Text"], Cell[BoxData[ \(fgrad[x_, y_] = grad[f[x, y]]\)], "Input"], Cell[TextData[{ "Notice that the argument for ", StyleBox["grad", FontWeight->"Bold"], " was f[x,y]... ", StyleBox["not", FontWeight->"Bold"], " f. Here' s why:" }], "Text"], Cell[BoxData[ \(grad[f]\)], "Input"], Cell["So... remember this if you are having troubles later!", "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Finding where the gradient is 0:", "Subsubsection"], Cell[TextData[{ "Now, let's find the critical points... there are a couple of ways to do \ this. We can use the ", StyleBox["Mathematica", FontSlant->"Italic"], " ", StyleBox["Solve", FontWeight->"Bold"], " command:" }], "Text"], Cell[BoxData[ \(\(?Solve\)\)], "Input"], Cell[BoxData[ \(Solve[grad[f[x, y]] \[Equal] {0, 0}, {x, y}]\)], "Input"], Cell[TextData[{ "Note here that since the gradient is a ", StyleBox["vector", FontWeight->"Bold"], ", I must set it equal to a ", StyleBox["vector", FontWeight->"Bold"], " - that's why we have {0,0} instead of 0. The {x,y} at the end of the \ command tells us the variables we are trying to solve for..." }], "Text"], Cell[TextData[{ "Another way to find critical points (which will work better in many \ instances) is the ", StyleBox["FindRoot ", FontWeight->"Bold"], "command. This uses ", StyleBox["numerical", FontWeight->"Bold"], " techniques to ", StyleBox["approximate", FontWeight->"Bold"], " where the function is 0 using \"seeds\" (or guesses) that you provide. \ Here is the syntax: ", StyleBox["FindRoot[{", FontWeight->"Bold"], "eqn1,eqn2", StyleBox["}, {x, xseed}, {y, yseed}]", FontWeight->"Bold"] }], "Text"], Cell[BoxData[ \(\(?FindRoot\)\)], "Input"], Cell[TextData[{ "Note: Remember that is the vector function that ", StyleBox["is", FontWeight->"Bold"], " the gradient of the function f that we defined above..." }], "Commentary", FontColor->RGBColor[0, 0, 1]], Cell[CellGroupData[{ Cell[BoxData[ StyleBox[\(FindRoot[fgrad[x, y] == {0, 0}, \ {x, \ 0.2}, \ {y, \ 0.2}]\), FontWeight->"Bold"]], "Input"], Cell["\<\ So the only critical point is (0,0). Cool... now we have 2 ways to do something! If one doesn't work; try the \ other!\ \>", "Text"] }, Open ]] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["You Try It: Part I", "Section"], Cell[TextData[{ "Find all the critical points for the function g[x,y] = ", Cell[BoxData[ \(TraditionalForm\`x\^2\/2 + 3 y\^3 + 9 y\^2 - 3 x\ y + 9 y - 9 x\)]], ". \nBe careful when typing xy that you don't create a new variable called \ 'xy' instead of the product (x)(y)..." }], "Text"], Cell[BoxData[{ RowBox[{\(Clear[g, x, y]\), " "}], "\n", RowBox[{ StyleBox[\(g[x_, y_]\), FontColor->RGBColor[1, 0, 0]], "="}]}], "Input"], Cell["What are the critical points?", "Text"], Cell[TextData[{ StyleBox["(Type your answer here)", FontSlant->"Italic"], "\n" }], "Commentary", Background->RGBColor[0, 1, 1]] }, Closed]], Cell[CellGroupData[{ Cell["Part II: Analyzing Critical Points", "Section"], Cell[CellGroupData[{ Cell["Graphical", "Subsubsection"], Cell["\<\ We can look at the graph of the function, and at the level curves to figure \ out what is happening:\ \>", "Text"], Cell[BoxData[ \(\(Plot3D[f[x, y], {x, \(-5\), 5}, {y, \(-5\), 5}, PlotPoints \[Rule] 20, AxesLabel \[Rule] {"\", "\", "\"}, BoxRatios \[Rule] {1, 1, 1}];\)\)], "Input"], Cell[TextData[{ "I think it is safe to say that the critical point at (0,0) is a minimum. \ In fact, it is a ", StyleBox["global minimum", FontWeight->"Bold"], ", meaning that there are no other minima having a smaller function value." }], "Text"], Cell[TextData[{ "Another interesting way to look at the function is to plot the ", StyleBox["contours", FontWeight->"Bold"], " and the ", StyleBox["gradient", FontWeight->"Bold"], ". You already know the ", StyleBox["ContourPlot", FontWeight->"Bold"], " command, but you don't know the ", StyleBox["PlotGradientField", FontWeight->"Bold"], " command (which needs the package ", StyleBox["Graphics'PlotField", FontWeight->"Bold"], " loaded before it can be used)." }], "Text"], Cell[BoxData[ \(\(?PlotGradientField\)\)], "Input"], Cell[TextData[{ "Go ahead and execute this cell... you will only need to execute it one \ time this ", StyleBox["Mathematica", FontSlant->"Italic"], " session. If you try to execute a ", StyleBox["PlotGradientField", FontWeight->"Bold"], " command before loading the ", StyleBox["PlotField", FontWeight->"Bold"], " graphics package, then ", StyleBox["Mathematica", FontSlant->"Italic"], " won't understand what you are trying to do and will think that you are \ defining a new function for it... So... if you can't get ", StyleBox["PlotGradientField", FontWeight->"Bold"], " to work, you should choose ", StyleBox["Quit Kernel -> Local", FontWeight->"Bold"], " under the ", StyleBox["Kernel", FontWeight->"Bold"], " pull down menu (and start over again)." }], "Text"], Cell[BoxData[ \(<< Graphics`PlotField`\)], "Input"], Cell["\<\ Now we are ready to create a contour plot and overlay the gradient plot on \ top of it!\ \>", "Text"], Cell[BoxData[ \(\(fconplot = ContourPlot[f[x, y], {x, \(-5\), 5}, {y, \(-5\), 5}, PlotPoints \[Rule] 20, AxesLabel \[Rule] {"\", "\"}, ContourShading \[Rule] False];\)\)], "Input"], Cell[BoxData[ \(\(fgradplot = PlotGradientField[ f[x, y], {x, \(-5\), 5}, {y, \(-5\), 5}];\)\)], "Input"], Cell[BoxData[ \(\(Show[fconplot, fgradplot];\)\)], "Input"], Cell["\<\ By looking at this combined graph, how can you tell that the critical point \ at (0,0) is a minimum? \ \>", "Text"], Cell[TextData[{ StyleBox["(Type your answer here)", FontSlant->"Italic"], "\n" }], "Commentary", Background->RGBColor[0, 1, 1]] }, Closed]], Cell[CellGroupData[{ Cell["Analytical", "Subsubsection"], Cell[TextData[{ "We can use the multivariable equivalent of the ", StyleBox["Second Derivative Test ", FontWeight->"Bold"], "to classify critical points. We'll look at the derivation next week, so \ here is the ", StyleBox["Quick and Dirty Guide", FontSlant->"Italic"], ":\nFirst, we define what is typically called ", StyleBox["Discriminant", FontWeight->"Bold"], ": D=", Cell[BoxData[ \(TraditionalForm\`\(\(f\_xx\)(x\_0, y\_0)\) \(\(f\_yy\)(x\_0, y\_0)\) - \((\(f\_xy\)(x\_0, y\_0))\)\^2\)]] }], "Text"], Cell[TextData[{ "Then, if we are testing the critical point ", Cell[BoxData[ \(TraditionalForm\`\((x\_0, y\_0)\)\)]], ", we have the following rules:\n\tLocal ", StyleBox["minimum", FontWeight->"Bold"], " if D>0 ", StyleBox["and", FontWeight->"Bold"], " ", Cell[BoxData[ \(TraditionalForm\`\(f\_xx\)(x\_0, y\_0) > 0\)]], "\n\tLocal ", StyleBox["maximum", FontWeight->"Bold"], " if D>0 ", StyleBox["and ", FontWeight->"Bold"], Cell[BoxData[ \(TraditionalForm\`\(f\_xx\)(x\_0, y\_0) < 0\)]], "\n\t", StyleBox["Saddle Point", FontWeight->"Bold"], " if D<0\n\t", StyleBox["Inconclusive test", FontWeight->"Bold"], " if D=0 - you could have a local maximum, local minimum, or a saddle \ point\nLet's test it out on our function (we'll find all the individual \ pieces, then put them together):" }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(fxx = \[PartialD]\_\(x, x\)f[x, y]\)], "Input"], Cell[BoxData[ \(fyy = \[PartialD]\_\(y, y\)f[x, y]\)], "Input"], Cell[BoxData[ \(fxy = \[PartialD]\_\(x, y\)f[x, y]\)], "Input"] }, Open ]], Cell[BoxData[ \(discrim = \((fxx)\) \((fyy)\) - \((fxy)\)\^2\)], "Input"], Cell[TextData[{ "Based upon the ", StyleBox["discriminant", FontWeight->"Bold"], "or ", StyleBox["second derivative test", FontWeight->"Bold"], ", what do we have", "?" }], "Text"], Cell[TextData[{ StyleBox["(Type your answer here)", FontSlant->"Italic"], "\n" }], "Commentary", Background->RGBColor[0, 1, 1]] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["You Try It: Part II", "Section"], Cell[CellGroupData[{ Cell["Graphical:", "Subsubsection"], Cell[TextData[{ "Go ahead and classify both critical points by graphing (try both the ", StyleBox["Plot3D ", FontWeight->"Bold"], "and ", StyleBox["PlotGradientField", FontWeight->"Bold"], " combined with ", StyleBox["ContourPlot", FontWeight->"Bold"], " approaches)... make sure to use plot ranges for x and y that encompass \ all critical points..." }], "Text"], Cell[BoxData[""], "Input"], Cell["How did each critical point classify?", "Text"], Cell[TextData[{ StyleBox["(Type your answer here)", FontSlant->"Italic"], "\n" }], "Commentary", Background->RGBColor[0, 1, 1]] }, Closed]], Cell[CellGroupData[{ Cell["Analytical:", "Subsubsection"], Cell[TextData[{ "Here, things work out a little differently than before, since the \ discriminant won't be a constant (it will change depending upon the values of \ your critical point ", Cell[BoxData[ \(TraditionalForm\`\((x\_0, y\_0)\)\)]], ".\nWhat is the ", StyleBox["discriminant", FontWeight->"Bold"], " for g[x,y]?", " " }], "Text"], Cell[TextData[{ StyleBox["(Type your answer here)", FontSlant->"Italic"], "\n" }], "Commentary", Background->RGBColor[0, 1, 1]], Cell["\<\ How did each critical point classify? Do your answers agree with your \ graphical approach?\ \>", "Text"], Cell[TextData[{ StyleBox["(Type your answer here)", FontSlant->"Italic"], "\n" }], "Commentary", Background->RGBColor[0, 1, 1]] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["A Nice Problem to Ponder... ", "Section"], Cell[TextData[{ "Find and classify all the critical points of the function:\tf(x, y) = ", Cell[BoxData[ RowBox[{\((1\/2 - x\^2 + y\^2)\), SuperscriptBox[ StyleBox["e", FontSlant->"Italic"], \((1\/2 - x\^2 - y\^2)\)]}]]], " on the interval (-2, 2) for x and (-2, 2) for y. \nUse whatever \ techniques you find appropriate. A hint - there aren't any flat spots on the \ graph, so if using that approach, make sure that you have a large value for \ PlotPoints... another hint: you should find about 5..." }], "Text"], Cell[TextData[{ StyleBox["(Type your answer here)", FontSlant->"Italic"], "\n" }], "Commentary", Background->RGBColor[0, 1, 1]] }, Closed]] }, FrontEndVersion->"4.1 for Microsoft Windows", ScreenRectangle->{{0, 767}, {0, 479}}, AutoGeneratedPackage->None, WindowToolbars->{"RulerBar", "EditBar"}, CellGrouping->Manual, WindowSize->{759, 452}, 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[1705, 50, 70, 1, 70, "Title"], Cell[1778, 53, 217, 9, 41, "Subtitle"], Cell[CellGroupData[{ Cell[2020, 66, 84, 1, 54, "Section"], Cell[2107, 69, 677, 17, 166, "Subsubtitle"] }, Closed]], Cell[CellGroupData[{ Cell[2821, 91, 31, 0, 34, "Section"], Cell[2855, 93, 383, 8, 153, "Text"], Cell[CellGroupData[{ Cell[3263, 105, 74, 1, 44, "Subsection"], Cell[3340, 108, 1206, 33, 370, "Text"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[4595, 147, 51, 0, 34, "Section"], Cell[CellGroupData[{ Cell[4671, 151, 54, 0, 44, "Subsubsection"], Cell[4728, 153, 299, 7, 48, "Text"], Cell[5030, 162, 278, 5, 36, "Commentary"], Cell[5311, 169, 83, 1, 40, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[5431, 175, 38, 0, 28, "Subsubsection"], Cell[5472, 177, 70, 0, 29, "Text"], Cell[5545, 179, 80, 1, 40, "Input"], Cell[5628, 182, 42, 0, 29, "Text"], Cell[5673, 184, 62, 1, 40, "Input"], Cell[5738, 187, 192, 8, 29, "Text"], Cell[5933, 197, 40, 1, 40, "Input"], Cell[5976, 200, 69, 0, 29, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[6082, 205, 57, 0, 28, "Subsubsection"], Cell[6142, 207, 247, 9, 29, "Text"], Cell[6392, 218, 43, 1, 40, "Input"], Cell[6438, 221, 77, 1, 40, "Input"], Cell[6518, 224, 335, 9, 48, "Text"], Cell[6856, 235, 549, 18, 48, "Text"], Cell[7408, 255, 46, 1, 40, "Input"], Cell[7457, 258, 222, 6, 23, "Commentary"], Cell[CellGroupData[{ Cell[7704, 268, 128, 2, 40, "Input"], Cell[7835, 272, 145, 4, 60, "Text"] }, Open ]] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[8041, 283, 37, 0, 34, "Section"], Cell[8081, 285, 313, 7, 66, "Text"], Cell[8397, 294, 160, 4, 60, "Input"], Cell[8560, 300, 45, 0, 29, "Text"], Cell[8608, 302, 137, 5, 50, "Commentary"] }, Closed]], Cell[CellGroupData[{ Cell[8782, 312, 53, 0, 34, "Section"], Cell[CellGroupData[{ Cell[8860, 316, 34, 0, 44, "Subsubsection"], Cell[8897, 318, 124, 3, 29, "Text"], Cell[9024, 323, 202, 3, 40, "Input"], Cell[9229, 328, 258, 6, 29, "Text"], Cell[9490, 336, 518, 17, 48, "Text"], Cell[10011, 355, 55, 1, 40, "Input"], Cell[10069, 358, 826, 25, 67, "Text"], Cell[10898, 385, 55, 1, 40, "Input"], Cell[10956, 388, 111, 3, 29, "Text"], Cell[11070, 393, 223, 4, 40, "Input"], Cell[11296, 399, 128, 3, 40, "Input"], Cell[11427, 404, 63, 1, 40, "Input"], Cell[11493, 407, 125, 3, 29, "Text"], Cell[11621, 412, 137, 5, 50, "Commentary"] }, Closed]], Cell[CellGroupData[{ Cell[11795, 422, 35, 0, 28, "Subsubsection"], Cell[11833, 424, 555, 15, 79, "Text"], Cell[12391, 441, 880, 30, 184, "Text"], Cell[CellGroupData[{ Cell[13296, 475, 67, 1, 40, "Input"], Cell[13366, 478, 67, 1, 40, "Input"], Cell[13436, 481, 67, 1, 40, "Input"] }, Open ]], Cell[13518, 485, 77, 1, 40, "Input"], Cell[13598, 488, 200, 9, 29, "Text"], Cell[13801, 499, 137, 5, 50, "Commentary"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[13987, 510, 38, 0, 34, "Section"], Cell[CellGroupData[{ Cell[14050, 514, 35, 0, 44, "Subsubsection"], Cell[14088, 516, 389, 12, 48, "Text"], Cell[14480, 530, 26, 0, 40, "Input"], Cell[14509, 532, 53, 0, 29, "Text"], Cell[14565, 534, 137, 5, 50, "Commentary"] }, Closed]], Cell[CellGroupData[{ Cell[14739, 544, 36, 0, 28, "Subsubsection"], Cell[14778, 546, 362, 11, 60, "Text"], Cell[15143, 559, 137, 5, 50, "Commentary"], Cell[15283, 566, 116, 3, 29, "Text"], Cell[15402, 571, 137, 5, 50, "Commentary"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[15588, 582, 47, 0, 34, "Section"], Cell[15638, 584, 560, 11, 86, "Text"], Cell[16201, 597, 137, 5, 50, "Commentary"] }, Closed]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)