(*********************************************************************** 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[ 10924, 310]*) (*NotebookOutlinePosition[ 11602, 334]*) (* CellTagsIndexPosition[ 11558, 330]*) (*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\nIn ", 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"], 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"], 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"], 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"], 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"], 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"], 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[{ \(TextForm\`Not\ bad, \ but\ I\ wouldn' t\ want\ to\ wait\ around\ for\ many\ more\ decimal\), \(TextForm \`places\ of\ \(accuracy!\)\ \ Euler' s\ method\ turns\ out\ to\ be\ rather\), \(TextForm\`expensive\ computationally . \ \ Since\ global\ error\ is\ \), \(TextForm\`proportional\ to\ step\ size, \ it\ would\ take\ about\ 10\^20\), \(TextForm \`operations\ to\ grind\ out\ 20\ digits\ of\ \(accuracy!\)\ \ More\ refined\), \(TextForm\`methods, \ such\ as\ the\ Runge - Kutta\ method\ are\ much\ more\ \), \(TextForm\`practical\ for\ computational\ \(purposes . \)\)}], "Text"] }, 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"], Cell[BoxData[ \(ya[x_]\ = \ First[y[x] /. NDSolve[\ {\(y'\)[x]\ == \ y[x], \ y[0]\ == \ 1}, y[x], {x, 0, 1}]]\)], "Input"], Cell[BoxData[ \(ya[1]\)], "Input"], Cell[TextData[{ "which is a pretty good approximation for e. Of course, NDSolve\nhas \ options such as AccuracyGoal that you can set to improve the number of \ accurate digits in the approximation.\n\nGood as NDSolve is, it isn't \ perfect. Consider the following\nexample (taken from the Mathematica \ supplement).\n\t\ty'(x) = y(x) - 3", Cell[BoxData[ \(TraditionalForm\`e\^x\)]], ", y(0) = 1", "\n", "This is linear and can be solved exactly:" }], "Text"], Cell[BoxData[ \(actual[x_]\ = \ First[y[x] /. DSolve[\n\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ { \(y'\)[x]\ == y[x]\ - \ 3 Exp[\(-2\) x], \n \t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[0]\ == \ 1}, \ \ \n \t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[x], x]] \)], "Input"], Cell["If we solve numerically and compare, we get", "Text"], Cell[BoxData[ \(\(\napprox[x_]\ = \ First[y[x] /. NDSolve[{\(y'\)[x]\ == y[x]\ - \ 3 Exp[\(-2\) x], \n \t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[0]\ == \ 1}, \n\t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[x], {x, 0, 11}]]\)\)], "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"], Cell[BoxData[ \(approx[x_]\ = \ First[y[x] /. NDSolve[{\(y'\)[x]\ == y[x]\ - \ 3 Exp[\(-2\) x], \n \t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[0]\ == \ 1}, \n\t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[x], {x, 0, 15}, \n \t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ AccuracyGoal -> 8, \n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PrecisionGoal\ -> \ 8, \ \n \t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ WorkingPrecision -> \ 18]]\)], "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"], 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"] }, Open ]] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, ScreenStyleEnvironment->"Presentation", WindowSize->{520, 600}, WindowMargins->{{Automatic, 198}, {156, Automatic}} ] (*********************************************************************** 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, 64, "Subsection"], Cell[1786, 53, 2357, 54, 598, "Text"], Cell[CellGroupData[{ Cell[4168, 111, 47, 0, 56, "Subsubsection"], Cell[4218, 113, 231, 5, 89, "Text"], Cell[4452, 120, 473, 8, 327, "Input"], Cell[4928, 130, 20, 0, 43, "Text"], Cell[4951, 132, 114, 2, 63, "Input"], Cell[5068, 136, 311, 6, 135, "Text"], Cell[5382, 144, 120, 2, 63, "Input"], Cell[5505, 148, 68, 0, 43, "Text"], Cell[5576, 150, 137, 2, 63, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5750, 157, 48, 0, 56, "Subsubsection"], Cell[5801, 159, 186, 4, 89, "Text"], Cell[5990, 165, 117, 2, 63, "Input"], Cell[6110, 169, 110, 3, 66, "Text"], Cell[6223, 174, 121, 2, 63, "Input"], Cell[6347, 178, 88, 3, 43, "Text"], Cell[6438, 183, 125, 2, 63, "Input"], Cell[6566, 187, 673, 15, 174, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[7276, 207, 72, 0, 56, "Subsubsection"], Cell[7351, 209, 330, 6, 135, "Text"], Cell[7684, 217, 160, 4, 87, "Input"], Cell[7847, 223, 38, 1, 39, "Input"], Cell[7888, 226, 479, 11, 204, "Text"], Cell[8370, 239, 398, 8, 111, "Input"], Cell[8771, 249, 59, 0, 43, "Text"], Cell[8833, 251, 318, 6, 135, "Input"], Cell[9154, 259, 101, 2, 63, "Input"], Cell[9258, 263, 119, 3, 66, "Text"], Cell[9380, 268, 621, 12, 183, "Input"], Cell[10004, 282, 101, 2, 63, "Input"], Cell[10108, 286, 273, 5, 112, "Text"], Cell[10384, 293, 65, 1, 39, "Input"], Cell[10452, 296, 277, 4, 135, "Input"], Cell[10732, 302, 164, 4, 89, "Text"] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)