(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of 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[ 6171, 176]*) (*NotebookOutlinePosition[ 6849, 200]*) (* CellTagsIndexPosition[ 6805, 196]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["The Runge -Kutta Method", "Subsection"], Cell[BoxData[{ FormBox[ \(As\ we' ve\ noted\ before, \ Euler' s\ method\ is\ \ a\ fairly\ intuitive\ but\ not\), TextForm], FormBox[ \(terribly\ efficient\ means\ for\ obtaining\ numerical\ approximations\ for\), TextForm], FormBox[ RowBox[{ FormBox[\(the\ solution\ of\ an\ initial\ value\ problem\), "TextForm"], "\n"}], TextForm], FormBox[ RowBox[{ FormBox[\(\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y' \((x)\)\ = \ f \((x, y)\), \ \ \ y \((x\_0)\)\ = \ y\_0\), "TextForm"], "\n"}], TextForm], FormBox[ \(The\ Runge - Kutta\ method\ is\ a\ much\ more\ efficient\ \((and\ much\ less\ \nintuitive)\)\ numerical\ method . \ \ The\ following\ routine\ generates\ an\), TextForm], FormBox[ \(approximate\ solution\ for\ the\ above\ initial\ value\ problem\ using \), TextForm], FormBox[ \(Runge - Kutta . \ \ \ The\ syntax\ is\ the\ same\ as\ it\ was\ for\ Euler' s\ method\), TextForm], FormBox[ \(in\ the\ demonstration\ from\ Section\ \(8.1 . \)\), TextForm]}], "Text"], Cell[BoxData[ \(RKMethod[f_, {x0_, y0_}, h_, n_]\ := \n\t\n\ \ \ \ \ Module[\n\t\t\ \ \ \ \ \ \ {RKStep}, \n\t\t\n\t\t\ \ \ \ \ \ \ \ RKStep[{x_, y_}]\ := \ \n\t\t\t\ \ \ \ \ \ Module[{k1, k2, k3, k4}, \n \t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ k1\ = \ f[x, y]; \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ k2\ = \ f[x + h/2, \ y + \ k1*h/2]; \n \t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ k3\ = \ f[x + h/2, y + \ k2*h/2]; \n\t\t\t\t\t\t\t\t\t\t\ \ k4\ \ = \ f[x + h, y + k3*h]; \n \t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ N[{x + h, y + h*\((k1\ + \ 2*k2\ + \ 2*k3\ + \ k4)\)/6}, 12]\n \t\t\t\ \ \ \ \ ]; \n\t\t\n\t\t\ \ \ \ \ \ \ \ Interpolation[\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NestList[RKStep, {x0, y0}, n], \n\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ InterpolationOrder\ -> \ 1\n\t\t\t\ \ \ \ \ ]\n\t\ \ \ \ \ ]\)], "Input"], Cell[BoxData[{ \(TextForm\`As\ before, \ we' ll\ try\ out\ our\ method\ on\ the\ initial\ value\ problem\), \(TextForm\`\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y' \((x)\)\ = \ y \((x)\), \ \ \ \ \ \ \ \ y \((0)\)\ = \ 1\), \(TextForm \`and\ compare\ the\ results\ with\ the\ exact\ solution\ y \((x)\)\ = \ \(e\^x . \)\ \ \ \ \)}], "Text"], Cell[BoxData[ \(f[x_, y_]\ = \ y\)], "Input"], Cell["\<\ With a ridiculously large step size h=1, we obtain the following approximation of e.\ \>", "Text"], Cell[BoxData[ \(approx\ = \ RKMethod[f, {0, 1}, 1, 1]\)], "Input"], Cell[BoxData[ \(approx[1]\)], "Input"], Cell["\<\ That's two digits of accuracy already! If we cut the step size by \ a factor of ten, we obtain\ \>", "Text"], Cell[BoxData[ \(approx\ = \ RKMethod[f, {0, 1}, .1, 10]\)], "Input"], Cell[BoxData[ \(approx[1]\)], "Input"], Cell[BoxData[{ \(TextForm \`which\ is\ accurate\ to\ \((at\ least)\)\ 6\ \(places!\)\ \ To\ obtain\ higher\ accuracy, \n we' d\ have\ to\ also\ specify\ more\ accuracy\ for\ the\ "N"\ command\ used\), \(TextForm \`in\ defining\ RKMethod . \ \ \ It\ turns\ out\ that\ local\ truncation\ error\ is\), \(TextForm \`proportional\ to\ \(h\^5\) for\ this\ method\ and\ global\ truncation\ error\ is\), \(TextForm\`proportional\ to\ h\^4 . \ \ \ That\ is, \ for\ every\ factor\ of\ ten\ that\ we\ shrink\), \(TextForm\`h, \ we\ gain\ a\ factor\ of\ 10, 000\ \ \((i . e . \ 4\ more\ decimal\ places)\)\), \(TextForm\`in\ accuracy . \ \ \ Not\ \(bad . \)\)}], "Text"], Cell["\<\ In closing, let's graphically examine the difference between the exact and approximate solutions using h=.25 and x ranging from 0 to 4.\ \>", "Text"], Cell[BoxData[ \(approx\ = \ RKMethod[f, {0, 1}, .25, 16]\)], "Input"], Cell[BoxData[ \(Plot[{approx[x], \ Exp[x]}, {x, 0, 4}]\)], "Input"], Cell["...virtually indistinguishable.", "Text"], Cell[BoxData[ \(Plot[approx[x] - Exp[x], {x, 0, 4}]\)], "Input"], Cell["Why do you suppose the difference oscillates so much?", "Text"] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, ScreenStyleEnvironment->"Presentation", WindowSize->{520, 600}, WindowMargins->{{174, Automatic}, {Automatic, 149}} ] (*********************************************************************** 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[1731, 51, 45, 0, 64, "Subsection"], Cell[1779, 53, 1176, 29, 259, "Text"], Cell[2958, 84, 1040, 17, 495, "Input"], Cell[4001, 103, 373, 7, 83, "Text"], Cell[4377, 112, 50, 1, 39, "Input"], Cell[4430, 115, 109, 3, 66, "Text"], Cell[4542, 120, 71, 1, 39, "Input"], Cell[4616, 123, 42, 1, 39, "Input"], Cell[4661, 126, 119, 4, 66, "Text"], Cell[4783, 132, 74, 1, 39, "Input"], Cell[4860, 135, 42, 1, 39, "Input"], Cell[4905, 138, 741, 16, 177, "Text"], Cell[5649, 156, 161, 4, 89, "Text"], Cell[5813, 162, 75, 1, 39, "Input"], Cell[5891, 165, 71, 1, 39, "Input"], Cell[5965, 168, 47, 0, 43, "Text"], Cell[6015, 170, 68, 1, 39, "Input"], Cell[6086, 173, 69, 0, 43, "Text"] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)