(*********************************************************************** 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[ 6264, 178]*) (*NotebookOutlinePosition[ 6942, 202]*) (* CellTagsIndexPosition[ 6898, 198]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Euler's Method", "Subsection"], Cell[BoxData[ \(TextForm \`The\ following\ `EulerMethod'\ routine\ implements\ Euler' s\ method\ for\ numerically\ solving\ the\ initial\ value\ problem . \n\n\ \ \ \ \ \t\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \(dy\/dx\)\ = \ f \((x, y)\), \ \ \ \ \ \ y \((x\_0)\)\ = \ y\_0 . \nHere\ is\ the\ Mathematica\ code\ for\ \ `EulerMethod'\ \(\((largely\ borrowed\ from\ page\ 74\ of\ the\ Mathematica\ supplement)\) : \)\)], "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["\<\ To use the routine first define a function f[x_,y_] of two \ variables corresponding to the right side of the differential equation. For example,\ \>", "Text"], Cell[BoxData[ \(\(\ \ \ \ \ \ \ \ \ \ \ f[x_, y_]\ := \ y\ - \ x\)\)], "Input"], Cell[BoxData[ \(TextForm\`Then\ type\)], "Text"], Cell[BoxData[ \(\(\ \ \ \ \ \ \ \ \ \ \ EulerMethod[f, {x0, y0}, h, n]\)\)], "Input"], Cell[TextData[ "where f is the function you defined, x0 and y0 are the initial data, h is \ the step size, and n is the number of steps taken. The routine then outputs \ a piecewise linear function of one variable that approximates the solution of \ the differential equation above. Notice that the domain of the function will \ extend from x0 to x0 + h \[Times] n, so you need to be careful about making \ sure you make n large enough so that the domain includes any points of \ interest. For the function f defined above, let's see what kind of output \ EulerMethod gives us and compare it with the exact solution obtained by \ DSolve. We'll try an initial value of y(0) = 1, a step size of .1, and 50 \ steps. This will give us an approximate solution defined for 0 < x < 5."], "Text"], Cell[BoxData[ \(approx\ = \ EulerMethod[f, {0, 2}, .1, \ 50]\)], "Input"], Cell[BoxData[ \(DSolve[{\(y'\)[x]\ == \ y[x]\ - \ x, \ \ y[0]\ == \ 2}, y[x], \ x] \)], "Input"], Cell[BoxData[ \(exactsol = \ y[x] /. First[%]\)], "Input"], Cell[BoxData[ \(Plot[{approx[x], \ exactsol}, \ {x, 0, 5}]\)], "Input"], Cell["\<\ So the solutions diverge quite a bit after x = 2. Let's see what \ effect a smaller step size has.\ \>", "Text"], Cell[BoxData[ \(approx\ = \ EulerMethod[f, {0, 2}, .01, \ 500]\)], "Input"], Cell[BoxData[ \(Plot[{approx[x], \ exactsol}, \ {x, 0, 5}]\)], "Input"], Cell["\<\ This looks quite a bit better, though still not perfect. Let's try \ dropping the step size by another factor of 10.\ \>", "Text"], Cell[BoxData[ \(approx\ = \ EulerMethod[f, {0, 2}, .001, \ 5000]\)], "Input"], Cell[BoxData[ \(Plot[{approx[x], \ exactsol}, \ {x, 0, 5}]\)], "Input"], Cell["\<\ The difference is now nearly impossible to see! It's important to \ realize that we've been depending on an exact solution in order to check the \ accuracy of our approximate solution. When an exact solution isn't \ available, we can at least compare approximate solutions (for different step sizes) with each other and see ifshrinking the \ step size affects the approximation very much. Let's try this idea out on a \ first order ODE with righthand side given by\ \>", "Text"], Cell[BoxData[ \(f[x_, y_]\ := \ x^2\ + \ y^2\)], "Input"], Cell[BoxData[ \(approx1\ = \ EulerMethod[f, {0, 1}, .1, 10]\)], "Input"], Cell[BoxData[ \(approx2\ = \ EulerMethod[f, {0, 1}, \ .01, \ 100]\)], "Input"], Cell[BoxData[ \(Plot[{approx1[x], approx2[x]}, \ {x, 0, .9}]\)], "Input"], Cell["\<\ Clearly at least one of these approximate solutions isn't \ trustworthy beyond x=.4! Let's try a third approximation with smaller step \ size.\ \>", "Text"], Cell[BoxData[ \(approx3\ = \ EulerMethod[f, {0, 1}, \ .001, \ 900]\)], "Input"], Cell[BoxData[ \(Plot[{approx1[x], approx2[x], \ approx3[x]}, \ {x, 0, .9}]\)], "Input"], Cell["\<\ It looks like things are starting to settle down from the second to \ the third approximation. Nevertheless, a further decrease in step size would \ likely yield a noticeable gain in accuracy...\ \>", "Text"] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, ScreenStyleEnvironment->"Presentation", WindowSize->{520, 600}, WindowMargins->{{277, Automatic}, {131, 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, 36, 0, 64, "Subsection"], Cell[1770, 53, 505, 9, 168, "Text"], Cell[2278, 64, 442, 7, 279, "Input"], Cell[2723, 73, 170, 5, 89, "Text"], Cell[2896, 80, 85, 1, 39, "Input"], Cell[2984, 83, 52, 1, 39, "Text"], Cell[3039, 86, 89, 1, 39, "Input"], Cell[3131, 89, 798, 11, 273, "Text"], Cell[3932, 102, 79, 1, 39, "Input"], Cell[4014, 105, 109, 2, 63, "Input"], Cell[4126, 109, 62, 1, 39, "Input"], Cell[4191, 112, 75, 1, 39, "Input"], Cell[4269, 115, 123, 3, 66, "Text"], Cell[4395, 120, 81, 1, 39, "Input"], Cell[4479, 123, 75, 1, 39, "Input"], Cell[4557, 126, 141, 3, 66, "Text"], Cell[4701, 131, 83, 1, 63, "Input"], Cell[4787, 134, 75, 1, 39, "Input"], Cell[4865, 137, 491, 8, 181, "Text"], Cell[5359, 147, 63, 1, 39, "Input"], Cell[5425, 150, 78, 1, 39, "Input"], Cell[5506, 153, 84, 1, 39, "Input"], Cell[5593, 156, 78, 1, 39, "Input"], Cell[5674, 159, 169, 4, 66, "Text"], Cell[5846, 165, 85, 1, 63, "Input"], Cell[5934, 168, 92, 1, 87, "Input"], Cell[6029, 171, 219, 4, 89, "Text"] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)