(*********************************************************************** 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[ 25465, 783]*) (*NotebookOutlinePosition[ 26126, 807]*) (* CellTagsIndexPosition[ 26082, 803]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["\<\ Math 230, Spring 1999 Mathematica assignment 3 (due Monday, 3/1/99)\ \>", "Subsubtitle"], Cell["\<\ The following problems are taken from pages 101 to 107 of the \ Differential Equations with Mathematica book. In problems requiring the use \ of Euler's method, feel free to use my version of the EulerMethod command rather than the book's version. The main \ difference is that my method returns a function whereas the book's returns a \ table of values. You should also compare results obtained with Euler's \ method with results obtained using the Runge-Kutta method. \ \>", "Text"], Cell[CellGroupData[{ Cell["Problem 1.", "Subsection"], Cell[TextData[{ "First we find explicit solutions to y' = ", Cell[BoxData[ \(TraditionalForm\`y\^2\)]], "and y' = 1+ ", Cell[BoxData[ \(TraditionalForm\`y\^2\)]], "with y(0) = 1." }], "Text"], Cell[BoxData[ \(sol1\ = \ y[x] /. First[ DSolve[{\(y'\)[x]\ == \ y[x]^2, \ y[0]\ == \ 1}, \ y[x], \ x]] \)], "Input"], Cell[BoxData[ \(sol2\ = \ y[x] /. First[ DSolve[{\(y'\)[x]\ == \ y[x]^2\ + 1, \ y[0]\ == \ 1}, \ y[x], \ x]]\)], "Input"], Cell[BoxData[ \(plot1\ = \ Plot[sol1, {x, 0, 1}]; \ \n plot2\ = \ Plot[sol2, {x, 0, Pi/4}]; \nShow[\ plot1, \ plot2]; \)], "Input"], Cell[BoxData[{ \(TextForm \`Now\ if\ I\ call\ the\ functions\ I\ just\ graphed\ \(y\_\(1\ \)\) and\ y\_2\ and\ I\ let\ y\_3\), \(TextForm\`be\ the\ solution\ to\ y'\ = \ x\^2\ + \ y\^s, \ y \((0)\)\ = \ 1, \ then\ I\ expect\), \(TextForm \`that\ \(y\_3\) will\ lie\ between\ \(y\_\(1\ \)\) and\ y\_2 . \ \ \ This\ is\ because, \ first\ of\ all, \ all\ \), \(TextForm\`three\ solutions\ agree\ at\ x = 0. \ \ \ \ Secondly, \ if\ \ 0\ \[LessEqual] \ x\ \[LessEqual] 1, \ and\ \), \(TextForm\`y\_\(1\ \) \[LessEqual] \ y\_3 \[LessEqual] y\_\(\ 2\), \ then\ it\ \ follows\ that\ \ the\ right\ sides\ of\ the\), \(TextForm\`corresponding\ differential\ equations\ satisfy\n\), \(TextForm\`\ \ \ \ \ \ \ \ \((\ y\_\(1\ \ \))\)\^2 \[LessEqual] \ \ \((y\_3)\)\^2 + \ x\^2 \[LessEqual] \ \((y\_\(\ 2\))\)\^2\ + \ 1. \n\), \(TextForm\`Hence\ the\ slopes\ of\ the\ three\ functions\ will\ satisfy\n \), \(TextForm\`\ \ \ \ \ \ \ \ \ \((y\_\(1\ \)\ )\)' \[LessEqual] \ \((y\_3)\)' \[LessEqual] \((y\_\(\ 2\))\)' . \n\nSo\ as\ x\ increases\ we\ will\ continue\ to\ have\ \ \ \(y\_\(1\ \)\) \((x)\)\ \[LessEqual] \ \(y\_3\) \((x)\) \[LessEqual] \(y\_\(\ 2\)\) \((x)\)\ \), \(TextForm\`for\ all\ x\ \[LessEqual] \ 1. \)}], "Text"], Cell[TextData[{ "From these observations and the formulae for ", Cell[BoxData[ \(TextForm\`\(\ \(y\_\(1\ \)\) and\ y\_2\)\)]], ", we see\nthat ", Cell[BoxData[ \(TraditionalForm\`\(\ y\_3\)\)]], " will necessarily tend to infinity as x goes to\nsome point x=a between \ \[Pi]/4 and 1. To confirm this, we solve\nnumerically:" }], "Text"], Cell[BoxData[ \(sol3\ = \ y[x] /. First[ NDSolve[{\(y'\)[x]\ == \ y[x]^2\ + x^2, \ y[0]\ == \ 1}, \ y[x], \ {x, 0, 1}]]\)], "Input"], Cell["\<\ We then plot and compare with the previous two graphs. We'll have to play with the range a lot in order to find the location of the vertical asymptote of the new function.\ \>", "Text"], Cell[BoxData[ \(plot3\ = \ Plot[sol3, {x, 0, .967}]; \nShow[plot1, plot2, plot3]; \)], "Input"], Cell["\<\ So the last solution really does lie between the other two solutions, and it has a vertical asymptote at about x = .967.\ \>", "Text"], Cell[BoxData[ \(\(\n\n\n\n\n\n\n\n\n\)\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Problem 4.", "Subsection"], Cell[CellGroupData[{ Cell["a.", "Subsubsection"], Cell[BoxData[ \(sol[x_, b_]\ := \ y[x] /. First[ NDSolve[\n \t\t\t\t\ { \(y'\)[x]\ == \ \((y[x]\ - \ x^2)\) \((1 - y[x]^2)\), \ y[0]\ == \ b}, y[x], \ {x, 0, 5}]]\)], "Input"], Cell[BoxData[ \(\(Plot[Evaluate[Table[sol[x, b], {b, \(-2\), 4, .25}]], {x, 0, 4}, PlotRange -> {\(-2\), 4}]; \)\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["b.", "Subsubsection"], Cell["\<\ Looking at the ODE, it's clear that y = -1 and y=1 are constant solutions. It looks like any solution that starts off with y(0)<1 will tend eventually to y= -1 as x goes to infinity. It's worth pointing out however that solutions with 0 1 seem to decrease initially and then turn around and head for infinity as x increases. Just to make sure about this last point, it's worth plotting solutions with y(0) > 1 on a larger scale:\ \>", "Text"], Cell[BoxData[ \(\(plot1\ = \ Plot[Evaluate[Table[sol[x, b], {b, 1, 4, .25}]], {x, 0, 5}, PlotRange -> { .5, 25}]; \)\)], "Input"], Cell[BoxData[{ FormBox[ \(This\ is\ truly\ peculiar\ behavior . \ \ All\ these\ solutions\ develop \), TextForm], FormBox[ RowBox[{ FormBox[\(a\ `kink' around\ x\ = \ 2.25 . \ \ Nevertheless, \ they\ do\ all\ seem\ to\), "TextForm"], " "}], TextForm], FormBox[ \(go\ to\ infinity\ as\ x\ increases . \ \ Since\ y'\ \[LessEqual] 0\ when\ y \[GreaterEqual] x\^2\ and\ y \[GreaterEqual] 1, \ \n it' s\ apparent\ that\ no\ solution\ will\ remain\ above\ y = x\^2\ for\), TextForm], FormBox[ \(long . \ \ However, \ it\ does\ look\ like\ solutions\ might\ be\ asymptotic\), TextForm], FormBox[ \(to\ y = x\^2 . \ \ We\ can\ tell\ best\ if\ we\ plot\ them\ along\ side\ the\ \(parabola : \)\), TextForm]}], "Text"] }, Open ]], Cell[CellGroupData[{ Cell["c.", "Subsubsection"], Cell[BoxData[ \(\(Show[plot1, \ Plot[x^2, {x, 0, 5}], \ PlotRange -> {0, 25}]; \)\)], "Input"], Cell["...an asymptote if ever there was one!", "Text"], Cell[BoxData[ \(Needs["\"]\)], "Input"], Cell[BoxData[ \(\(PlotVectorField[{1, \((y - x^2)\) \((1 - y^2)\)}, \ {x, 0, 5}, {y, 0, 25}, \ Axes -> \ True, \n\t ScaleFunction \[Rule] \((1&)\), Ticks \[Rule] None, Frame \[Rule] True, AspectRatio \[Rule] 1, \ PlotPoints\ -> \ 30]; \)\)], "Input"], Cell["\<\ The arrows in the direction field seem to be nearly vertical except on the parabola. The arrows above the parabola point down and those below point up. Hence any solution is going to be driven to converge to the parabola as x increases.\ \>", "Text"], Cell[BoxData[ \(\(\n\n\n\n\n\n\n\n\n\)\)], "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Problem 12.", "Subsection"], Cell[CellGroupData[{ Cell["a.", "Subsubsection"], Cell["\<\ I copy in the EulerMethod routine from one of the class demos:\ \>", "Text"], Cell[BoxData[ \(\(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["Now I apply to y' = f(x,y) where", "Text"], Cell[BoxData[ \(f[x_, y_]\ = \ 4 x - 2 y/x\)], "Input"], Cell["(Notice the problem at x=0!)", "Text"], Cell[BoxData[ \(sol1\ = \ EulerMethod[f, {\(- .5\), 4.35}, .2, 10]; \n Plot[sol1[x], {x, \(- .5\), 1.5}, \ PlotRange -> \ {\(-260\), 100}]; \)], "Input"], Cell["What a mess! ", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["b.", "Subsubsection"], Cell[BoxData[ \(sol2\ = \ EulerMethod[f, {\(- .5\), 4.35}, .1, 20]; \n Plot[sol2[x], {x, \(- .5\), 1.5}, \ PlotRange -> \ {\(-260\), 100}]; \)], "Input"], Cell["\<\ This one is even worse. We might've expected that things would go wrong around x=0. It's a little surprising that the Euler Method command didn't balk with a step size of .1, since at the fifth step x lands right on 0.\ \>", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["c.", "Subsubsection"], Cell[BoxData[ \(sol3\ = \ y[x] /. First[ NDSolve[{\(y'\)[x] == f[x, y[x]], \ y[\(- .5\)]\ == \ 4.35}, y[x], \n\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {x, \(- .5\), 1.5}]]\)], "Input"], Cell["\<\ It's choking around x=0 again. We'll just plot what comes before that:\ \>", "Text"], Cell[BoxData[ \(\(Plot[sol3, {x, \(- .5\), 0}]; \)\)], "Input"], Cell["\<\ Sure enough, there's a vertical asymptote at x=0. There's no reasonable way to continue the solution (numerically or otherwise) past this point.\ \>", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["d.", "Subsubsection"], Cell["\<\ Mathematica won't do the first one on its own. We'll settle for the general solution to the ODE and then solve the initial value problem ourselves:\ \>", "Text"], Cell[BoxData[ \(\(\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ DSolve[{\(y'\)[x]\ == \ f[x, y[x]]}, y[x], x]\)\)], "Input"], Cell[BoxData[ \(TextForm\`To\ get\ y \((0)\)\ = \ 0, \ we\ need\ to\ take\ C[1]\ = \ 0. \ \ That\ is, \ y \((x)\)\ = \ \(x\^2 . \)\)], "Text"], Cell[BoxData[ \(sol1\ = \ x^2\)], "Input"], Cell["The other initial value problems are easier.\t", "Text"], Cell[BoxData[{ \(sol2 = \ y[x] /. First[\n\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ DSolve[{\(y'\)[x]\ == \ f[x, y[x]], y[\(- .5\)] == 4.25}, y[x], x] \n\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\), \(sol3 = \ y[x] /. First[\n\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ DSolve[{\(y'\)[x]\ == \ f[x, y[x]], y[ .5] == 4.25}, y[x], x]\n \t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\), \(\tsol4 = \ y[x] /. First[\n\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ DSolve[{\(y'\)[x]\ == \ f[x, y[x]], y[\(- .5\)] == 3.75}, y[x], x] \n\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\), \(\tsol5 = \ y[x] /. First[\n\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ DSolve[{\(y'\)[x]\ == \ f[x, y[x]], y[ .5] == 3.75}, y[x], x]\n \t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]\)}], "Input"], Cell["\<\ The first two and last two of these are the same. Hence we need \ only plot three of the five solutions.\ \>", "Text"], Cell[BoxData[ \(\(Plot[{sol1, \ sol2, \ sol4}, {x, \(-3\), 3}, \ PlotRange -> {\(-1\), 20}]; \)\)], "Input"], Cell[BoxData[{ FormBox[ \(The\ solution\ y = \ \(x\^2\) is\ a\ little\ surprising\ but\ the\ asymptotes\), TextForm], FormBox[ RowBox[{ FormBox[ \(in\ the\ other\ solutions\ match\ up\ very\ well\ with\ the\ x\ in \ the\), "TextForm"], " "}], TextForm], FormBox[\(denominator\ of\ the\ original\ \(equation . \)\), TextForm]}], "Text"], Cell[BoxData[ \(\(\n\n\n\n\n\n\n\n\)\)], "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Problem 15.", "Subsection"], Cell[CellGroupData[{ Cell["a.", "Subsubsection"], Cell[BoxData[ \(f[x_, y_]\ := \ 2 y - 2 + 3 E^\((\(-x\))\)\)], "Input"], Cell[BoxData[ \(sol1\ = \ EulerMethod[f, {0, 0}, .2, 10]\)], "Input"], Cell[BoxData[ \(\(plot1\ = \ Plot[sol1[x], {x, 0, 2}]; \)\)], "Input"], Cell["\<\ Based on this graph, it looks like y(x) is going to infinity as x increases.\ \>", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["b.", "Subsubsection"], Cell[BoxData[{ \(sol2\ = \ EulerMethod[f, {0, 0}, .1, 20]\), \(\(plot2\ = \ Plot[sol2[x], {x, 0, 2}]; \)\)}], "Input"], Cell[BoxData[{ \(sol3\ = \ EulerMethod[f, {0, 0}, .05, 40]\), \(\(plot3\ = \ Plot[sol3[x], {x, 0, 2}]; \)\)}], "Input"], Cell["\<\ Looks like there's a problem here. The graphs appear to be \ identical but their ranges are different. If we put them all together, we see:\ \>", "Text"], Cell[BoxData[ \(\(Show[plot1, plot2, plot3]; \)\)], "Input"], Cell["\<\ Long term predictions based on these approximate solutions are going to be unreliable.\ \>", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["c.", "Subsubsection"], Cell[BoxData[{ \(sol4\ = \ y[x] /. First[ NDSolve[\n \t\t\t\t\ \ \ \ \ \ \ { \(y'\)[x]\ == \ 2 y[x] - 2 + 3 E^\((\(-x\))\), \ y[0] == 0}, \ y[x], \ {x, 0, 2}]]\), \(\t\(Plot[sol4, {x, 0, 2}]; \)\)}], "Input"], Cell["\<\ Now it looks like y might be headed for a finite limit as x \ increases. Can't tell though without checking some larger values of x.\ \>", "Text"], Cell[BoxData[{ \(sol5\ = \ y[x] /. First[ NDSolve[\n \t\t\t\t\ \ \ \ \ \ \ { \(y'\)[x]\ == \ 2 y[x] - 2 + 3 E^\((\(-x\))\), \ y[0] == 0}, \ y[x], \ {x, 0, 10}]]\), \(\t\(Plot[sol5, {x, 0, 10}]; \)\)}], "Input"], Cell["Now it looks like y is headed to negative infinity!", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["d.", "Subsubsection"], Cell[BoxData[{ \(sol6\ = \ Expand[y[x] /. First[DSolve[\n \t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ { \(y'\)[x]\ == \ 2 y[x] - 2 + 3 E^\((\(-x\))\), \ y[0] == 0}, \ \ \ \ \ \n \t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[x], x]]] \), \(\(Plot[sol6, {x, 0, 10}]; \)\)}], "Input"], Cell[BoxData[{ \(TextForm \`So\ the\ truth\ is\ that\ y\ is\ tending\ to\ 1\ as\ x\ increases . \ \ My\ guess\ is\ that\), \(TextForm \`the\ solution\ y \((x)\)\ = 1 - E\^\(-x\)\ is\ very\ unstable\ in\ the\ sense\ that\ any\), \(TextForm \`solution\ that\ starts\ near\ this\ one\ diverges\ rapidly\ as\ x\ increases . \nThis\ means\ that\ when\ I\ seek\ a\ numerical\ approximation, \ and\ small\), \(TextForm \`error\ in\ the\ approximation\ is\ quickly\ magnified\ for\ larger\ values\), \(TextForm\`of\ x . \ \ To\ check\ my\ theory, \ I' ll\ solve\ the\ ODE\ for\ several\ initial\), \(TextForm \`values\ close\ to\ zero\ and\ plot\ all\ the\ solutions\ \(together . \)\)}], "Text"], Cell[BoxData[ \(sol[x_, b_]\ = \ y[x] /. First[ DSolve[\ \n \t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ { \(y'\)[x]\ == \ 2 y[x] - 2 + 3 E^\((\(-x\))\), \ y[0] == b}, \ \ \ \ \ \n\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ y[x], x]]\)], "Input"], Cell[BoxData[ \(\(Plot[ Evaluate[Table[sol[x, b], {b, \(- .001\), .001, .0002}]], {x, 0, 10}, \nPlotRange -> {\(-5\), 5}]; \)\)], "Input"], Cell["That's pretty unstable looking!", "Text"], Cell[BoxData[ \(\ \)], "Input"], Cell[TextData[{ "\n\n\n\n\n\n", Cell[BoxData[ \(TextForm\`\(\n\n\n\n\n\n\n\n\n\n\)\)], "Text"] }], "Text"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Quadratic Version of Euler's Method", "Subsection"], Cell[BoxData[{ FormBox[ \(Euler' s\ method\ for\ approximating\ solutions\ of\), TextForm], FormBox[ RowBox[{" ", \(y' \((x)\)\ = \ f \((x, y)\), \ \ \ \ \ y \((x\_0)\)\ = \ y\_0\)}], TextForm], FormBox[ RowBox[{ \(works\ by\ assuming\ that\ y\ is\ approximately\ linear\ on\ small\ intervals . Describe\ a\ variation\ on\), " "}], TextForm], FormBox[ RowBox[{ RowBox[{ \(Euler'\), "s", " ", "method", " ", "that", " ", "works", " ", "by", " ", "assuming", " ", "that", " ", "y", " ", "is", " ", StyleBox["quadratic", FontSlant->"Italic"], StyleBox[" ", FontSlant->"Italic"], "on", " ", "small", " ", \(intervals . \ Implement\)}], " "}], TextForm], FormBox[ RowBox[{ "this", " ", "new", " ", "method", " ", "as", " ", "a", " ", "Mathematica", " ", "command", " ", "called", " ", RowBox[{ StyleBox["QuadMethod", FontWeight->"Bold"], ".", " ", "Use"}], " ", "my", " ", StyleBox["EulerMethod", FontWeight->"Bold"]}], TextForm], FormBox[ RowBox[{ RowBox[{ RowBox[{"and", " ", \((especially)\), " ", StyleBox["RKMethod", FontWeight->"Bold"], " ", "commands", " ", "as", " ", "models", " ", "for", " ", "syntax"}], ",", " ", \(etc . \ \ When\ you\ get\ your\)}], " "}], TextForm], FormBox[ RowBox[{ RowBox[{ RowBox[{ StyleBox["QuadMethod", FontWeight->"Bold"], " ", "command", " ", "working"}], ",", " ", \(try\ to\ answer\ the\ following\ questions\ by\ applying\ the \)}], " "}], TextForm], FormBox[ \(command\ in\ specific\ examples \((e . g . \ the\ y'\ = \ y, \ y \((0)\)\ = \ 1\ example; \ \ also\ try\ y'\ = \ 3 y/x, \n y \((1)\)\ = \ 1\ to\ check\ that\ your\ method\ works\ for\ non - autonomous\ ODE' s . \n\t\n\ \ \ \ \ \ \((a)\)\ How\ do\ local\ and\ global\ truncation\ errors\ depend\ on\ h?\ \ That\ is, \ are\ they\ about\ \n\t\ \ \ \ \ \ \ \ equal\ to\ Ch?\ to\ \(Ch\^2\)?\ \ etc . \n\t\t\n\ \ \ \ \ \ \((b)\)\ Does\ the\ new \ method\ work\ better\ or\ worse\ than\ Euler' s\ \ \ method?\ \ than\ the\ Runge - \n\t\ \ \ \ \ \ \ \ \ Kutta\ \(method?\)\ \)\), TextForm]}], "Text"], Cell[CellGroupData[{ Cell["Solution", "Subsubsection"], 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[""], "Input"], Cell[BoxData[{ \(TextForm \`My\ QuadMethod\ command\ will\ be\ similar\ to\ Euler' s\ method, \ but \), \(TextForm\`instead\ of\ using\ linear\ approximations\ to\ estimate\ y, \ I\ will\), \(TextForm \`use\ quadratic\ approximation\ \(\((i . e . \ the\ first\ three\ terms\ in\nthe\ Taylor\ series)\) : \n\t\t\t\t\n\t\ty \((x)\)\)\ = \ y \((x\_0)\)\ + \ y' \((x\_0)\) \((x - x\_0)\)\ + \(\(\(y'\)' \((x\_0)\)\)\/2\) \((x - x\_0)\)\^2\nAs\ with\ Euler' s\ method, \ I\ get\ y' \((x\_0)\)\ from\ the\ differential\nequation\n\t\t\ \ \ \ \ y' = \ f \((x, y)\) . \nTo\ get\ \(y'\)' \((x\_0)\), \ I\ differentiate\ this\ equation\ with\ respect\ to\ \n \(x : \n\t\t\ \ \ \ \ \(y'\)'\) = \ \(\[PartialD]f\/\[PartialD]x\ dx\/dx\ + \ \ \[PartialD]f\/\[PartialD]y\ dy\/dx\ = \ \ \[PartialD]f\/\[PartialD]x\ \ + \ \ \[PartialD]f\/\[PartialD]y\ \(f . \)\)\n\n\t\t\)}], "Text"], Cell[BoxData[ \(QuadMethod[f_, {x0_, y0_}, h_, n_]\ := \n\n\t\t Module[\n\t\t\ \ \ \ \ \ \ \ {QMStep, \ deriv2}, \t\t\n \t\t\ \ \ \ \ \ \ \ \n\t\t\ \ \ \ \ \ \ \ deriv2[x_, y_]\ = \ \[PartialD]\_x f[x, y]\ + \ f[x, y]*\[PartialD]\_y f[x, y]; \n \ \ \ \ \ \ \ \ \ \ \ \ \ QMStep[{x_, y_}]\ := \ N[{x + h, y + h*f[x, y]\n \t\t\t\t\t\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ + \((h^2)\)*deriv2[x, y]/2. }, 20]; \n\t\t\t\t\t Interpolation[\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NestList[QMStep, {x0, y0}, n], \n\t\t\t\ \ \ \ \ \ \ \ \ \ \ \ InterpolationOrder\ -> \ 2\n\t\t\t\ \ \ \ \ ]\t\n\t\ \ ]\)], "Input"], Cell["\<\ Note that I'm setting the InterpolationOrder option to 2 this time. \t\t For starters, we'll try this method out on the initial value problem y'= y, y(0) = 1, comparing our results for a given step size with those from Euler's method and the Runge-Kutta method.\ \>", "Text"], Cell[BoxData[ \(\(f[x_, y_]\ := \ y; \)\)], "Input"], Cell[BoxData[ \(sol1\ = \ QuadMethod[f, {0, 1}, .25, 4]; \n sol2\ = \ EulerMethod[f, {0, 1}, .25, 4]; \n sol3\ = \ RKMethod[f, {0, 1}, .25, 4]; \)], "Input"], Cell[BoxData[ \(\(Plot[{sol1[x], sol2[x], sol3[x], E^x}, {x, 0, 1}]; \)\)], "Input"], Cell["\<\ Only one of these four graphs is much different from the others. I'll bet it's the EulerMethod graph. Let's just look at values at x=1 instead:\ \>", "Text"], Cell[BoxData[{ \(sol1[1]\), \(sol2[1]\), \(sol3[1]\), \(N[E^1, 6]\)}], "Input"], Cell["\<\ So the Runge-Kutta approximation looks best by about three digits! At least our new method beats the Euler approximation. Now we consider the issue of global approximation error. I define a function that takes a given step size h and gives me the difference between my approximate solution (based on the new method with step size h) at x=1and the correct value of e . \ \>", "Text"], Cell[BoxData[ \(error[h_]\ := \ \((QuadMethod[f, {0, 1}, h, IntegerPart[2. /h]])\)[1]\ - \ E\)], "Input"], Cell[BoxData[ \(error[ .1]\)], "Input"], Cell[BoxData[ \(error[ .01]\)], "Input"], Cell[BoxData[ \(error[ .001]\)], "Input"], Cell[BoxData[ \(error[ .0001]\)], "Input"], Cell[BoxData[{ \(TextForm\`Every\ time\ I\ decrease\ h\ by\ a\ factor\ of\ ten, \ I\ gain\ about\ a\ factor\ of\), \(TextForm \`one\ hundred\ in\ accuracy . \ \ It\ looks\ like\ I\ have\ a\ global\ approximation\), \(TextForm\`error\ proportional\ to\ h\^2 . \ \ Again, \ this\ is\ worse\ than\ Runge - Kutta\), \(TextForm\`but\ better\ than\ Euler' s\ \(Method . \)\)}], "Text"], Cell["Finally I consider local approximation error", "Text"], Cell[BoxData[ \(local[h_]\ := \ \((QuadMethod[f, {0, 1}, h, 2])\)[h]\ - \ E^h\)], "Input"], Cell[BoxData[ \(local[ .1]\)], "Input"], Cell[BoxData[ \(local[ .01]\)], "Input"], Cell[BoxData[ \(local[ .001]\)], "Input"], Cell[BoxData[ \(local[ .0001]\)], "Input"], Cell[BoxData[{ \(TextForm \`So\ with\ every\ factor\ of\ 10\ decrease\ in\ step\ size\ I\ seem\ to\ be\ getting\), \(TextForm \`a\ factor\ of\ 1000\ increase\ in\ accuracy . \ \ It\ looks\ like\ local \ \), \(TextForm\`truncation\ error\ is\ proportional\ to\ \(h\^3 . \)\)}], "Text"] }, Open ]] }, Open ]] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{533, 833}, WindowMargins->{{286, Automatic}, {-116, Automatic}}, ShowCellLabel->False ] (*********************************************************************** 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, 98, 4, 89, "Subsubtitle"], Cell[1832, 57, 498, 8, 122, "Text"], Cell[CellGroupData[{ Cell[2355, 69, 32, 0, 45, "Subsection"], Cell[2390, 71, 213, 8, 32, "Text"], Cell[2606, 81, 151, 4, 43, "Input"], Cell[2760, 87, 159, 4, 43, "Input"], Cell[2922, 93, 144, 3, 59, "Input"], Cell[3069, 98, 1353, 24, 268, "Text"], Cell[4425, 124, 361, 9, 86, "Text"], Cell[4789, 135, 170, 4, 43, "Input"], Cell[4962, 141, 197, 4, 68, "Text"], Cell[5162, 147, 106, 2, 43, "Input"], Cell[5271, 151, 145, 3, 50, "Text"], Cell[5419, 156, 55, 1, 171, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5511, 162, 32, 0, 45, "Subsection"], Cell[CellGroupData[{ Cell[5568, 166, 27, 0, 42, "Subsubsection"], Cell[5598, 168, 241, 6, 59, "Input"], Cell[5842, 176, 137, 2, 43, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[6016, 183, 27, 0, 42, "Subsubsection"], Cell[6046, 185, 591, 10, 176, "Text"], Cell[6640, 197, 151, 3, 43, "Input"], Cell[6794, 202, 844, 20, 115, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[7675, 227, 27, 0, 42, "Subsubsection"], Cell[7705, 229, 101, 2, 27, "Input"], Cell[7809, 233, 54, 0, 32, "Text"], Cell[7866, 235, 65, 1, 27, "Input"], Cell[7934, 238, 275, 4, 75, "Input"], Cell[8212, 244, 262, 5, 86, "Text"], Cell[8477, 251, 55, 1, 171, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[8581, 258, 33, 0, 45, "Subsection"], Cell[CellGroupData[{ Cell[8639, 262, 27, 0, 42, "Subsubsection"], Cell[8669, 264, 89, 3, 32, "Text"], Cell[8761, 269, 444, 7, 171, "Input"], Cell[9208, 278, 48, 0, 32, "Text"], Cell[9259, 280, 62, 1, 27, "Input"], Cell[9324, 283, 44, 0, 32, "Text"], Cell[9371, 285, 166, 3, 43, "Input"], Cell[9540, 290, 30, 0, 32, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[9607, 295, 27, 0, 42, "Subsubsection"], Cell[9637, 297, 166, 3, 43, "Input"], Cell[9806, 302, 244, 5, 86, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[10087, 312, 27, 0, 42, "Subsubsection"], Cell[10117, 314, 245, 5, 59, "Input"], Cell[10365, 321, 95, 3, 50, "Text"], Cell[10463, 326, 67, 1, 27, "Input"], Cell[10533, 329, 169, 4, 68, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[10739, 338, 27, 0, 42, "Subsubsection"], Cell[10769, 340, 172, 4, 68, "Text"], Cell[10944, 346, 124, 2, 27, "Input"], Cell[11071, 350, 159, 3, 31, "Text"], Cell[11233, 355, 47, 1, 27, "Input"], Cell[11283, 358, 62, 0, 32, "Text"], Cell[11348, 360, 1050, 20, 203, "Input"], Cell[12401, 382, 129, 4, 50, "Text"], Cell[12533, 388, 119, 2, 27, "Input"], Cell[12655, 392, 418, 12, 65, "Text"], Cell[13076, 406, 53, 1, 155, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[13178, 413, 33, 0, 45, "Subsection"], Cell[CellGroupData[{ Cell[13236, 417, 27, 0, 42, "Subsubsection"], Cell[13266, 419, 78, 1, 27, "Input"], Cell[13347, 422, 75, 1, 27, "Input"], Cell[13425, 425, 75, 1, 27, "Input"], Cell[13503, 428, 100, 3, 50, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[13640, 436, 27, 0, 42, "Subsubsection"], Cell[13670, 438, 130, 2, 43, "Input"], Cell[13803, 442, 131, 2, 43, "Input"], Cell[13937, 446, 168, 5, 50, "Text"], Cell[14108, 453, 64, 1, 27, "Input"], Cell[14175, 456, 110, 3, 50, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[14322, 464, 27, 0, 42, "Subsubsection"], Cell[14352, 466, 277, 7, 75, "Input"], Cell[14632, 475, 156, 4, 50, "Text"], Cell[14791, 481, 279, 7, 75, "Input"], Cell[15073, 490, 67, 0, 32, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[15177, 495, 27, 0, 42, "Subsubsection"], Cell[15207, 497, 389, 9, 75, "Input"], Cell[15599, 508, 785, 18, 132, "Text"], Cell[16387, 528, 330, 7, 59, "Input"], Cell[16720, 537, 157, 3, 59, "Input"], Cell[16880, 542, 47, 0, 32, "Text"], Cell[16930, 544, 35, 1, 27, "Input"], Cell[16968, 547, 116, 4, 306, "Text"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[17133, 557, 57, 0, 45, "Subsection"], Cell[17193, 559, 2558, 57, 268, "Text"], Cell[CellGroupData[{ Cell[19776, 620, 33, 0, 42, "Subsubsection"], Cell[19812, 622, 1040, 17, 315, "Input"], Cell[20855, 641, 26, 0, 27, "Input"], Cell[20884, 643, 1015, 20, 284, "Text"], Cell[21902, 665, 812, 16, 219, "Input"], Cell[22717, 683, 291, 7, 122, "Text"], Cell[23011, 692, 57, 1, 27, "Input"], Cell[23071, 695, 175, 3, 59, "Input"], Cell[23249, 700, 88, 1, 27, "Input"], Cell[23340, 703, 168, 4, 68, "Text"], Cell[23511, 709, 98, 4, 75, "Input"], Cell[23612, 715, 397, 8, 140, "Text"], Cell[24012, 725, 121, 3, 43, "Input"], Cell[24136, 730, 43, 1, 27, "Input"], Cell[24182, 733, 44, 1, 27, "Input"], Cell[24229, 736, 45, 1, 27, "Input"], Cell[24277, 739, 46, 1, 27, "Input"], Cell[24326, 742, 419, 8, 81, "Text"], Cell[24748, 752, 60, 0, 32, "Text"], Cell[24811, 754, 99, 2, 27, "Input"], Cell[24913, 758, 43, 1, 27, "Input"], Cell[24959, 761, 44, 1, 27, "Input"], Cell[25006, 764, 45, 1, 27, "Input"], Cell[25054, 767, 46, 1, 27, "Input"], Cell[25103, 770, 322, 8, 64, "Text"] }, Open ]] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)