(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. 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[ 9170, 358]*) (*NotebookOutlinePosition[ 9977, 386]*) (* CellTagsIndexPosition[ 9933, 382]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell[TextData[{ "Differential Equations with ", StyleBox["Mathematica", FontSlant->"Italic"] }], "Title", TextAlignment->Center, TextJustification->0, FontSize->18], Cell[TextData[{ "In this notebook we'll collect examples of how to use ", StyleBox["Mathematica", FontSlant->"Italic"], " to analyze differential equations. \n\nSometimes we can solve \ differential equations in symbolic form using DSolve and sometimes we need to \ work with a numerical solution, using NDSolve. There are also different ways \ to represent derivatives; we'll include examples below. More detail can be \ found using the Help Browser under DSolve (including more examples)." }], "Text"], Cell[CellGroupData[{ Cell["Clear symbols", "Section", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ In order to avoid interference from symbols defined in other \ notebooks, we first Clear all symbols. We assume that the relevant symbols \ are in the Global` context.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Clear[\"Global`*\"]", "Input", AspectRatioFixed->True] }, Open ]], Cell[CellGroupData[{ Cell["Example 1 -- First-Order Separable ", "Section", FontSize->14], Cell["\<\ First we give a name (de1) to the differential equation. We use \ the notation y'[x] to indicate dy/dx. Note that we use = in defining the \ name but == in the actual equation. \ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(de1\ = \ \ \ \(y'\)[x]\ == \ a*x^2\ + \ b\)], "Input"], Cell[BoxData[ RowBox[{ RowBox[{ SuperscriptBox["y", "\[Prime]", MultilineFunction->None], "[", "x", "]"}], "==", \(b + a\ x\^2\)}]], "Output"] }, Open ]], Cell["\<\ The command to solve the equation symbolically is called \"DSolve\" \ (note the capital letters!). Here we solve the differential equation with an \ intial condition that y(x=0) = y0, which translates into y[0]==y0: \ \>", \ "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(result\ = \ \ DSolve[\ {de1, \ y[0] \[Equal] y0}, \ y[x], \ x]\)], "Input"], Cell[BoxData[ \({{y[x] \[Rule] 1\/3\ \((3\ b\ x + a\ x\^3 + 3\ y0)\)}}\)], "Output"] }, Open ]], Cell[TextData[{ "The solution for y[x] is in the double set of {}'s. We can extract the \ function part by using ", StyleBox["Mathematica", FontSlant->"Italic"], "'s commands for extracting nested expressions:" }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(result[\([1, 1, 2]\)]\)], "Input"], Cell[BoxData[ \(1\/3\ \((3\ b\ x + a\ x\^3 + 3\ y0)\)\)], "Output"] }, Open ]], Cell["We can use this directy to define a function f:", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(f[x_]\ = \ result[\([1, 1, 2]\)]\)], "Input"], Cell[BoxData[ \(1\/3\ \((3\ b\ x + a\ x\^3 + 3\ y0)\)\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(f[z]\)], "Input"], Cell[BoxData[ \(1\/3\ \((3\ y0 + 3\ b\ z + a\ z\^3)\)\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(f[2]\)], "Input"], Cell[BoxData[ \(1\/3\ \((8\ a + 6\ b + 3\ y0)\)\)], "Output"] }, Open ]], Cell["\<\ We have to be more careful if we want to define the function with \ := (delayed):\ \>", "Text"], Cell[BoxData[ \(g[x_]\ := \ result[\([1, 1, 2]\)]\)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(g[z]\)], "Input"], Cell[BoxData[ \(1\/3\ \((3\ b\ x + a\ x\^3 + 3\ y0)\)\)], "Output"] }, Open ]], Cell["Use ?g to see the problem:", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(\(?g\)\)], "Input"], Cell[BoxData[ \("Global`g"\)], "Print"], Cell[BoxData[ InterpretationBox[GridBox[{ {GridBox[{ {\(g[x_] := result\[LeftDoubleBracket]1, 1, 2\[RightDoubleBracket]\)} }, GridBaseline->{Baseline, {1, 1}}, ColumnWidths->0.999, ColumnAlignments->{Left}]} }, GridBaseline->{Baseline, {1, 1}}, ColumnAlignments->{Left}], Definition[ g], Editable->False]], "Print"] }, Open ]], Cell["\<\ We need to evaluate the rhs immediately, so use Evaluate in the \ definition:\ \>", "Text"], Cell[BoxData[ \(\(Clear[g];\)\)], "Input"], Cell[BoxData[ \(g[x_]\ := \ Evaluate[result[\([1, 1, 2]\)]]\)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(\(?g\)\)], "Input"], Cell[BoxData[ \("Global`g"\)], "Print"], Cell[BoxData[ InterpretationBox[GridBox[{ {GridBox[{ {\(g[x_] := 1\/3\ \((3\ b\ x + a\ x\^3 + 3\ y0)\)\)} }, GridBaseline->{Baseline, {1, 1}}, ColumnWidths->0.999, ColumnAlignments->{Left}]} }, GridBaseline->{Baseline, {1, 1}}, ColumnAlignments->{Left}], Definition[ g], Editable->False]], "Print"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(g[y]\)], "Input"], Cell[BoxData[ \(1\/3\ \((3\ b\ y + a\ y\^3 + 3\ y0)\)\)], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Example 2 -- First-Order Separable", "Section", FontSize->14], Cell["\<\ Now we do another first-order equation that has the function being \ differentiated (x in this case) on the right-hand-side as well. Note that we \ have to write x[t] rather than just x, to indicate the dependence on the \ independent variable t.\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(de2\ = \ \ \ \(x'\)[t]\ == \ \(\(-A\)\(*\)\(x[t]\)\(\ \)\)\)], "Input"], Cell[BoxData[ RowBox[{ RowBox[{ SuperscriptBox["x", "\[Prime]", MultilineFunction->None], "[", "t", "]"}], "==", \(\(-A\)\ x[t]\)}]], "Output"] }, Open ]], Cell["Once again, we specify an initial condition.", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(answer2\ = \ DSolve[\ {de2, \ x[0] \[Equal] x0}, \ x[t], \ t]\)], "Input"], Cell[BoxData[ \({{x[t] \[Rule] \[ExponentialE]\^\(\(-A\)\ t\)\ x0}}\)], "Output"] }, Open ]], Cell["We find an exponential!", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["Example 3 -- Constant Acceleration due to gravity", "Section", FontSize->14], Cell["\<\ Next we do a familiar second-order equation: one-dimensional \ constant acceleration of -g. We could do it in two stages, finding v from a \ and then z from v, but we'll solve the full second-order equation at once, \ with initial conditions (at t=0) of v=v0 and z=z0.\ \>", "Text"], Cell[BoxData[ \(Remove[z, t, v0, z0, g]\)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(de3\ = \ \(z''\)[t]\ \[Equal] \ \(-g\)\)], "Input"], Cell[BoxData[ RowBox[{ RowBox[{ SuperscriptBox["z", "\[Prime]\[Prime]", MultilineFunction->None], "[", "t", "]"}], "==", \(-g\)}]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(answer3\ = \ DSolve[{de3, \(z'\)[0] \[Equal] v0, z[0] \[Equal] z0}, z[t], t]\)], "Input"], Cell[BoxData[ \({{z[t] \[Rule] \(-\(\(g\ t\^2\)\/2\)\) + t\ v0 + z0}}\)], "Output"] }, Open ]], Cell["\<\ Suppose we wanted to pick out the right-hand side of z[t] from this \ result. It is the second part of the first part of the first part of the \ result:\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(answer3[\([1, 1, 2]\)]\)], "Input"], Cell[BoxData[ \(\(-\(\(g\ t\^2\)\/2\)\) + t\ v0 + z0\)], "Output"] }, Open ]] }, Open ]] }, Open ]] }, FrontEndVersion->"4.0 for X", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowToolbars->"EditBar", WindowSize->{709, 925}, WindowMargins->{{111, Automatic}, {Automatic, 16}}, PrintingPageRange->{Automatic, Automatic}, PrintingOptions->{"PaperSize"->{612, 792}, "PaperOrientation"->"Portrait", "Magnification"->1} ] (*********************************************************************** 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[1739, 51, 178, 7, 93, "Title"], Cell[1920, 60, 518, 9, 104, "Text"], Cell[CellGroupData[{ Cell[2463, 73, 80, 2, 60, "Section", Evaluatable->False], Cell[2546, 77, 240, 6, 50, "Text", Evaluatable->False], Cell[2789, 85, 62, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2888, 91, 70, 1, 56, "Section"], Cell[2961, 94, 204, 4, 50, "Text"], Cell[CellGroupData[{ Cell[3190, 102, 77, 1, 27, "Input"], Cell[3270, 105, 177, 5, 29, "Output"] }, Open ]], Cell[3462, 113, 244, 5, 50, "Text"], Cell[CellGroupData[{ Cell[3731, 122, 105, 2, 27, "Input"], Cell[3839, 126, 88, 1, 44, "Output"] }, Open ]], Cell[3942, 130, 234, 6, 50, "Text"], Cell[CellGroupData[{ Cell[4201, 140, 54, 1, 27, "Input"], Cell[4258, 143, 71, 1, 44, "Output"] }, Open ]], Cell[4344, 147, 63, 0, 32, "Text"], Cell[CellGroupData[{ Cell[4432, 151, 66, 1, 27, "Input"], Cell[4501, 154, 71, 1, 44, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[4609, 160, 37, 1, 27, "Input"], Cell[4649, 163, 71, 1, 44, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[4757, 169, 37, 1, 27, "Input"], Cell[4797, 172, 65, 1, 44, "Output"] }, Open ]], Cell[4877, 176, 105, 3, 32, "Text"], Cell[4985, 181, 67, 1, 27, "Input"], Cell[CellGroupData[{ Cell[5077, 186, 37, 1, 27, "Input"], Cell[5117, 189, 71, 1, 44, "Output"] }, Open ]], Cell[5203, 193, 42, 0, 32, "Text"], Cell[CellGroupData[{ Cell[5270, 197, 39, 1, 27, "Input"], Cell[5312, 200, 43, 1, 23, "Print"], Cell[5358, 203, 462, 13, 23, "Print"] }, Open ]], Cell[5835, 219, 101, 3, 32, "Text"], Cell[5939, 224, 46, 1, 27, "Input"], Cell[5988, 227, 77, 1, 27, "Input"], Cell[CellGroupData[{ Cell[6090, 232, 39, 1, 27, "Input"], Cell[6132, 235, 43, 1, 23, "Print"], Cell[6178, 238, 424, 12, 30, "Print"] }, Open ]], Cell[CellGroupData[{ Cell[6639, 255, 37, 1, 27, "Input"], Cell[6679, 258, 71, 1, 44, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[6799, 265, 69, 1, 56, "Section"], Cell[6871, 268, 271, 5, 50, "Text"], Cell[CellGroupData[{ Cell[7167, 277, 93, 1, 27, "Input"], Cell[7263, 280, 178, 5, 27, "Output"] }, Open ]], Cell[7456, 288, 60, 0, 32, "Text"], Cell[CellGroupData[{ Cell[7541, 292, 102, 2, 27, "Input"], Cell[7646, 296, 85, 1, 28, "Output"] }, Open ]], Cell[7746, 300, 39, 0, 32, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[7822, 305, 84, 1, 56, "Section"], Cell[7909, 308, 294, 5, 68, "Text"], Cell[8206, 315, 56, 1, 27, "Input"], Cell[CellGroupData[{ Cell[8287, 320, 73, 1, 27, "Input"], Cell[8363, 323, 169, 4, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[8569, 332, 126, 3, 27, "Input"], Cell[8698, 337, 87, 1, 46, "Output"] }, Open ]], Cell[8800, 341, 177, 4, 50, "Text"], Cell[CellGroupData[{ Cell[9002, 349, 55, 1, 27, "Input"], Cell[9060, 352, 70, 1, 46, "Output"] }, Open ]] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)