(*********************************************************************** 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[ 6468, 188]*) (*NotebookOutlinePosition[ 7146, 212]*) (* CellTagsIndexPosition[ 7102, 208]*) (*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", FontFamily->"Times"], 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", FontFamily->"Times"], Cell[BoxData[ \(\(\ \ \ \ \ \ \ \ \ \ \ f[x_, y_]\ := \ y\ - \ x\)\)], "Input"], Cell[BoxData[ \(TextForm\`Then\ type\)], "Text", FontFamily->"Times"], Cell[BoxData[ \(\(\ \ \ \ \ \ \ \ \ \ \ EulerMethod[f, {x0, y0}, h, n]\)\)], "Input"], Cell["\<\ 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", FontFamily->"Times"], 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", FontFamily->"Times"], 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", FontFamily->"Times"], 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 if shrinking 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", FontFamily->"Times"], 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", FontFamily->"Times"], 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", FontFamily->"Times"] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, ScreenStyleEnvironment->"Presentation", WindowSize->{520, 600}, WindowMargins->{{Automatic, 188}, {Automatic, 114}} ] (*********************************************************************** 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, 71, "Subsection"], Cell[1770, 53, 528, 10, 189, "Text"], Cell[2301, 65, 442, 7, 292, "Input"], Cell[2746, 74, 193, 6, 101, "Text"], Cell[2942, 82, 85, 1, 39, "Input"], Cell[3030, 85, 75, 2, 43, "Text"], Cell[3108, 89, 89, 1, 39, "Input"], Cell[3200, 92, 817, 13, 317, "Text"], Cell[4020, 107, 79, 1, 39, "Input"], Cell[4102, 110, 109, 2, 63, "Input"], Cell[4214, 114, 62, 1, 39, "Input"], Cell[4279, 117, 75, 1, 39, "Input"], Cell[4357, 120, 146, 4, 74, "Text"], Cell[4506, 126, 81, 1, 39, "Input"], Cell[4590, 129, 75, 1, 39, "Input"], Cell[4668, 132, 164, 4, 74, "Text"], Cell[4835, 138, 83, 1, 63, "Input"], Cell[4921, 141, 75, 1, 39, "Input"], Cell[4999, 144, 515, 9, 209, "Text"], Cell[5517, 155, 63, 1, 39, "Input"], Cell[5583, 158, 78, 1, 39, "Input"], Cell[5664, 161, 84, 1, 39, "Input"], Cell[5751, 164, 78, 1, 39, "Input"], Cell[5832, 167, 192, 5, 74, "Text"], Cell[6027, 174, 85, 1, 63, "Input"], Cell[6115, 177, 92, 1, 87, "Input"], Cell[6210, 180, 242, 5, 101, "Text"] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)