(*********************************************************************** 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[ 10905, 339]*) (*NotebookOutlinePosition[ 11582, 363]*) (* CellTagsIndexPosition[ 11538, 359]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Errors in Numerical Procedures", "Subsection"], Cell[TextData[{ "Since numercial methods for solving initial value initial value problems \ only yield approximate solutions, it is very important in \npractice to \ understand how well a numerical solution \napproximates the actual solution \ of a differential equation.\nThat is, one needs useful upper bounds for \ `truncation error.'\n\n ", Cell[BoxData[{ FormBox[ \(In\ Euler' s\ method\ \((and\ its\ many\ refinements, \ such\ as\ the\ \nRunge - Kutta\ method)\), \ there\ are\ two\ main\ observations\ to\), TextForm], FormBox[ \(make . \n\t\n\((1)\)\ The\ quality\ of\ the\ approximate\ solution\ depends\ on\ the\ step\), TextForm], FormBox[ RowBox[{ FormBox[\(size\ h, \ a\ smaller\ step\ size\ usually\ yielding\ a\ better\), "TextForm"], " "}], TextForm], FormBox[ \(approximation . \ \ Therefore, \ h\ will\ figure\ prominently\ as\ a\), TextForm], FormBox[ RowBox[{ FormBox[\(parameter\ in\ any\ reasonable\ upper\ bound\ for\ error . \ \ In\), "TextForm"], " "}], TextForm], FormBox[ \(Euler' s\ method, \ one\ generally\ expects\ a\ \((global)\)\ error\ that\ is\ no\), TextForm], FormBox[ RowBox[{ FormBox[\(larger\ than\ Ch\ where\ C\ is\ a\ constant . \n\t\t\n\((2)\)\ Local\ truncation\ error\ \((that\ is, \ the\ error\ incurred\ in\nthe\ first\ step\ of\ a\ method)\)\ is\ easier\ to\ understand\ than\), "TextForm"], " "}], TextForm], FormBox[ \(global\ truncation\ error\ \((the\ error\ that\ accumulates\ in\nall\ the\ steps, \ from\ first\ to\ last, \ of\ the\ approximation . \nTo\ appreciate\ the\ difference, \ note\ that\ local\ truncation\nerror\ in\ Euler' s\ method\ is\ bounded\ by\ Ch\^2\ where\ C\ is\ roughly\nhalf\ the\ second\ derivative\ of\ the\ solution\ function; \ global\ntruncation\ error\ is\ also\ bounded\ by\ Ch, \ but\ a\ useful\ formula\nfor\ C\ is\ much\ harder\ to\ \(find . \)\)\), TextForm]}]] }], "Text", FontFamily->"Times"], Cell[CellGroupData[{ Cell["Local Truncation Error", "Subsubsection"], Cell[TextData[{ "In what follows we will consider the initial value problem \n \ y'(x) = y(x), y(0) = 1,\nwhich has the solution y =", Cell[BoxData[ \(TraditionalForm\`e\^x\)]] }], "Text", FontFamily->"Times"], Cell[BoxData[{ \(f[x_, y_]\ := \ y\n\), \(EulerMethod[\ f_, {x0_, \ y0_}, \ h_, \ n_\ ]\ := \ \n\n\t\t Module[\n\t\t\ \ \ {EMStep}, \n\t\t\ \ \ Interpolation[\n\ \ \ \t\ \ \ \ \ \ \ \ \ EMStep[{x_, y_}]\ := \ N[{x + h, y + h*f[x, y]}]; \n \t\t\ \ \ \ \ \ \ \ \ \ NestList[EMStep, {x0, y0}, n], \n \t\t\t\ \ \ \ \ \ \ InterpolationOrder\ -> \ 1\n\t\t\ \ \ ]\n \t\ \ ]\ \ \ \ \ \ \ \ \ \ \ \ \ \)}], "Input"], Cell["h=1:", "Text"], Cell[BoxData[{ \(ya = \ EulerMethod[f, {0, 1}, 1, 1]\), \(err\ = \ Abs[ya[1]\ - \ Exp[1]]\)}], "Input"], Cell["\<\ which is significantly larger than 1/2. The problem is that we really need to take account of the second derivative of y for all values of x between 0 and 0+h. The maximum value of y''(x) on the interval [0,1] is e, and e/2 is about 1.36, so we're actually doing OK. Let's try h=.1:\ \>", "Text", FontFamily->"Times"], Cell[BoxData[{ \(ya = \ EulerMethod[f, {0, 1}, .1, 1]\), \(err\ = \ Abs[ya[ .1]\ - \ Exp[ .1]]\)}], "Input"], Cell["Better. Let's finish with something *really* small.", "Text", FontFamily->"Times"], Cell[BoxData[{ \(ya\ = \ EulerMethod[f, {0, 1}, .000001, 1]\), \(err\ = \ Abs[ya[ .000001]\ - \ Exp[ .000001]]\)}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Global Truncation Error", "Subsubsection"], Cell["\<\ Now we will retry the same values of h but always compare the approximate and actual solutions at x = 1. For h = 1, this is already done. For h = .1, we obtain\ \>", "Text", FontFamily->"Times"], Cell[BoxData[{ \(ya = \ EulerMethod[f, {0, 1}, .1, 11]\), \(err\ = \ Abs[ya[1]\ - \ Exp[1]]\)}], "Input"], Cell["\<\ The global error seems roughly the same size as the step size now. With h=.001, we get\ \>", "Text", FontFamily->"Times"], Cell[BoxData[{ \(ya = \ EulerMethod[f, {0, 1}, .001, 1000]\), \(err\ = \ Abs[ya[1]\ - \ Exp[1]]\)}], "Input"], Cell["\<\ which is again about the same size as h. Let's try h=.00001:\ \>", "Text"], Cell[BoxData[{ \(ya = \ EulerMethod[f, {0, 1}, .00001, 100000]\), \(err\ = \ Abs[ya[1]\ - \ Exp[1]]\)}], "Input"], Cell[BoxData[{ FormBox[ \(Not\ bad, \ but\ I\ wouldn' t\ want\ to\ wait\ around\ for\ many\ more\ decimal\), TextForm], FormBox[ \(places\ of\ \(accuracy!\)\ \ Euler' s\ method\ turns\ out\ to\ be\ rather\), TextForm], FormBox[ RowBox[{ FormBox[ \(expensive\ computationally . \ \ Since\ global\ error\ is\), "TextForm"], " "}], TextForm], FormBox[ \(proportional\ to\ step\ size, \ it\ would\ take\ about\ 10\^20\), TextForm], FormBox[ \(operations\ to\ grind\ out\ 20\ digits\ of\ \(accuracy!\)\ \ More\ refined\), TextForm], FormBox[ RowBox[{ FormBox[\(methods, \ such\ as\ the\ Runge - Kutta\ method\ are\ much\ more\), "TextForm"], " "}], TextForm], FormBox[\(practical\ for\ computational\ \(purposes . \)\), TextForm]}], "Text", FontFamily->"Times"] }, Open ]], Cell[CellGroupData[{ Cell["Unstable Equilibria and Global Truncation Error", "Subsubsection"], Cell["\<\ Mathematica provides a routine --called `NDSolve'-- for solving ODE's numerically,and it is much better than the home-made Euler's method routine we've been using. It uses the same syntax as DSolve, except that it requires a range for the independent variable. That is, to solve the above ODE, we type\ \>", "Text", FontFamily->"Times"], Cell[BoxData[{ \(Clear[y]\), \(Clear[x]\), \(Clear[ya]\)}], "Input"], Cell[BoxData[ \(soln\ = \ NDSolve[\ {\(y'\)[x]\ == \ y[x], \ y[0]\ == \ 1}, y[x], {x, 0, 1}] \)], "Input"], Cell[BoxData[ \(ya[x_]\ = \ y[x] /. First[soln]\)], "Input"], Cell[BoxData[ \(ya[1]\)], "Input"], Cell["\<\ which is a pretty good approximation for e. Of course, NDSolve has options such as AccuracyGoal that you can set to improve the number of \ accurate digits in the approximation. Good as NDSolve is, it isn't perfect. Consider the following example (taken from the Mathematica supplement). \t\t\ \>", "Text", FontFamily->"Times"], Cell[BoxData[{ \(Clear[y]\), \(Clear[x]\t\t\), \(ivp\ = \ {\(y'\)[x]\ == \ y[x]\ - \ 3 Exp[\(-2\) x], \ y[0]\ == \ 1}\)}], "Input"], Cell["This is linear and can be solved exactly:", "Text", FontFamily->"Times"], Cell[BoxData[ \(DSolve[ivp, y[x], x]\)], "Input"], Cell[BoxData[ \(actual[x_]\ = \ y[x] /. First[%]\)], "Input"], Cell["If we solve numerically and compare, we get", "Text", FontFamily->"Times"], Cell[BoxData[{ \(Clear[y]\), \(Clear[x]\), \(NDSolve[ivp, y[x], {x, 0, 11}]\)}], "Input"], Cell[BoxData[ \(approx[x_]\ = \ y[x] /. First[%]\)], "Input"], Cell[BoxData[ \(Plot[approx[x] - actual[x], {x, 0, 11}, PlotRange -> {\(-1\), 1}]\)], "Input"], Cell["\<\ Something definitely goes wrong when x reaches 8. Asking for more accuracy does little to help.\ \>", "Text", FontFamily->"Times"], Cell[BoxData[{ \(Clear[y]\), \(Clear[x]\), \(NDSolve[ivp, y[x], {x, 0, 15}, AccuracyGoal -> 8, \n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PrecisionGoal\ -> \ 8, \ WorkingPrecision -> \ 18]\)}], "Input"], Cell[BoxData[ \(approx[x_]\ = \ y[x] /. First[%]\)], "Input"], Cell[BoxData[ \(Plot[approx[x] - actual[x], {x, 0, 15}, PlotRange -> {\(-1\), 1}]\)], "Input"], Cell["\<\ We've effectively asked for a hundred times more precision and gotten an approximation that only holds out for another two units along the x axis. The problem is that the exact solution of our differential equation is a sort of unstable equilibrium\ \>", "Text", FontFamily->"Times"], Cell[BoxData[ \(Needs["\"]\)], "Input"], Cell[BoxData[ \(\(PlotVectorField[{1, y - 3 Exp[\(-2\) x]}, {x, 0, 10}, {y, \(- .5\), 1.5}, \ Axes -> \ True, \n\tScaleFunction \[Rule] \((1&)\), Ticks \[Rule] None, Frame \[Rule] True, AspectRatio \[Rule] 1, \ PlotPoints\ -> \ 10]; \)\)], "Input"], Cell["\<\ As soon as the approximate solution is even a tiny bit off, the direction field will rapidly carry it further away from the actual solution.\ \>", "Text", FontFamily->"Times"] }, Open ]] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, ScreenStyleEnvironment->"Presentation", WindowSize->{517, 628}, WindowMargins->{{320, Automatic}, {Automatic, 61}} ] (*********************************************************************** 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, 52, 0, 71, "Subsection"], Cell[1786, 53, 2378, 55, 700, "Text"], Cell[CellGroupData[{ Cell[4189, 112, 47, 0, 58, "Subsubsection"], Cell[4239, 114, 254, 6, 101, "Text"], Cell[4496, 122, 473, 8, 340, "Input"], Cell[4972, 132, 20, 0, 45, "Text"], Cell[4995, 134, 114, 2, 63, "Input"], Cell[5112, 138, 334, 7, 155, "Text"], Cell[5449, 147, 120, 2, 63, "Input"], Cell[5572, 151, 91, 1, 47, "Text"], Cell[5666, 154, 137, 2, 63, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5840, 161, 48, 0, 58, "Subsubsection"], Cell[5891, 163, 209, 5, 101, "Text"], Cell[6103, 170, 117, 2, 63, "Input"], Cell[6223, 174, 133, 4, 74, "Text"], Cell[6359, 180, 121, 2, 63, "Input"], Cell[6483, 184, 88, 3, 45, "Text"], Cell[6574, 189, 125, 2, 63, "Input"], Cell[6702, 193, 919, 26, 199, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[7658, 224, 72, 0, 58, "Subsubsection"], Cell[7733, 226, 353, 7, 155, "Text"], Cell[8089, 235, 82, 3, 87, "Input"], Cell[8174, 240, 128, 3, 87, "Input"], Cell[8305, 245, 65, 1, 39, "Input"], Cell[8373, 248, 38, 1, 39, "Input"], Cell[8414, 251, 342, 9, 209, "Text"], Cell[8759, 262, 160, 5, 111, "Input"], Cell[8922, 269, 80, 1, 47, "Text"], Cell[9005, 272, 53, 1, 39, "Input"], Cell[9061, 275, 66, 1, 39, "Input"], Cell[9130, 278, 82, 1, 47, "Text"], Cell[9215, 281, 103, 3, 87, "Input"], Cell[9321, 286, 66, 1, 39, "Input"], Cell[9390, 289, 101, 2, 63, "Input"], Cell[9494, 293, 142, 4, 74, "Text"], Cell[9639, 299, 228, 5, 159, "Input"], Cell[9870, 306, 66, 1, 39, "Input"], Cell[9939, 309, 101, 2, 63, "Input"], Cell[10043, 313, 296, 6, 128, "Text"], Cell[10342, 321, 65, 1, 39, "Input"], Cell[10410, 324, 277, 4, 141, "Input"], Cell[10690, 330, 187, 5, 101, "Text"] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)