(*********************************************************************** 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[ 10033, 416]*) (*NotebookOutlinePosition[ 10673, 439]*) (* CellTagsIndexPosition[ 10629, 435]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Debugging Mathematica", "Title", TextAlignment->Center, TextJustification->0, FontSize->18], Cell[TextData[{ "In this notebook, we'll look at some ways to debug ", StyleBox["Mathematica", FontSlant->"Italic"], " expressions and programs." }], "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 and Remove all symbols. We assume that the \ relevant symbols are in the Global` context.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Clear[\"Global`*\"]", "Input", AspectRatioFixed->True], Cell["Remove[\"Global`*\"]", "Input", AspectRatioFixed->True] }, Open ]], Cell[CellGroupData[{ Cell["Common pitfalls", "Section", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ "Pay attention to ", StyleBox["Mathematica", FontSlant->"Italic"], "'s warning messages, they may indicate one of these pitfalls!" }], "Text"], Cell[CellGroupData[{ Cell[" Different brackets [, (, { mean different things. ", "Subsubsection", Evaluatable->False, AspectRatioFixed->True], Cell["You have to use the correct bracket and they must be paired.", "Text"], Cell[BoxData[ \(Sqrt \((4)\)\)], "Input"], Cell[BoxData[ \(Sqrt[4]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ " ", StyleBox["Mathematica", FontSlant->"Italic"], " is case sensitive. " }], "Subsubsection", Evaluatable->False, AspectRatioFixed->True], Cell["So Sqrt is different from sqrt.", "Text"], Cell[BoxData[ \(sqrt[4]\)], "Input"], Cell[BoxData[ \(Sqrt[4]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell[" Typograhical errors", "Subsubsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " will give you warnings if it thinks you may have mispelled a command or a \ variable. The warnings are harmless if you really did spell the name \ correctly (and it just happens to be close to another)." }], "Text"], Cell["\<\ To minimize spelling errors: * cut and paste whenever possible (e.g., package names from the Help \ Browser) * use \"command completion\" (ctrl-k)\ \>", "Text"] }, Open ]], Cell[CellGroupData[{ Cell[" Be careful of spaces!", "Subsubsection", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ If you use spaces to implicitly indicate multiplication, don't \ forget the spaces! For example:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(e\ = \ mc^2\)], "Input"], Cell["\<\ We have set e equal to the square of a variable mc, rather than m * \ c^2. Correct:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(e\ = \ m\ c^2\)], "Input"], Cell["\<\ To avoid problems, use plenty of spaces (extras don't matter) and \ use * if there is any chance of confusion.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True] }, Open ]], Cell[CellGroupData[{ Cell["Shadowing", "Subsubsection"], Cell["\<\ If you refer to a function before loading the package that defines \ that function, you may find that the function does not work. You need to \ Clear or Remove the definition before reloading the package.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Suppose you know that the speed of light is stored as SpeedOfLight:\ \ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(SpeedOfLight\)], "Input"], Cell["Oops, forgot to load the appropriate package:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(<< Miscellaneous`PhysicalConstants`\)], "Input"], Cell["Still doesn't work: the definition is shadowed!", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(SpeedOfLight\)], "Input"], Cell["Use Remove, then all is well:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(Remove[SpeedOfLight]\)], "Input"], Cell[BoxData[ \(\(?SpeedOfLight\)\)], "Input"], Cell[BoxData[ \(SpeedOfLight\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Underscores in function definitions", "Subsubsection"], Cell["\<\ When a function is defined, e.g., f[x_], don't forget the \ underscore _ on the left hand side. There are no underscores on the right \ hand side. (See below for an example.)\ \>", "Text", Evaluatable->False, AspectRatioFixed->True] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Using ? and ?? to learn about functions", "Section", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ "You can use ? and ?? to learn about ", StyleBox["Mathematica", FontSlant->"Italic"], " functions. " }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ If a function is not behaving as expected,try?function or \ ??function (for more detail) to see what's going on.\ \>", "Text"], Cell[BoxData[ \(\(?Sqrt\)\)], "Input"], Cell[BoxData[ \(?? Sqrt\)], "Input"], Cell["\<\ You can use * as a wildcard symbol. For example, to find all \ functions with \"Plot\" somewhere in the name:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(\(?*Plot*\)\)], "Input"], Cell["\<\ You should always check your own functions with ? and ?? to make \ sure they are defined as you expect. This can help you catch many \ errors.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Example: Forgetting the _", "Subsection"], Cell["\<\ Suppose you want to define f(x) to be sin(x) and you forgot about \ the _ in the function definition as well as the ['s.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(f[x]\ := \ Sin \((x)\)\)], "Input"], Cell["Now you remember, so you just type it again.", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(f[x_]\ := \ Sin[x]\)], "Input"], Cell["\<\ Checking f[Pi], you are satisfied that everything is ok. But then \ somewhere you use f[x]. Oops!\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(f[Pi]\)], "Input"], Cell[BoxData[ \(f[x]\)], "Input"], Cell["Find the problem using ?f:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(\(?f\)\)], "Input"], Cell["\<\ Fix it by using Clear on f, then redefining:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(Clear[f]\)], "Input"], Cell[BoxData[ \(f[x_]\ := \ Sin[x]\)], "Input"], Cell[BoxData[ \(\(?f\)\)], "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Using Trace", "Section", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ "If a ", StyleBox["Mathematica", FontSlant->"Italic"], " expression is passed to the Trace command, it returns a set of nested \ lists corresponding to the evaluations ", StyleBox["Mathematica", FontSlant->"Italic"], " performs in evaluating the expression. This can be very useful in \ discovering why ", StyleBox["Mathematica", FontSlant->"Italic"], " is not behaving as expected." }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Example: Numerical Precision", "Subsection"], Cell["\<\ Suppose we want to calculate the square root of 3 to 20 decimal \ places. We try: \ \>", "Text"], Cell[BoxData[ \(N[Sqrt[3. ], 20]\)], "Input"], Cell["\<\ This is not 20 places! On the other hand, if we omit the decimal \ point it works:\ \>", "Text"], Cell[BoxData[ \(N[Sqrt[3], 20]\)], "Input"], Cell["Use Trace on each line to see what is happening.", "Text"], Cell[BoxData[ \(Trace[N[Sqrt[3. ], 20]]\)], "Input"], Cell[BoxData[ \(Trace[N[Sqrt[3], 20]]\)], "Input"], Cell["\<\ Now we see what happens: In the first expression, the Sqrt is \ evaluated numerically immediately (because 3. is a floating point number), so \ the precision of the number given to N is only six digits. In the second \ case, the Sqrt evaluates to itself, so N is able to evaluate it to arbitrary precision.\ \>", "Text"] }, Open ]] }, Open ]] }, Open ]] }, FrontEndVersion->"4.0 for X", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{691, 889}, WindowMargins->{{29, Automatic}, {45, Automatic}} ] (*********************************************************************** 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, 103, 3, 93, "Title"], Cell[1845, 56, 167, 5, 32, "Text"], Cell[CellGroupData[{ Cell[2037, 65, 80, 2, 60, "Section", Evaluatable->False], Cell[2120, 69, 251, 6, 50, "Text", Evaluatable->False], Cell[2374, 77, 62, 1, 27, "Input"], Cell[2439, 80, 63, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2539, 86, 82, 2, 60, "Section", Evaluatable->False], Cell[2624, 90, 168, 5, 32, "Text"], Cell[CellGroupData[{ Cell[2817, 99, 126, 2, 42, "Subsubsection", Evaluatable->False], Cell[2946, 103, 76, 0, 32, "Text"], Cell[3025, 105, 45, 1, 27, "Input"], Cell[3073, 108, 40, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[3150, 114, 171, 7, 42, "Subsubsection", Evaluatable->False], Cell[3324, 123, 47, 0, 32, "Text"], Cell[3374, 125, 40, 1, 27, "Input"], Cell[3417, 128, 40, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[3494, 134, 94, 2, 42, "Subsubsection", Evaluatable->False], Cell[3591, 138, 288, 6, 50, "Text"], Cell[3882, 146, 174, 5, 68, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[4093, 156, 96, 2, 42, "Subsubsection", Evaluatable->False], Cell[4192, 160, 171, 5, 32, "Text", Evaluatable->False], Cell[4366, 167, 45, 1, 27, "Input"], Cell[4414, 170, 156, 5, 32, "Text", Evaluatable->False], Cell[4573, 177, 47, 1, 27, "Input"], Cell[4623, 180, 182, 5, 32, "Text", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[4842, 190, 34, 0, 42, "Subsubsection"], Cell[4879, 192, 277, 6, 50, "Text", Evaluatable->False], Cell[5159, 200, 141, 5, 32, "Text", Evaluatable->False], Cell[5303, 207, 45, 1, 27, "Input"], Cell[5351, 210, 109, 2, 32, "Text", Evaluatable->False], Cell[5463, 214, 68, 1, 27, "Input"], Cell[5534, 217, 112, 2, 32, "Text", Evaluatable->False], Cell[5649, 221, 45, 1, 27, "Input"], Cell[5697, 224, 93, 2, 32, "Text", Evaluatable->False], Cell[5793, 228, 53, 1, 27, "Input"], Cell[5849, 231, 50, 1, 27, "Input"], Cell[5902, 234, 45, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5984, 240, 60, 0, 42, "Subsubsection"], Cell[6047, 242, 248, 6, 50, "Text", Evaluatable->False] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[6344, 254, 106, 2, 60, "Section", Evaluatable->False], Cell[6453, 258, 187, 7, 32, "Text", Evaluatable->False], Cell[6643, 267, 136, 3, 32, "Text"], Cell[6782, 272, 42, 1, 27, "Input"], Cell[6827, 275, 40, 1, 27, "Input"], Cell[6870, 278, 182, 5, 32, "Text", Evaluatable->False], Cell[7055, 285, 44, 1, 27, "Input"], Cell[7102, 288, 215, 6, 50, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[7342, 298, 48, 0, 45, "Subsection"], Cell[7393, 300, 192, 5, 32, "Text", Evaluatable->False], Cell[7588, 307, 56, 1, 27, "Input"], Cell[7647, 310, 108, 2, 32, "Text", Evaluatable->False], Cell[7758, 314, 52, 1, 27, "Input"], Cell[7813, 317, 171, 5, 32, "Text", Evaluatable->False], Cell[7987, 324, 38, 1, 27, "Input"], Cell[8028, 327, 37, 1, 27, "Input"], Cell[8068, 330, 90, 2, 32, "Text", Evaluatable->False], Cell[8161, 334, 39, 1, 27, "Input"], Cell[8203, 337, 116, 4, 32, "Text", Evaluatable->False], Cell[8322, 343, 41, 1, 27, "Input"], Cell[8366, 346, 52, 1, 27, "Input"], Cell[8421, 349, 39, 1, 27, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[8509, 356, 78, 2, 60, "Section", Evaluatable->False], Cell[8590, 360, 487, 15, 68, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[9102, 379, 51, 0, 45, "Subsection"], Cell[9156, 381, 107, 3, 32, "Text"], Cell[9266, 386, 49, 1, 27, "Input"], Cell[9318, 389, 107, 3, 32, "Text"], Cell[9428, 394, 47, 1, 27, "Input"], Cell[9478, 397, 64, 0, 32, "Text"], Cell[9545, 399, 56, 1, 27, "Input"], Cell[9604, 402, 54, 1, 27, "Input"], Cell[9661, 405, 332, 6, 68, "Text"] }, Open ]] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)