(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.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[ 8257, 309]*) (*NotebookOutlinePosition[ 9324, 344]*) (* CellTagsIndexPosition[ 9280, 340]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Difference Equations", "Title", Evaluatable->False, TextAlignment->Center, AspectRatioFixed->True], Cell[TextData[{ "Sean Mauch\nsean@caltech.edu\n", ButtonBox["http://www.its.caltech.edu/~sean", ButtonData:>{ URL[ "http://www.its.caltech.edu/~sean"], None}, ButtonStyle->"Hyperlink"], "\n", "This work is distributed under the GNU FDL. See ", ButtonBox["license.nb ", ButtonData:>{"license.nb", None}, ButtonStyle->"Hyperlink"], "for details." }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["The Fibonacci Sequence", "Section"], Cell["The difference equation for the Fibonacci sequence is", "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ FormBox[ RowBox[{\(a\_\(n + 2\) = a\_\(n + 1\) + a\_n\), ",", " ", FormBox[\(a\_1 = \(a\_2 = 1 . \)\), "TraditionalForm"]}], TraditionalForm]], "DisplayFormula", TextAlignment->Center, TextJustification->0], Cell[BoxData[ \(Clear[a]\)], "Input"], Cell[TextData[{ "You cannot define this recurrence relation with the ", Cell[BoxData[ \(TraditionalForm\` = \)]], " assignment operator. Try executing the next command. After a few error \ messages pop up you can abort the command by pressing \ \[AltKey]\[LeftModified].\[RightModified]." }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(\(a[n_] = a[n - 1] + a[n - 2]; \)\)], "Input"], Cell[TextData[{ "The above command starts an infinite recursion. This is because when you \ use regular assignment, the right hand side is evaluated. First the right \ hand side is set to ", Cell[BoxData[ \(TraditionalForm\`a[n - 1] + a[n - 2]\)]], " then it is set to ", Cell[BoxData[ \(TraditionalForm\`a[n - 2] + 2 a[n - 3] + a[n - 4]\)]], " then to ", Cell[BoxData[ \(TraditionalForm\`a[n - 3] + 3 a[n - 4] + 3 a[n - 5] + a[n - 6]\)]], " and so on infinitum.\nThe correct way to define this recurrence relation \ in ", StyleBox["Mathematica", FontSlant->"Italic"], " is" }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(a[n_] := a[n - 1] + a[n - 2]\)], "Input"], Cell[TextData[{ "Recall that ", Cell[BoxData[ \(TraditionalForm\` := \)]], " is a delayed assignment. The right hand side is not evaluated. We \ specify the initial conditions with ordinary assignment. Now if we ask ", StyleBox["Mathematica", FontSlant->"Italic"], " what it knows about ", Cell[BoxData[ \(TraditionalForm\`a\)]], " it will give us the recursive definition." }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(\(?a\)\)], "Input"], Cell["We specify the initial conditions with ordinary assignment", "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(a[1] = 1; \na[2] = 1; \)], "Input"], Cell[BoxData[ \(\(?a\)\)], "Input"], Cell[TextData[{ "Now when we input ", Cell[BoxData[ \(TraditionalForm\`a[5]\)]], ", ", StyleBox["Mathematica", FontSlant->"Italic"], " first applies the recursive definition until the expression is defined in \ terms of ", Cell[BoxData[ \(TraditionalForm\`a[2]\)]], " and ", Cell[BoxData[ \(TraditionalForm\`a[1]\)]], ". Then it can compute a number." }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(a[5]\)], "Input"], Cell[BoxData[ \(Table[{n, a[n]}, {n, 1, 10}] // TableForm\)], "Input"], Cell[TextData[{ "If you will be computing a lot of the coefficients, this is very \ inefficient. If you ask ", StyleBox["Mathematica", FontSlant->"Italic"], " to evaluate ", Cell[BoxData[ \(TraditionalForm\`a[1000000]\)]], ", ", Cell[BoxData[ \(TraditionalForm\`a[1000001]\)]], " and ", Cell[BoxData[ \(TraditionalForm\`a[1000002]\)]], " it will compute each of them \"from scratch\". It would be handy if we \ could tell ", StyleBox["Mathematica", FontSlant->"Italic"], " to remember the values that it computes. Note that ", StyleBox["Mathematica", FontSlant->"Italic"], " has computed ", Cell[BoxData[ \(TraditionalForm\`a[3], \[Ellipsis], a[10]\)]], " but it hasn't stored those values for future reference." }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(\(?a\)\)], "Input"], Cell["First we remove the old definitions.", "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(Clear[a]\)], "Input"], Cell["Now we take care of the initial conditions", "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(a[1] = 1; \na[2] = 1; \)], "Input"], Cell[TextData[{ "We tell ", StyleBox["Mathematica", FontSlant->"Italic"], " to remember the values that it computes with the command" }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(a[n_] := \(a[n] = a[n - 1] + a[n - 2]\)\)], "Input"], Cell[TextData[{ "Now when ", StyleBox["Mathematica", FontSlant->"Italic"], " encounters the expression ", Cell[BoxData[ \(TraditionalForm\`a[5]\)]], " as input, it executes the command\n\t", Cell[BoxData[ \(TraditionalForm\`a[5] = a[4] + a[3]\)]], "\nwhich first computes the value of ", Cell[BoxData[ \(TraditionalForm\`a[4]\)]], " and ", Cell[BoxData[ \(TraditionalForm\`a[3]\)]], " and then sets the value of ", Cell[BoxData[ \(TraditionalForm\`a[5]\)]], " to 5." }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(a[5]\)], "Input"], Cell[TextData[{ "Now when we ask ", StyleBox["Mathematica", FontSlant->"Italic"], " what it knows about ", Cell[BoxData[ \(TraditionalForm\`a\)]], ", we see that it has stored all the values that it has computed." }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(\(?a\)\)], "Input"], Cell[TextData[{ "We can look at the series in a graph by making a table of values and then \ using ", Cell[BoxData[ FormBox[ StyleBox[\(ListPlot[]\), "Input"], TraditionalForm]]], "." }], "Text", TextAlignment->Left, TextJustification->1], Cell[BoxData[ \(Table[{n, a[n]}, {n, 1, 20}]\)], "Input"], Cell[BoxData[ \(\(ListPlot[%, PlotStyle -> PointSize[0.01], PlotRange -> All]; \)\)], "Input"], Cell[BoxData[ \(Clear[a]\)], "Input"] }, Closed]] }, Open ]] }, FrontEndVersion->"5.2 for Microsoft Windows", ScreenRectangle->{{0, 1680}, {0, 963}}, WindowToolbars->"EditBar", CellGrouping->Automatic, WindowSize->{756, 765}, WindowMargins->{{2, Automatic}, {Automatic, 5}}, PrintingPageRange->{Automatic, Automatic}, PrintingOptions->{"PaperSize"->{612, 792}, "PaperOrientation"->"Portrait", "Magnification"->1}, PrivateNotebookOptions->{"ColorPalette"->{RGBColor, 128}}, ShowCellLabel->True, ShowCellTags->False, RenderingOptions->{"ObjectDithering"->True, "RasterDithering"->False}, CharacterEncoding->"XAutomaticEncoding", Magnification->1.5 ] (******************************************************************* 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, 110, 3, 141, "Title", Evaluatable->False], Cell[1889, 58, 439, 14, 125, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[2353, 76, 41, 0, 109, "Section"], Cell[2397, 78, 116, 2, 47, "Text"], Cell[2516, 82, 256, 6, 35, "DisplayFormula"], Cell[2775, 90, 41, 1, 42, "Input"], Cell[2819, 93, 361, 9, 99, "Text"], Cell[3183, 104, 66, 1, 42, "Input"], Cell[3252, 107, 678, 19, 151, "Text"], Cell[3933, 128, 61, 1, 42, "Input"], Cell[3997, 131, 466, 14, 99, "Text"], Cell[4466, 147, 39, 1, 42, "Input"], Cell[4508, 150, 121, 2, 47, "Text"], Cell[4632, 154, 55, 1, 68, "Input"], Cell[4690, 157, 39, 1, 42, "Input"], Cell[4732, 160, 451, 17, 73, "Text"], Cell[5186, 179, 37, 1, 42, "Input"], Cell[5226, 182, 74, 1, 42, "Input"], Cell[5303, 185, 840, 27, 151, "Text"], Cell[6146, 214, 39, 1, 42, "Input"], Cell[6188, 217, 99, 2, 47, "Text"], Cell[6290, 221, 41, 1, 42, "Input"], Cell[6334, 224, 105, 2, 47, "Text"], Cell[6442, 228, 55, 1, 68, "Input"], Cell[6500, 231, 202, 7, 47, "Text"], Cell[6705, 240, 72, 1, 42, "Input"], Cell[6780, 243, 585, 22, 125, "Text"], Cell[7368, 267, 37, 1, 42, "Input"], Cell[7408, 270, 292, 10, 73, "Text"], Cell[7703, 282, 39, 1, 42, "Input"], Cell[7745, 285, 272, 10, 73, "Text"], Cell[8020, 297, 61, 1, 42, "Input"], Cell[8084, 300, 101, 2, 68, "Input"], Cell[8188, 304, 41, 1, 42, "Input"] }, Closed]] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)