(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 4.2' 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[ 9171, 258]*) (*NotebookOutlinePosition[ 9801, 280]*) (* CellTagsIndexPosition[ 9757, 276]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell[TextData[{ "A note about Lists in ", StyleBox["Mathematica", FontSlant->"Italic"] }], "Section", FontSize->14], Cell[TextData[{ "The basic data structure in ", StyleBox["Mathematica", FontSlant->"Italic"], " is a ", StyleBox["list", FontSlant->"Italic"], ". One can have simple lists of numbers, such as\n\t{1, 3, 7, 2, 5},\n\ lists of plots, such as this one containing two plots\n\tp = {Plot[f[x], {x, \ 1,3}], Plot[2+f[x], {x, 1, 5}]},\neven lists of lists\n\tpts = {{1, 1}, {2, \ 2}, {3, 0}}.\nLists can be created/assigned with commands like those above. \ Often a large list may also be created more succinctly. For, instance, the \ command\n\tp = Table[i, {i,1,10}]\ncreates a list called p and is equivalent \ to the statement\n\tp = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.\nAnother pair of \ commands which also would create this list is\n\t", StyleBox["p = { }\n\tDo[AppendTo[p, i], {i, 1, 10}],", "DisplayFormula"], "\nwhich causes i to take on all integer values between 1 and 10, appending \ each value to a list p which begins empty." }], "Text", FontSize->14], Cell[TextData[{ "In the course of using Euler's method, we will be computing values of an \ unknown function --- a function that solves a 1st-order ODE of the form\n\t", Cell[BoxData[ \(TraditionalForm\`dy\/dt\ = \ f(t, \ y)\)]], "\n--- at evenly-spaced t-values. To get a feel for the relationship these \ points nearly have to the function itself, execute the following cells:" }], "Text", FontSize->14], Cell[BoxData[{ \(g[x_] := x^3 - 3 x + 1\), "\[IndentingNewLine]", \(Plot[g[x], {x, \(-2.25\), 2.25}]\)}], "Input", FontSize->14], Cell[BoxData[{ \(\(pts = Table[{x, g[x]}, {x, \(-2.25\), 2.25, 0.05}];\)\), "\[IndentingNewLine]", \(ListPlot[pts]\)}], "Input"], Cell["\<\ The second cell creates a table of points, all on the graph of the \ continuous function g, but all spaced 0.05 units apart in their x-coordinate. \ The ListPlot[ ] command produces a plot of these points.\ \>", "Text", FontSize->14] }, Closed]], Cell[CellGroupData[{ Cell["An Euler Method algorithm", "Section", FontSize->14], 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 cell." }], "Text", FontSize->14], Cell[BoxData[{ \(Clear[f, t, y]\[IndentingNewLine]\ \ \ (*\ This\ Clear\ is\ just\ a\ precaution, \ in\ case\ any\ of\ these\ three\ have\ values\ currently\ assigned\ to\ \ them\ *) \[IndentingNewLine]\[IndentingNewLine] (*\ Assignments\ pertinent\ to\ our\ problem\ *) \), "\[IndentingNewLine]", \ \(f[t_, y_] := y\ - \ Sin[t]\), "\[IndentingNewLine]", \(\(y0\ = \ 0.5;\)\), "\[IndentingNewLine]", \(\(t0\ = \ 0;\)\), "\[IndentingNewLine]", \(\(tLast\ = \ 5;\)\), "\[IndentingNewLine]", \(\(h\ = \ 0.1;\)\[IndentingNewLine]\), "\[IndentingNewLine]", \(\(yVals\ = \ {y0};\)\), "\[IndentingNewLine]", \(\(pts\ = \ {{t0, y0}};\)\), "\[IndentingNewLine]", \(\(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]AppendTo[ pts, {tNew, yNew}]; \[IndentingNewLine]yCurrent\ = \ yNew; \[IndentingNewLine]tCurrent = tNew\[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[{ "What was achieved in the last cell is the production of 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 (try it; see above for an example) \ to show the graph of ", StyleBox["y(t)", FontSlant->"Italic"], ". 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\t\ - soon you will be taught how to do so, and\n\t- ", 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 indicated \ as 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"], " 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["\<\ 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 where it is used is all that \ is required) 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->"4.2 for X", ScreenRectangle->{{0, 1024}, {0, 768}}, WindowSize->{520, 600}, WindowMargins->{{Automatic, 129}, {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, 125, 5, 58, "Section"], Cell[1904, 60, 982, 19, 337, "Text"], Cell[2889, 81, 421, 8, 111, "Text"], Cell[3313, 91, 140, 3, 48, "Input"], Cell[3456, 96, 159, 4, 43, "Input"], Cell[3618, 102, 246, 5, 71, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[3901, 112, 60, 1, 36, "Section"], Cell[3964, 115, 483, 13, 92, "Text"], Cell[4450, 130, 1259, 24, 395, "Input"], Cell[5712, 156, 250, 5, 71, "Text"], Cell[5965, 163, 503, 14, 90, "Text"], Cell[6471, 179, 92, 2, 43, "Input"], Cell[6566, 183, 247, 7, 52, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[6850, 195, 117, 4, 41, "Section"], Cell[6970, 201, 765, 17, 204, "Text"], Cell[7738, 220, 82, 1, 27, "Input"], Cell[7823, 223, 730, 15, 166, "Text"], Cell[8556, 240, 110, 2, 27, "Input"], Cell[8669, 244, 314, 6, 90, "Text"], Cell[8986, 252, 169, 3, 59, "Input"] }, Closed]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)