(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.0' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing 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[ 8190, 229]*) (*NotebookOutlinePosition[ 8820, 251]*) (* CellTagsIndexPosition[ 8776, 247]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["An Euler Method Algorithm", "Section"], Cell[TextData[{ "The following algorithm implements an Euler Method solution of\n\t", Cell[BoxData[ \(TraditionalForm\`\(\(dy\/dt\ = \ y\ - \ sin\ t\)\(,\)\)\)]], " with initial condition ", Cell[BoxData[ \(TraditionalForm\`y(0)\ = \ \(\(0.5\)\(.\)\)\)]], "\nThe stepsize is set to 0.1, and we compute approximate points on the \ solution up until ", StyleBox["t", FontSlant->"Italic"], " = 5. Execute the cells with the key (far right on keyboard; on a \ PC execute with -)." }], "Text", FontSize->14], Cell[BoxData[ \(\(\(\ \ \ \)\( (*\ Initialize\ values\ *) \)\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\ \(Clear[f, t, y]\[IndentingNewLine] \(f[t_, y_] := y\ - \ Sin[t];\)\[IndentingNewLine] \(t0\ = \ 0;\)\[IndentingNewLine] \(y0\ = \ 0.5;\)\[IndentingNewLine] \(h\ = \ 0.1;\)\[IndentingNewLine] \(tLast\ = \ 5;\)\[IndentingNewLine]\[IndentingNewLine] \)\)\)], "Input"], Cell[BoxData[ \(\(\( (*\ Start\ separate\ lists\ of\ y - values, \ and\ \((t, y)\)\ points\ through\ which\ the\ solution\ passes . \ \ Both\ \ contain\ just\ our\ initial\ condition\ at\ the\ outset . \ \ Note\ the\ \ curly\ brackets\ throughout, \ rather\ than\ regular\ parentheses, \ which\ are\ never\ used\ for\ anything\ other\ than\ grouping\ in\ \ \(\(Mathematica\)\(.\)\)\ *) \)\(\[IndentingNewLine]\)\(\ \)\(\ \[IndentingNewLine]\)\(\(yVals\ = \ {y0};\)\[IndentingNewLine] \(pts\ = \ {{t0, y0}};\)\[IndentingNewLine]\[IndentingNewLine] \)\)\)], "Input"], Cell[BoxData[{ \(\(tCurrent\ = \ t0;\)\), "\[IndentingNewLine]", \(\(yCurrent\ = \ y0;\)\), "\[IndentingNewLine]", \(While[ tCurrent\ < \ tLast, \[IndentingNewLine]yNew\ = \ yCurrent\ + \ h*f[tCurrent, \ yCurrent]; \[IndentingNewLine]tNew\ = \ tCurrent\ + \ h; \[IndentingNewLine]AppendTo[yVals, yNew]; \[IndentingNewLine]\ \ \ \ \ \ \ \ \ (*\ add\ new\ y - value\ to\ list\ *) \[IndentingNewLine]\[IndentingNewLine]AppendTo[ pts, {tNew, yNew}]; \[IndentingNewLine]\ \ \ \ \ \ \ \ (*\ add\ new\ \((t, y)\)\ point\ to\ list\ \ *) \[IndentingNewLine]\[IndentingNewLine]yCurrent\ = \ yNew; \[IndentingNewLine]tCurrent = tNew\[IndentingNewLine]]\[IndentingNewLine]\), "\[IndentingNewLine]", \ \(\)}], "Input"], Cell["\<\ The first time you execute the cell above, you will get a number of \ warnings about the names I chose for my variables. They are not errors, just \ warnings, and if you re-execute the cell, they will go away.\ \>", "Text", FontSize->14], Cell[TextData[{ "The previous cell produced two lists, one containing just the approximate \ y-values, and the other containing (", StyleBox["t, y", FontSlant->"Italic"], ") points on the solution curve ", StyleBox["y(t)", FontSlant->"Italic"], ". One can use the ListPlot[ ] command to show the resulting approximate \ graph of ", StyleBox["y(t)", FontSlant->"Italic"], ". (ListPlot[ ] is used instead of the Plot[ ] command you may have used \ so often in Math 162. This is because, rather than having a formula for ", StyleBox["y(t)", FontSlant->"Italic"], ", what we have is a list of distinct points it passes through.)" }], "Text", FontSize->14], Cell[BoxData[ \(ListPlot[pts]\)], "Input"], Cell["One can also use commands like", "Text", FontSize->14], Cell[BoxData[{ \(Last[yVals]\), "\[IndentingNewLine]", \(pts[\([3]\)]\)}], "Input"], Cell[TextData[{ "in order to view the very last element in the list yVals (the last y-value \ that was computed) and the 2nd computed point in the list ", StyleBox["pts", FontSlant->"Italic"], " respectively." }], "Text", FontSize->14] }, Closed]], Cell[CellGroupData[{ Cell[TextData[{ "Finding explicit solutions in ", StyleBox["Mathematica", FontSlant->"Italic"] }], "Section"], Cell[TextData[{ "The initial-value problem\n\tdy/dt = ", Cell[BoxData[ \(TraditionalForm\`y\ - \ sin(t)\)]], ", y(0) = 0.5\ninvolves a linear ODE and an initial condition. Being \ among the easier kinds of ODEs to solve explicitly, you would expect that\n \ - soon you will be taught how to do so, and\n - ", StyleBox["Mathematica", FontSlant->"Italic"], " probably has the capability to find the solution.\nIndeed, both are the \ case. For the purposes of comparisons between the numerical method and the \ actual solution, we will get the actual solution using ", StyleBox["Mathematica", FontSlant->"Italic"], ". The pertinent command is DSolve[ ]. It may be used in a couple of \ ways. The cell" }], "Text", FontSize->14], Cell[BoxData[ \(DSolve[\(y'\)[t] \[Equal] y[t] - Sin[t], y[t], t]\)], "Input"], Cell[TextData[{ "produces a 1-parameter family of solutions. (The parameter is denoted \ C[1].) This indicates that there are many solutions of the ODE, one for each \ choice of the parameter. We have seen this kind of thing before. As you may \ recall, specifying that we wish for a solution that passes through a specific \ point (in the above case that point is (0, 0.5)) will usually nail down a \ specific solution. To get ", StyleBox["Mathematica", FontSlant->"Italic"], " to find the specific solution that passes through this point, we give it \ not just the DE to solve, but a ", StyleBox["list", FontSlant->"Italic"], " (Note the curly brackets enclosing these equations, and the use of double \ equals signs) of equations to solve, as in the next cell." }], "Text", FontSize->14], Cell[BoxData[ \(DSolve[{\(y'\)[t] \[Equal] y[t] - Sin[t], y[0] \[Equal] 0.5}, y[t], t]\)], "Input"], Cell[TextData[{ "You may plot both your approximated points and the actual solution \ together using a command like DisplayTogether[ ], which is not a built-in \ command and must be imported (once per session is all that is required, ", StyleBox["prior to the first time you use it", FontSlant->"Italic"], ") as a part of the \"Graphics\" package." }], "Text", FontSize->14], Cell[BoxData[{ \(<< Graphics`Graphics`\), "\[IndentingNewLine]", \(DisplayTogether[ListPlot[pts], Plot[\((Cos[t] + Sin[t])\)/2, {t, 0, 5}]]\)}], "Input"] }, Closed]] }, FrontEndVersion->"5.0 for X", ScreenRectangle->{{0, 1024}, {0, 768}}, WindowSize->{520, 600}, WindowMargins->{{Automatic, 113}, {Automatic, 49}} ] (******************************************************************* 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[1776, 53, 44, 0, 75, "Section"], Cell[1823, 55, 568, 14, 111, "Text"], Cell[2394, 71, 413, 9, 171, "Input"], Cell[2810, 82, 603, 10, 203, "Input"], Cell[3416, 94, 846, 18, 267, "Input"], Cell[4265, 114, 250, 5, 71, "Text"], Cell[4518, 121, 690, 18, 147, "Text"], Cell[5211, 141, 46, 1, 27, "Input"], Cell[5260, 144, 62, 1, 33, "Text"], Cell[5325, 147, 92, 2, 43, "Input"], Cell[5420, 151, 247, 7, 71, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[5704, 163, 117, 4, 45, "Section"], Cell[5824, 169, 773, 17, 204, "Text"], Cell[6600, 188, 82, 1, 27, "Input"], Cell[6685, 191, 815, 16, 204, "Text"], Cell[7503, 209, 110, 2, 27, "Input"], Cell[7616, 213, 386, 8, 109, "Text"], Cell[8005, 223, 169, 3, 59, "Input"] }, Closed]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)