(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 40860, 1471] NotebookOptionsPosition[ 34310, 1264] NotebookOutlinePosition[ 35095, 1297] CellTagsIndexPosition[ 34976, 1290] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell[TextData[{ "Introduction to ", StyleBox["Mathematica \[MathematicaIcon]", FontSlant->"Italic"] }], "Title"], Cell[TextData[{ "5. Programming in ", StyleBox["Mathematica", FontSlant->"Italic"] }], "Subtitle"], Cell["\<\ P. S. Cally, School of Mathematical Sciences, Monash University\ \>", "Author"], Cell[TextData[{ "We will turn off that annoying spelling warning ", StyleBox["Mathematica", FontSlant->"Italic"], " often gives. Note that this is set up as an \"Initialization Cell\", so it \ automatically runs when you first start the kernel (after asking you). Go to \ Cell > CellProperties > InitializationCell to set these. They are marked by \ cell brackets featuring small vertical and diagonal lines in the top corner." }], "Text", CellChangeTimes->{{3.401436574960552*^9, 3.401436575907666*^9}}], Cell[BoxData[ RowBox[{"Off", "[", RowBox[{"General", "::", "\"\\""}], "]"}]], "Input", InitializationCell->True], Cell[CellGroupData[{ Cell["Compound Expressions", "Section"], Cell[TextData[{ "Unlike many programming languages, ", StyleBox["Mathematica", FontSlant->"Italic"], " programs do not normally consist of a structured set of lines, which may \ or may not need to be compiled. Rather, we set up a series of definitions. \ These can be in separate cells, or strung togeher as a single ", StyleBox["compound expression", FontWeight->"Bold"], ".\nHere is an example. Say we wanted to use the recurrence relation y(n)=a \ y(n-1)+y(n) with y(0)=0, y(1)=1, to find y(10). There are very many ways to \ do this. Here is one:" }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"y", "[", "0", "]"}], "=", "0"}], ";", RowBox[{ RowBox[{"y", "[", "1", "]"}], "=", "1"}], ";", RowBox[{ RowBox[{"y", "[", "n_", "]"}], ":=", RowBox[{ RowBox[{"a", " ", RowBox[{"y", "[", RowBox[{"n", "-", "1"}], "]"}]}], "+", RowBox[{"y", "[", RowBox[{"n", "-", "2"}], "]"}]}]}], ";", RowBox[{"FullSimplify", "[", RowBox[{"y", "[", "10", "]"}], "]"}]}]], "Input"], Cell["\<\ By using \";\" we have made a small \"program\". So, what's wrong with that? \ Well, nothing really, but it just leaves a lot of rubbish lying around \ \[Ellipsis]\ \>", "Text"], Cell[BoxData[ RowBox[{"?", "y"}]], "Input"], Cell["We need to tidy up:", "Text"], Cell[BoxData[ RowBox[{"Clear", "[", "y", "]"}]], "Input"], Cell[BoxData[ RowBox[{"?", "y"}]], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Modules", "Section"], Cell["\<\ A far neater way to do this is to use a Module. In this case, y is declared \ to be a local variable:\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"m", "[", RowBox[{"a_", ",", "k_"}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"y", ",", RowBox[{"y0", "=", "0"}], ",", RowBox[{"y1", "=", "1"}]}], "}"}], ",", RowBox[{ RowBox[{ RowBox[{"y", "[", "0", "]"}], "=", "y0"}], ";", RowBox[{ RowBox[{"y", "[", "1", "]"}], "=", "y1"}], ";", RowBox[{ RowBox[{"y", "[", "n_", "]"}], ":=", RowBox[{ RowBox[{"a", " ", RowBox[{"y", "[", RowBox[{"n", "-", "1"}], "]"}]}], "+", RowBox[{"y", "[", RowBox[{"n", "-", "2"}], "]"}]}]}], ";", RowBox[{"FullSimplify", "[", RowBox[{"y", "[", "k", "]"}], "]"}]}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"m", "[", RowBox[{"a", ",", "10"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"?", "y"}]], "Input"], Cell["\<\ We see that y is unknown outside the module, which is what we want. Modules \ provide a convenient way to \"hide\" all the internal workings.\ \>", "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Blocks", "Section"], Cell[TextData[{ "Blocks are very like modules. Indeed, the difference is rather subtle. ", StyleBox["As a rule of thumb, you should normally use Modules rather than \ Blocks. ", FontWeight->"Bold"], "For our recurrence relation example, Block behaves exactly like Module." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"b", "[", RowBox[{"a_", ",", "k_"}], "]"}], ":=", RowBox[{"Block", "[", RowBox[{ RowBox[{"{", RowBox[{"y", ",", RowBox[{"y0", "=", "0"}], ",", RowBox[{"y1", "=", "1"}]}], "}"}], ",", RowBox[{ RowBox[{ RowBox[{"y", "[", "0", "]"}], "=", "y0"}], ";", RowBox[{ RowBox[{"y", "[", "1", "]"}], "=", "y1"}], ";", RowBox[{ RowBox[{"y", "[", "n_", "]"}], ":=", RowBox[{ RowBox[{"a", " ", RowBox[{"y", "[", RowBox[{"n", "-", "1"}], "]"}]}], "+", RowBox[{"y", "[", RowBox[{"n", "-", "2"}], "]"}]}]}], ";", RowBox[{"FullSimplify", "[", RowBox[{"y", "[", "k", "]"}], "]"}]}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"b", "[", RowBox[{"a", ",", "10"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"?", "y"}]], "Input"], Cell["\<\ However, this little example will illustrate an important difference:\ \>", "Text"], Cell[BoxData[ RowBox[{"p", "=", RowBox[{"Sin", "[", "k", "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"Block", "[", RowBox[{ RowBox[{"{", RowBox[{"k", "=", "a"}], "}"}], ",", RowBox[{"k", "+", "p"}]}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"k", "=", "a"}], "}"}], ",", RowBox[{"k", "+", "p"}]}], "]"}]], "Input"], Cell["\<\ What has happened? Well, in Module the local variable k is actually given a \ different name internally:\ \>", "Text"], Cell[BoxData[ RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "t", "}"}], ",", "t"}], "]"}]], "Input"], Cell["\<\ This is how it keeps it's value private. The $n symbol is unique to each \ invokation of Module; so for example, this is what happens if we do it again\ \>", "Text"], Cell[BoxData[ RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "t", "}"}], ",", "t"}], "]"}]], "Input"], Cell["\<\ However, Block does not do this; it keeps the symbol t, but just changes its \ local value (if desired), resetting it to the global value at the end.\ \>", "Text"], Cell[BoxData[ RowBox[{"Block", "[", RowBox[{ RowBox[{"{", "t", "}"}], ",", "t"}], "]"}]], "Input"], Cell["So, for example,", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"a", "=", "12"}], ";", RowBox[{"Block", "[", RowBox[{ RowBox[{"{", RowBox[{"a", "=", "3"}], "}"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", "a"}], "]"}]}], "]"}], ";", "a"}]], "Input"], Cell[BoxData[ RowBox[{"Clear", "[", "a", "]"}]], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["With", "Section"], Cell["\<\ With is rather like Module and Block again, but you cannot just declare a \ local variable without giving it a particular value. It is designed as a \ convenient way of assigning constants.\ \>", "Text"], Cell[BoxData[ RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{"k", "=", "3"}], "}"}], ",", RowBox[{ SuperscriptBox[ RowBox[{"(", RowBox[{"1.0", "+", "k"}], ")"}], "2"], "+", RowBox[{"Sin", "[", "k", "]"}], "+", RowBox[{"Exp", "[", SuperscriptBox["k", "2"], "]"}]}]}], "]"}]], "Input"], Cell["This could alternatively be done like this:", "Text"], Cell[BoxData[ RowBox[{ RowBox[{ SuperscriptBox[ RowBox[{"(", RowBox[{"1.0", "+", "k"}], ")"}], "2"], "+", RowBox[{"Sin", "[", "k", "]"}], "+", RowBox[{"Exp", "[", SuperscriptBox["k", "2"], "]"}]}], "/.", RowBox[{"k", "\[Rule]", "3"}]}]], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Loops and Control Structures (if you must)", "Section"], Cell[TextData[{ "Does ", StyleBox["Mathematica", FontSlant->"Italic"], " have the do loops and other constructs of classical procedural languages \ such as FORTRAN or C? Yes indeed, but using them is a sign of a pretty poor \ ", StyleBox["Mathematica", FontSlant->"Italic"], " programmer. They tend to be both inelegant and slow. However, \ occasionally, they are useful, so let's have a quick look at a few of them." }], "Text"], Cell[CellGroupData[{ Cell["Do", "Subsection"], Cell["\<\ Here we use Do to add the first million integers (a rather silly exercise of \ course, as the answer is obvious):\ \>", "Text"], Cell[BoxData[ RowBox[{"Timing", "[", RowBox[{ RowBox[{"sum", "=", "0"}], ";", RowBox[{"Do", "[", RowBox[{ RowBox[{"sum", "+=", "i"}], ",", RowBox[{"{", RowBox[{"i", ",", "1000000"}], "}"}]}], "]"}], ";", "sum"}], "]"}]], "Input"], Cell["but this is better", "Text"], Cell[BoxData[ RowBox[{"Timing", "[", RowBox[{"Sum", "[", RowBox[{"i", ",", RowBox[{"{", RowBox[{"i", ",", "1000000"}], "}"}]}], "]"}], "]"}]], "Input"], Cell["and this is better still", "Text"], Cell[BoxData[ RowBox[{"Timing", "[", RowBox[{"Plus", "@@", RowBox[{"Range", "[", "1000000", "]"}]}], "]"}]], "Input"], Cell["By the way, +=k adds k to a variable:", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"n", "=", "3"}], ";", RowBox[{"n", "+=", "5"}], ";", "n"}]], "Input"], Cell["We could just as well have said n=n+5.", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"sum", "=."}], ";", RowBox[{"n", "=."}]}]], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["For and While", "Subsection"], Cell[TextData[{ "For is similar to Do, except that it does not prescribe a set numbver of \ loops, but rather tests to see when a stop criterion is satisfied. It takes \ the form ", StyleBox["For[", "MR"], StyleBox["start", "TI"], StyleBox[",", "MR"], " ", StyleBox["test", "TI"], StyleBox[",", "MR"], " ", StyleBox["incr", "TI"], StyleBox[",", "MR"], " ", StyleBox["body", "TI"], StyleBox["].", "MR"] }], "Text"], Cell[BoxData[ RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", " ", RowBox[{"i", "<", "5"}], ",", " ", RowBox[{"i", "++"}], ",", " ", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "i", ",", "\"\<, \!\(\*SuperscriptBox[\(i\), \(2\)]\)=\>\"", ",", SuperscriptBox["i", "2"]}], "]"}]}], "]"}]], "Input", CellTags->"For"], Cell["By the way, i++ increases the value of i by 1. Undefine i.", "Text", CellChangeTimes->{{3.401436885613927*^9, 3.4014368914594183`*^9}}], Cell[BoxData[ RowBox[{"i", "=."}]], "Input"], Cell[TextData[{ "While takes the cut-down form ", StyleBox["While[", "MR"], StyleBox["test", "TI"], StyleBox[",", "MR"], " ", StyleBox["body", "TI"], StyleBox["]. ", "MR"], StyleBox["You need to work the iteration into ", "MR", FontFamily->"Times"], StyleBox["test", "MR", FontFamily->"Times", FontSlant->"Italic"], StyleBox[" or ", "MR", FontFamily->"Times"], StyleBox["body", "MR", FontFamily->"Times", FontSlant->"Italic"], StyleBox[". Here it's in the ", "MR", FontFamily->"Times"], StyleBox["test", "MR", FontFamily->"Times", FontSlant->"Italic"], StyleBox[".", "MR", FontFamily->"Times"] }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"nn", "=", "10"}], ";", RowBox[{"While", "[", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{"nn", "=", RowBox[{"nn", "-", "1"}]}], ")"}], ">", "5"}], ",", RowBox[{"Print", "[", "nn", "]"}]}], "]"}]}]], "Input", CellTags->"While"], Cell[BoxData[ RowBox[{"nn", "=."}]], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Others", "Subsection"], Cell[TextData[{ "You might also like to check out Break, Throw, Return, Catch, Abort, Sow, \ Reap, \[Ellipsis] You should rarely need them. In fact, there are a few I've \ ", StyleBox["never", FontSlant->"Italic"], " used!" }], "Text"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Compile", "Section"], Cell[TextData[{ "Here are three ways to make many iterations of the recurrence ", Cell[BoxData[ FormBox[ RowBox[{ SubscriptBox["t", RowBox[{"n", "+", "1"}]], "=", RowBox[{"4", RowBox[{ RowBox[{ SubscriptBox["t", "n"], "(", RowBox[{"1", "-", SubscriptBox["t", "n"]}], ")"}], ".", " "}]}]}], TraditionalForm]]], "The first uses Do, and is very slow." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"fd", "[", RowBox[{"n_", ",", "x_"}], "]"}], ":=", RowBox[{"Timing", "[", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "t", "}"}], ",", RowBox[{ RowBox[{"t", "=", "x"}], ";", RowBox[{"Do", "[", RowBox[{ RowBox[{"t", "=", RowBox[{"4", "t", RowBox[{"(", RowBox[{"1", "-", "t"}], ")"}]}]}], ",", RowBox[{"{", "n", "}"}]}], "]"}], ";", "t"}]}], "]"}], "]"}]}]], "Input", CellTags->"Compile"], Cell["\<\ Use Timing to see how long it takes to do one million iterations.\ \>", "Text"], Cell[BoxData[ RowBox[{"fd", "[", RowBox[{"1000000", ",", ".37"}], "]"}]], "Input"], Cell[TextData[{ "Slow! But it can be speeded up greatly by ", StyleBox["compiling", FontSlant->"Italic"], " the function.", StyleBox[" ", FontSlant->"Italic"], "First, create a CompiledFunction" }], "Text", CellChangeTimes->{{3.401436967336821*^9, 3.4014369737499313`*^9}}], Cell[BoxData[ RowBox[{"fc", "=", RowBox[{"Compile", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"n", ",", "_Integer"}], "}"}], ",", "x"}], "}"}], ",", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "t", "}"}], ",", RowBox[{ RowBox[{"t", "=", "x"}], ";", RowBox[{"Do", "[", RowBox[{ RowBox[{"t", "=", RowBox[{"4", "t", RowBox[{"(", RowBox[{"1", "-", "t"}], ")"}]}]}], ",", RowBox[{"{", "n", "}"}]}], "]"}], ";", "t"}]}], "]"}]}], "]"}]}]], "Input"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " has turned fd into much more efficient code, in part by assuming that x \ (and hence t) is a real number, whereas fd has to admit the possibility that \ x is say a list, or a variable that hasn't been given a numeric value, etc. \ That makes for far less efficient machine code. So, let's see how the \ compiled code performs:" }], "Text"], Cell[BoxData[ RowBox[{"Timing", "[", RowBox[{"fc", "[", RowBox[{"1000000", ",", "0.37"}], "]"}], "]"}]], "Input"], Cell[TextData[{ "That's a BIG improvement. However, it is usually best to use built in ", StyleBox["Mathematica", FontSlant->"Italic"], " functions, such as Nest in this case, which are ", StyleBox["normally", FontSlant->"Italic"], " as fast or faster than user-compiled code. In this case, on my computer, \ Nest turns out to be a ", StyleBox["little", FontSlant->"Italic"], " slower, but that isn't typical." }], "Text"], Cell[BoxData[ RowBox[{"Clear", "[", "fn", "]"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"fn", "[", RowBox[{"n_", ",", "t_"}], "]"}], ":=", RowBox[{"Nest", "[", RowBox[{ RowBox[{ RowBox[{"4", "#", RowBox[{"(", RowBox[{"1", "-", "#"}], ")"}]}], "&"}], ",", "t", ",", "n"}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"Timing", "[", RowBox[{"fn", "[", RowBox[{"1000000", ",", ".37"}], "]"}], "]"}]], "Input"], Cell["\<\ Warning: there is no point compiling built-in functions. They are already \ compiled normally. Compile does best when there are large numbers of \ elementary operations.\ \>", "Text"] }, Closed]], Cell[CellGroupData[{ Cell[TextData[{ "Trace: Looking inside the ", StyleBox["Mathematica", FontSlant->"Italic"], " mind" }], "Section"], Cell["Here is a simple calculation, made up of several stages.", "Text"], Cell[BoxData[ RowBox[{"Nest", "[", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"4", " ", "#", " ", RowBox[{"(", RowBox[{"1", "-", "#"}], ")"}]}], "&"}], ")"}], ",", ".2", ",", "2"}], "]"}]], "Input"], Cell["\<\ Using Trace, we can see what is happening at all these stages:\ \>", "Text"], Cell[BoxData[ RowBox[{"Trace", "[", RowBox[{"Nest", "[", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"4", " ", "*", "#", "*", RowBox[{"(", RowBox[{"1", "-", "#"}], ")"}]}], "&"}], ")"}], ",", ".2", ",", "2"}], "]"}], "]"}]], "Input"], Cell["\<\ Trace has a number of options which allow you to taylor its behaviour; see \ Help.\ \>", "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Exercises", "Section", FontColor->RGBColor[0, 0, 1]], Cell["\<\ Do not look at the solution until you have completed the exercise!\ \>", "Text", TextAlignment->Center, FontFamily->"Helvetica", FontSize->18, FontWeight->"Bold", FontColor->RGBColor[1, 0, 0]], Cell[CellGroupData[{ Cell["Solving Recurrence Equations", "Subsection"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " actually has a utility for exactly solving recurrence equations (if \ possible): RSolve. Use it to find the exact solution of the equation treated \ in Compound Expressions above, and compare with the solution obtained there." }], "Text"], Cell[CellGroupData[{ Cell["Solution", "Subsubsection"], Cell[BoxData[ RowBox[{"Clear", "[", "y", "]"}]], "Input"], Cell[BoxData[ RowBox[{"rsoln", "=", RowBox[{"First", "[", RowBox[{"RSolve", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"y", "[", "n", "]"}], "==", RowBox[{ RowBox[{"a", " ", RowBox[{"y", "[", RowBox[{"n", "-", "1"}], "]"}]}], "+", RowBox[{"y", "[", RowBox[{"n", "-", "2"}], "]"}]}]}], ",", RowBox[{ RowBox[{"y", "[", "0", "]"}], "\[Equal]", "0"}], ",", RowBox[{ RowBox[{"y", "[", "1", "]"}], "\[Equal]", "1"}]}], "}"}], ",", "y", ",", "n"}], "]"}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"y", "[", "10", "]"}], "/.", "rsoln"}], "//", "FullSimplify"}]], "Input"], Cell["Tricky use of Module", "Exercise"], Cell[TextData[{ "Try evaluating ", Cell[BoxData[ FormBox[ RowBox[{ UnderoverscriptBox["\[Sum]", RowBox[{"n", "=", "0"}], "\[Infinity]"], FractionBox["1", RowBox[{"n", "!!"}]]}], TraditionalForm]]], " exactly using Sum. [Note: n!!=n(n-2)(n-4)\[Ellipsis]", Cell[BoxData[ FormBox[GridBox[{ { RowBox[{"1", " ", "or"}]}, {"2"} }], TraditionalForm]]], " is the so-called double factorial.] You should find that ", StyleBox["Mathematica", FontSlant->"Italic"], " cannot do it. However, it ", StyleBox["can", FontSlant->"Italic"], " succeed with a little bit of help from FunctionExpand; try applying it to \ 1/n!! and hence find ", Cell[BoxData[ FormBox[ RowBox[{ UnderoverscriptBox["\[Sum]", RowBox[{"n", "=", "0"}], "\[Infinity]"], FractionBox["1", RowBox[{"n", "!!"}]]}], TraditionalForm]]], ". Check your answer numerically using NSum." }], "Text", CellChangeTimes->{3.401437272724197*^9}], Cell[BoxData[ RowBox[{"Sum", "[", RowBox[{ FractionBox["1", RowBox[{"n", "!!"}]], ",", RowBox[{"{", RowBox[{"n", ",", "0", ",", "\[Infinity]"}], "}"}]}], "]"}]], "Input"], Cell["Of course, it can be done numerically (i.e., approximately).", "Text", CellChangeTimes->{{3.401437381993287*^9, 3.401437407595364*^9}}], Cell[BoxData[ RowBox[{"NSum", "[", RowBox[{ FractionBox["1", RowBox[{"n", "!!"}]], ",", RowBox[{"{", RowBox[{"n", ",", "0", ",", "\[Infinity]"}], "}"}]}], "]"}]], "Input", CellChangeTimes->{3.401437365583765*^9}], Cell[TextData[{ "Now, explain why ", StyleBox["this", FontSlant->"Italic"], " works (it can take a few seconds to run)" }], "Text", CellChangeTimes->{{3.401437272724197*^9, 3.401437280574139*^9}}], Cell[BoxData[ RowBox[{ FractionBox["1", RowBox[{"n", "!!"}]], "//", "FunctionExpand"}]], "Input", CellChangeTimes->{{3.401437187912315*^9, 3.401437193460432*^9}}], Cell[BoxData[ RowBox[{" ", RowBox[{ RowBox[{"gg", "[", "expr_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"Sum", ",", "ee"}], "}"}], ",", RowBox[{ RowBox[{"ee", "=", RowBox[{"MapAt", "[", RowBox[{"FunctionExpand", ",", "expr", ",", "1"}], "]"}]}], ";", "ee"}]}], "]"}], " "}]}]], "Input"], Cell[BoxData[ RowBox[{"gg", "[", RowBox[{"Sum", "[", RowBox[{ FractionBox["1", RowBox[{"n", "!!"}]], ",", RowBox[{"{", RowBox[{"n", ",", "0", ",", "\[Infinity]"}], "}"}]}], "]"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"%", "//", "N"}]], "Input", CellChangeTimes->{{3.401437324625746*^9, 3.401437331356351*^9}}] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Module or Block?", "Subsection"], Cell[TextData[{ "Assume there is a parameter, ", StyleBox["g", FontFamily->"Courier"], " say, in a function ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{"f", "(", "x", ")"}], "=", RowBox[{"sin", "[", RowBox[{ StyleBox["g", FontFamily->"Courier", FontSlant->"Plain"], "/", RowBox[{"(", RowBox[{ SuperscriptBox["x", "2"], "+", "1"}], ")"}]}], "]"}]}], TraditionalForm]]], ", but not explicitly mentioned in its arguments. Now say you want to create \ a scoping construct, i.e., a Module, Block, or With, to numerically calculate \ ", Cell[BoxData[ FormBox[ RowBox[{ SubsuperscriptBox["\[Integral]", "0", "X"], RowBox[{ RowBox[{"f", "[", "x", "]"}], RowBox[{"\[DifferentialD]", "x"}]}]}], TraditionalForm]]], ", but with ", StyleBox["g", FontFamily->"Courier"], " set to 5. How could you best do it? Why don't some obvious options work? \ Finally, define a function B(", StyleBox["g", FontFamily->"Courier"], ",X) which evaluates this integral for any given ", StyleBox["numeric", FontSlant->"Italic"], " ", StyleBox["g", FontFamily->"Courier"], "." }], "Text"], Cell[CellGroupData[{ Cell["Solution", "Subsubsection"], Cell[BoxData[ RowBox[{ RowBox[{"f", "[", "x_", "]"}], ":=", " ", RowBox[{"Sin", "[", FractionBox["g", RowBox[{ SuperscriptBox["x", "2"], "+", "1"}]], "]"}]}]], "Input"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " can't do the integral exactly." }], "Text"], Cell[BoxData[ RowBox[{"Integrate", "[", RowBox[{ RowBox[{"f", "[", "x", "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "1"}], "}"}]}], "]"}]], "Input"], Cell["So we have to do it numerically. Does this work?", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"F", "[", "X_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"g", "=", "5"}], "}"}], ",", RowBox[{"NIntegrate", "[", RowBox[{ RowBox[{"f", "[", "x", "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "X"}], "}"}]}], "]"}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"F", "[", "1", "]"}]], "Input"], Cell[TextData[{ "No! NIntegrate can't work if the integrand isn't numerical; in this case it \ contains the variable ", StyleBox["g", FontFamily->"Courier"], ". Why didn't Module set ", StyleBox["g", FontFamily->"Courier"], " to 5?\nWhat about this?" }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"H", "[", "X_", "]"}], ":=", RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{"g", "=", "5"}], "}"}], ",", RowBox[{"NIntegrate", "[", RowBox[{ RowBox[{"f", "[", "x", "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "X"}], "}"}]}], "]"}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"H", "[", "1", "]"}]], "Input"], Cell["\<\ Again no! What is going wrong? As a last resort, let's see if Block works.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"G", "[", "X_", "]"}], ":=", RowBox[{"Block", "[", RowBox[{ RowBox[{"{", RowBox[{"g", "=", "5"}], "}"}], ",", RowBox[{"NIntegrate", "[", RowBox[{ RowBox[{"f", "[", "x", "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "X"}], "}"}]}], "]"}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"G", "[", "1", "]"}]], "Input"], Cell["\<\ At last! Explain why Block works and Module doesn't. Now to define B. This will work:\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"B", "[", RowBox[{"gg_", ",", "X_"}], "]"}], ":=", RowBox[{"Block", "[", RowBox[{ RowBox[{"{", RowBox[{"g", "=", "gg"}], "}"}], ",", RowBox[{"NIntegrate", "[", RowBox[{ RowBox[{"f", "[", "x", "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "X"}], "}"}]}], "]"}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"B", "[", RowBox[{"5", ",", "1"}], "]"}]], "Input"], Cell[TextData[{ "Of course, it doesn't work if ", StyleBox["g", FontFamily->"Courier"], " is not numeric." }], "Text"], Cell[BoxData[ RowBox[{"B", "[", RowBox[{"g", ",", "1"}], "]"}]], "Input"], Cell[TextData[{ "but it does work for any numeric ", StyleBox["g", FontFamily->"Courier"], ", even if complex." }], "Text"], Cell[BoxData[ RowBox[{"B", "[", RowBox[{ RowBox[{"1", "+", "\[ImaginaryI]"}], ",", "1"}], "]"}]], "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Simple Programming Project", "Subsection"], Cell[TextData[{ "Write a Module to calculate the length of the curve in (x,y) space between \ the first two non-negative zeros of the solution of ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{ RowBox[{ RowBox[{"y", "''"}], "+", RowBox[{ RowBox[{"(", RowBox[{"1", "+", SuperscriptBox["x", "4"]}], ")"}], "y"}]}], "=", "0"}], ","}], TraditionalForm]]], " y(0)=0, y'(0)=1. It should plot y(x) over (0,5), and return {z,len} where \ z is the first positive zero and len is the length sought." }], "Text"], Cell[CellGroupData[{ Cell["Solution", "Subsubsection"], Cell[BoxData[ RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"sol", ",", "z"}], "}"}], ",", RowBox[{ RowBox[{"sol", "=", RowBox[{"First", "[", RowBox[{"NDSolve", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{"y", "''"}], "[", "x", "]"}], "+", RowBox[{ RowBox[{"(", RowBox[{"1", "+", SuperscriptBox["x", "4"]}], ")"}], RowBox[{"y", "[", "x", "]"}]}]}], "\[Equal]", "1"}], ",", RowBox[{ RowBox[{"y", "[", "0", "]"}], "\[Equal]", "0"}], ",", RowBox[{ RowBox[{ RowBox[{"y", "'"}], "[", "0", "]"}], "\[Equal]", "1"}]}], "}"}], ",", "y", ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "5"}], "}"}]}], "]"}], "]"}]}], ";", RowBox[{"Plot", "[", RowBox[{ RowBox[{"Evaluate", "[", RowBox[{ RowBox[{"y", "[", "x", "]"}], "/.", "sol"}], "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "5"}], "}"}], ",", RowBox[{"AspectRatio", "\[Rule]", "Automatic"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"z", "=", RowBox[{"x", "/.", RowBox[{"FindRoot", "[", RowBox[{ RowBox[{"Evaluate", "[", RowBox[{ RowBox[{"y", "[", "x", "]"}], "/.", "sol"}], "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "2"}], "}"}]}], "]"}]}]}], ";", RowBox[{"{", RowBox[{"z", ",", RowBox[{"NIntegrate", "[", RowBox[{ RowBox[{"Evaluate", "[", RowBox[{ SqrtBox[ RowBox[{"1", "+", SuperscriptBox[ RowBox[{ RowBox[{"y", "'"}], "[", "x", "]"}], "2"]}]], "/.", "sol"}], "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "z"}], "}"}]}], "]"}]}], "}"}]}]}], "]"}]], "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Advanced Programming Project", "Subsection"], Cell[TextData[{ "Extend the previous exercise by writing a module with returns the bounds of \ all the \"lobes\" (i.e., sections of curve between each successive zero of y) \ up to x=5 and their lengths in a list of the form ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{ FormBox[ RowBox[{"{", RowBox[{"{", RowBox[{"{", RowBox[{ SubscriptBox["z", "0"], ",", SubscriptBox["z", "1"]}], "}"}]}]}], TraditionalForm], ",", SubscriptBox["len", "1"]}], "}"}], ",", " ", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ SubscriptBox["z", "1"], ",", SubscriptBox["z", "2"]}], "}"}], ",", " ", SubscriptBox["len", "2"]}], "}"}], ",", " ", "\[Ellipsis]"}], "}"}], "."}], TraditionalForm]]], " It should also produce a ListPlot of lobe length against lobe number \ (1,2,3,\[Ellipsis]). You may need the utilities Select and Partition, and \ perhaps Prepend. [Hint: first make a table of y(x) for 0\"", ",", "\"\\""}], "}"}]}]}], "]"}], ";", "ans"}]}], "\[IndentingNewLine]", "]"}]], "Input"] }, Closed]] }, Closed]] }, Closed]] }, Open ]] }, AutoGeneratedPackage->Automatic, WindowSize->{699, 929}, WindowMargins->{{1, Automatic}, {Automatic, 0}}, ShowSelection->True, Magnification->1, FrontEndVersion->"6.0 for Mac OS X x86 (32-bit) (June 19, 2007)", StyleDefinitions->FrontEnd`FileName[{"Creative"}, "NaturalColor.nb", CharacterEncoding -> "UTF-8"] ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{ "For"->{ Cell[10353, 397, 373, 11, 50, "Input", CellTags->"For"]}, "While"->{ Cell[11565, 443, 293, 10, 44, "Input", CellTags->"While"]}, "Compile"->{ Cell[12715, 494, 524, 18, 44, "Input", CellTags->"Compile"]} } *) (*CellTagsIndex CellTagsIndex->{ {"For", 34732, 1278}, {"While", 34807, 1281}, {"Compile", 34886, 1284} } *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 118, 4, 66, "Title"], Cell[711, 29, 102, 4, 31, "Subtitle"], Cell[816, 35, 89, 2, 20, "Author"], Cell[908, 39, 510, 9, 71, "Text"], Cell[1421, 50, 126, 3, 37, "Input", InitializationCell->True], Cell[CellGroupData[{ Cell[1572, 57, 39, 0, 72, "Section"], Cell[1614, 59, 572, 12, 94, "Text"], Cell[2189, 73, 459, 15, 44, "Input"], Cell[2651, 90, 187, 4, 37, "Text"], Cell[2841, 96, 44, 1, 44, "Input"], Cell[2888, 99, 35, 0, 22, "Text"], Cell[2926, 101, 58, 1, 44, "Input"], Cell[2987, 104, 44, 1, 44, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[3068, 110, 26, 0, 42, "Section"], Cell[3097, 112, 125, 3, 22, "Text"], Cell[3225, 117, 756, 24, 60, "Input"], Cell[3984, 143, 78, 2, 44, "Input"], Cell[4065, 147, 44, 1, 44, "Input"], Cell[4112, 150, 165, 3, 37, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[4314, 158, 25, 0, 42, "Section"], Cell[4342, 160, 289, 6, 37, "Text"], Cell[4634, 168, 755, 24, 60, "Input"], Cell[5392, 194, 78, 2, 44, "Input"], Cell[5473, 198, 44, 1, 44, "Input"], Cell[5520, 201, 93, 2, 22, "Text"], Cell[5616, 205, 79, 2, 44, "Input"], Cell[5698, 209, 154, 5, 44, "Input"], Cell[5855, 216, 155, 5, 44, "Input"], Cell[6013, 223, 128, 3, 22, "Text"], Cell[6144, 228, 106, 3, 44, "Input"], Cell[6253, 233, 175, 3, 37, "Text"], Cell[6431, 238, 106, 3, 44, "Input"], Cell[6540, 243, 173, 3, 37, "Text"], Cell[6716, 248, 105, 3, 44, "Input"], Cell[6824, 253, 32, 0, 22, "Text"], Cell[6859, 255, 276, 9, 44, "Input"], Cell[7138, 266, 58, 1, 44, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[7233, 272, 23, 0, 42, "Section"], Cell[7259, 274, 213, 4, 37, "Text"], Cell[7475, 280, 335, 11, 50, "Input"], Cell[7813, 293, 59, 0, 22, "Text"], Cell[7875, 295, 278, 9, 50, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[8190, 309, 61, 0, 42, "Section"], Cell[8254, 311, 438, 11, 52, "Text"], Cell[CellGroupData[{ Cell[8717, 326, 24, 0, 41, "Subsection"], Cell[8744, 328, 137, 3, 22, "Text"], Cell[8884, 333, 266, 9, 44, "Input"], Cell[9153, 344, 34, 0, 22, "Text"], Cell[9190, 346, 170, 5, 44, "Input"], Cell[9363, 353, 40, 0, 22, "Text"], Cell[9406, 355, 125, 3, 44, "Input"], Cell[9534, 360, 53, 0, 22, "Text"], Cell[9590, 362, 106, 3, 44, "Input"], Cell[9699, 367, 54, 0, 22, "Text"], Cell[9756, 369, 89, 3, 44, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[9882, 377, 35, 0, 25, "Subsection"], Cell[9920, 379, 430, 16, 37, "Text"], Cell[10353, 397, 373, 11, 50, "Input", CellTags->"For"], Cell[10729, 410, 142, 1, 22, "Text"], Cell[10874, 413, 45, 1, 44, "Input"], Cell[10922, 416, 640, 25, 22, "Text"], Cell[11565, 443, 293, 10, 44, "Input", CellTags->"While"], Cell[11861, 455, 46, 1, 44, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[11944, 461, 28, 0, 25, "Subsection"], Cell[11975, 463, 240, 7, 37, "Text"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[12264, 476, 26, 0, 42, "Section"], Cell[12293, 478, 419, 14, 22, "Text"], Cell[12715, 494, 524, 18, 44, "Input", CellTags->"Compile"], Cell[13242, 514, 89, 2, 22, "Text"], Cell[13334, 518, 86, 2, 44, "Input"], Cell[13423, 522, 283, 9, 22, "Text"], Cell[13709, 533, 597, 20, 44, "Input"], Cell[14309, 555, 407, 8, 52, "Text"], Cell[14719, 565, 121, 3, 44, "Input"], Cell[14843, 570, 435, 12, 52, "Text"], Cell[15281, 584, 59, 1, 44, "Input"], Cell[15343, 587, 275, 10, 44, "Input"], Cell[15621, 599, 120, 3, 44, "Input"], Cell[15744, 604, 193, 4, 37, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[15974, 613, 119, 5, 42, "Section"], Cell[16096, 620, 72, 0, 20, "Text"], Cell[16171, 622, 229, 8, 27, "Input"], Cell[16403, 632, 86, 2, 20, "Text"], Cell[16492, 636, 273, 9, 27, "Input"], Cell[16768, 647, 106, 3, 20, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[16911, 655, 59, 1, 42, "Section"], Cell[16973, 658, 207, 7, 26, "Text"], Cell[CellGroupData[{ Cell[17205, 669, 50, 0, 31, "Subsection"], Cell[17258, 671, 306, 6, 35, "Text"], Cell[CellGroupData[{ Cell[17589, 681, 33, 0, 18, "Subsubsection"], Cell[17625, 683, 58, 1, 27, "Input"], Cell[17686, 686, 617, 19, 27, "Input"], Cell[18306, 707, 122, 4, 27, "Input"], Cell[18431, 713, 40, 0, 20, "Exercise"], Cell[18474, 715, 977, 33, 80, "Text"], Cell[19454, 750, 191, 6, 47, "Input"], Cell[19648, 758, 142, 1, 20, "Text"], Cell[19793, 761, 234, 7, 47, "Input"], Cell[20030, 770, 202, 6, 20, "Text"], Cell[20235, 778, 170, 4, 47, "Input"], Cell[20408, 784, 376, 12, 27, "Input"], Cell[20787, 798, 228, 8, 47, "Input"], Cell[21018, 808, 116, 2, 27, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[21183, 816, 38, 0, 31, "Subsection"], Cell[21224, 818, 1173, 42, 80, "Text"], Cell[CellGroupData[{ Cell[22422, 864, 33, 0, 18, "Subsubsection"], Cell[22458, 866, 188, 6, 45, "Input"], Cell[22649, 874, 111, 4, 20, "Text"], Cell[22763, 880, 173, 5, 27, "Input"], Cell[22939, 887, 64, 0, 20, "Text"], Cell[23006, 889, 349, 11, 27, "Input"], Cell[23358, 902, 54, 1, 27, "Input"], Cell[23415, 905, 271, 9, 35, "Text"], Cell[23689, 916, 347, 11, 27, "Input"], Cell[24039, 929, 54, 1, 27, "Input"], Cell[24096, 932, 98, 2, 20, "Text"], Cell[24197, 936, 348, 11, 27, "Input"], Cell[24548, 949, 54, 1, 27, "Input"], Cell[24605, 952, 109, 3, 35, "Text"], Cell[24717, 957, 375, 12, 27, "Input"], Cell[25095, 971, 77, 2, 27, "Input"], Cell[25175, 975, 123, 5, 20, "Text"], Cell[25301, 982, 77, 2, 27, "Input"], Cell[25381, 986, 128, 5, 20, "Text"], Cell[25512, 993, 113, 3, 27, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[25674, 1002, 48, 0, 31, "Subsection"], Cell[25725, 1004, 557, 16, 56, "Text"], Cell[CellGroupData[{ Cell[26307, 1024, 33, 0, 18, "Subsubsection"], Cell[26343, 1026, 1987, 61, 119, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[28379, 1093, 50, 0, 31, "Subsection"], Cell[28432, 1095, 1119, 31, 65, "Text"], Cell[CellGroupData[{ Cell[29576, 1130, 33, 0, 18, "Subsubsection"], Cell[29612, 1132, 4646, 126, 285, "Input"] }, Closed]] }, Closed]] }, Closed]] }, Open ]] } ] *) (* End of internal cache information *)