(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 4.2' 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[ 5302, 153]*) (*NotebookOutlinePosition[ 6005, 177]*) (* CellTagsIndexPosition[ 5961, 173]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["n-dimensional Newton's Method", "Title", FontSize->36], Cell["\<\ Here are our two functions. We want to solve f1=0,f2=0 simultaneously for x \ and y.\ \>", "Text"], Cell[BoxData[{ \(\(f1[x_, y_]\ = \ x + y + Cos[x\^2 + y\^2];\)\), "\[IndentingNewLine]", \(\(f2[x_, y_]\ = \ 2*x*y - Sin[x\^2 - y\^2];\)\)}], "Input"], Cell["\<\ Here's the vector function we're looking to set equal to zero. \ \>", "Text"], Cell[BoxData[ \(f[x_, y_]\ = \ {f1[x, y], f2[x, y]}\)], "Input"], Cell["Here's the Jacobian. ", "Text"], Cell[BoxData[ RowBox[{\(J[x_, y_]\), " ", "=", " ", RowBox[{"(", GridBox[{ {\(D[f1[x, y], x]\), \(D[f1[x, y], y]\)}, {\(D[f2[x, y], x]\), \(D[f2[x, y], y]\)} }], ")"}]}]], "Input"], Cell[TextData[{ "Here's a version of f that accepts as input a vector v. That will make \ the Newton's method code nicer (\"vector version\"). Notice \"Block\" to run \ a set of commands at once. Notice the := (\"delayed evaluation\") . That \ tells ", StyleBox["Mathematica", FontSlant->"Italic"], " not to try to compute anything until a specific v is plugged in. Right \ now it doesn't know what to make of v[[1]] and v[[2]], the first and second \ slots of the vector v." }], "Text"], Cell[BoxData[ \(fvec[v_]\ := \ f[\ v[\([1]\)], \ v[\([2]\)]\ ]\)], "Input"], Cell["\<\ Similarly, here's a version of J that accepts as input a vector v. \ \>", "Text"], Cell[BoxData[ \(Jvec[v_]\ := \ J[\ v[\([1]\)], \ v[\([2]\)]\ ]\)], "Input"], Cell[TextData[{ "Now let's run Newton's method. I use initial guess {x,y}={-1/2,0}. My \ stopping condition is that the length of f is less than ", Cell[BoxData[ \(TraditionalForm\`10\^\(-12\)\)]], ". Notice the use of LinearSolve to compute ", Cell[BoxData[ \(TraditionalForm\`J\^\(-1\)\)]], "f. " }], "Text"], Cell[BoxData[{ \(\(v\ = \ {\(-0.5\), 0.0};\)\), "\[IndentingNewLine]", \(\(Print[{"\<{x,y}\>", "\"}];\)\), "\[IndentingNewLine]", \(\(Print[v, "\<,\>", fvec[v]];\)\), "\[IndentingNewLine]", \(While[\@\(fvec[v] . fvec[v]\) > \ 10\^\(-12\), \[IndentingNewLine]v\ = \ v\ - \ LinearSolve[Jvec[v], fvec[v]]; \[IndentingNewLine]Print[ v, "\<,\>", fvec[v]]\[IndentingNewLine]]\), "\[IndentingNewLine]", \(\)}], "Input"], Cell["\<\ Where did I get that initial guess from? Well, a contour plot of the two \ functions, with only the zero contour plotted, gives useful information: \ \>", "Text"], Cell[BoxData[{ \(plot1 = ContourPlot[f1[x, y], {x, \(-3\), 3}, {y, \(-3\), 3}, Contours \[Rule] {0}, PlotPoints \[Rule] 50, ContourShading \[Rule] False]\), "\[IndentingNewLine]", \(plot2 = ContourPlot[f2[x, y], {x, \(-3\), 3}, {y, \(-3\), 3}, Contours \[Rule] {0}, PlotPoints \[Rule] 50, ContourShading \[Rule] False, ContourStyle \[Rule] RGBColor[1, 0, 0]]\), "\[IndentingNewLine]", \(Show[plot1, plot2]\)}], "Input"], Cell[BoxData[{ \(\(v\ = \ {0.5, \(-1.0\)};\)\), "\[IndentingNewLine]", \(\(Print[{"\<{x,y}\>", "\"}];\)\), "\[IndentingNewLine]", \(\(Print[v, "\<,\>", fvec[v]];\)\), "\[IndentingNewLine]", \(While[\@\(fvec[v] . fvec[v]\) > \ 10\^\(-12\), \[IndentingNewLine]v\ = \ v\ - \ LinearSolve[Jvec[v], fvec[v]]; \[IndentingNewLine]Print[ v, "\<,\>", fvec[v]]\[IndentingNewLine]]\), "\[IndentingNewLine]", \(\)}], "Input"] }, Open ]] }, FrontEndVersion->"4.2 for Microsoft Windows", ScreenRectangle->{{0, 1400}, {0, 967}}, WindowSize->{1300, 851}, WindowMargins->{{32, Automatic}, {Automatic, 29}}, Magnification->2, StyleDefinitions -> "PrimaryColor.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[CellGroupData[{ Cell[1776, 53, 62, 1, 152, "Title"], Cell[1841, 56, 109, 3, 85, "Text"], Cell[1953, 61, 170, 3, 140, "Input"], Cell[2126, 66, 88, 2, 85, "Text"], Cell[2217, 70, 69, 1, 87, "Input"], Cell[2289, 73, 38, 0, 85, "Text"], Cell[2330, 75, 227, 5, 123, "Input"], Cell[2560, 82, 502, 10, 273, "Text"], Cell[3065, 94, 80, 1, 87, "Input"], Cell[3148, 97, 92, 2, 85, "Text"], Cell[3243, 101, 80, 1, 87, "Input"], Cell[3326, 104, 335, 9, 132, "Text"], Cell[3664, 115, 474, 8, 467, "Input"], Cell[4141, 125, 173, 3, 132, "Text"], Cell[4317, 130, 492, 10, 444, "Input"], Cell[4812, 142, 474, 8, 467, "Input"] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)