(* 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[ 107906, 3404] NotebookOptionsPosition[ 97097, 3071] NotebookOutlinePosition[ 97464, 3087] CellTagsIndexPosition[ 97421, 3084] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[TextData[{ "Towards programming in ", StyleBox["Mathematica", FontSlant->"Italic"] }], "Title", CellChangeTimes->{{3.392045624509651*^9, 3.3920456250504503`*^9}, 3.4586560125625*^9}], Cell["\<\ Comp 160 Course Module 2 \ \>", "Subtitle", CellChangeTimes->{{3.3920456294970217`*^9, 3.3920456328419647`*^9}, { 3.45865582121875*^9, 3.4586558234375*^9}}], Cell[TextData[{ "Having seen some of the basics of ", StyleBox["Mathematica,", FontSlant->"Italic"], " our goal in this module is to start to learn how to program in ", StyleBox["Mathematica", FontSlant->"Italic"], ". Perhaps the best way to think of ", StyleBox["Mathematica", FontSlant->"Italic"], " is as an extremely powerful calculator. You, the user, type in an \ expression and ", StyleBox["Mathematica", FontSlant->"Italic"], " then evaluates that expression and returns an answer. For simple \ problems, these expressions will only involve functions that are built into ", StyleBox["Mathematica", FontSlant->"Italic"], ". For more complex problems, one typically defines new functions in terms \ of existing functions that are then used in more complex expressions. In \ the following module, we explore this process in more detail." }], "Text", CellChangeTimes->{{3.3920456192318516`*^9, 3.3920456200330353`*^9}, { 3.3924793657394047`*^9, 3.3924793868529177`*^9}, {3.393604752073944*^9, 3.393604755065617*^9}, 3.409250881921875*^9, 3.409251096921875*^9}], Cell[CellGroupData[{ Cell["Arithmetic Expressions", "Section"], Cell[TextData[{ "To begin, let's briefly review some basic material from Module 1. \ Arithmetic expressions in ", StyleBox["Mathematica", FontSlant->"Italic"], " are built using numbers such as ", Cell[BoxData["2"]], ", ", Cell[BoxData[ RowBox[{"-", "1"}]]], ", ", Cell[BoxData["0.003"]], " and operations such as addition +, subtraction ", Cell[BoxData["-"]], ", multiplication ", Cell[BoxData["*"]], ", and division ", Cell[BoxData["/"]], ". These expressions can be entered in standard mathematical form such as" }], "Text", CellChangeTimes->{{3.3920456922297287`*^9, 3.3920457293946514`*^9}, 3.3920562793755484`*^9, {3.392479487710204*^9, 3.392479488603916*^9}, 3.392485428754545*^9, 3.392485706709031*^9, {3.45865603990625*^9, 3.458656045984375*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"1", "+", "2", "+", "3"}], "\[IndentingNewLine]", FractionBox["5.0", "7.5"], "\[IndentingNewLine]", SqrtBox[ RowBox[{"2.2", "-", "0.9"}]]}], "Input"], Cell[BoxData["6"], "Output"], Cell[BoxData["0.6666666666666666`"], "Output"], Cell[BoxData["1.140175425099138`"], "Output"] }, Open ]], Cell[TextData[{ "One point of interest that will be become more relevant later in the class \ is that all ", StyleBox["Mathematica", FontSlant->"Italic"], " expression are represented in the form ", Cell[BoxData[ RowBox[{"foo", "[", RowBox[{"a", ",", "b", ",", " ", "\[Ellipsis]", ",", " ", "c"}], "]"}]]], " where ", Cell[BoxData["foo"]], " is a function, and ", Cell[BoxData[ RowBox[{"a", ",", "b", ",", "c"}]]], " are themselves expressions. For example, the previous three expressions \ written in \"standard\" form are" }], "Text", CellChangeTimes->{{3.392045683396675*^9, 3.3920456844081697`*^9}, 3.392056283521676*^9, {3.392479541158259*^9, 3.3924795605213757`*^9}, 3.45865606196875*^9}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"Plus", "[", RowBox[{"1", ",", "2", ",", "3"}], "]"}], "\[IndentingNewLine]", RowBox[{"Divide", "[", RowBox[{"5.0", ",", "7.5"}], "]"}], "\[IndentingNewLine]", RowBox[{"Sqrt", "[", RowBox[{"Subtract", "[", RowBox[{"2.2", ",", "0.9"}], "]"}], "]"}]}], "Input"], Cell[BoxData["6"], "Output"], Cell[BoxData["0.6666666666666666`"], "Output"], Cell[BoxData["1.140175425099138`"], "Output"] }, Open ]], Cell[TextData[{ "In fact, all expressions in ", StyleBox["Mathematica", FontSlant->"Italic"], " are represented internally in the ", Cell[BoxData["Standard"]], " form ", Cell[BoxData[ RowBox[{"foo", "[", RowBox[{"a", ",", "b", ",", "\[Ellipsis]", ",", "c"}], "]"}]]], ". However, for ease of use, ", StyleBox["Mathematica", FontSlant->"Italic"], " also supports writing expressions in ", Cell[BoxData["Traditional"]], " form." }], "Text", CellChangeTimes->{{3.392045747841913*^9, 3.3920457491538515`*^9}, { 3.3920458092526665`*^9, 3.3920458131584387`*^9}, {3.3920484516977057`*^9, 3.392048466509595*^9}, 3.458656081765625*^9}], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.45916651846875*^9, 3.459166519921875*^9}, 3.4591667359375*^9}], Cell[TextData[{ StyleBox["Compute the value of the product of the integers from ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["1"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["30"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" inclusive. Instead of typing in the thirty numbers, use the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to locate a function in ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that performs this computation. Find at least two functions that \ can handle this task.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920456603225756`*^9, 3.392045661173834*^9}, { 3.3920457583774824`*^9, 3.3920457782869053`*^9}, {3.392046440215111*^9, 3.3920464700792446`*^9}, {3.393604865140153*^9, 3.393604865896513*^9}, { 3.458656099171875*^9, 3.458656100078125*^9}}], Cell[TextData[{ "In general, ", StyleBox["Mathematica", FontSlant->"Italic"], " has almost every mathematical function that you can imagine already \ available. Your goal in this class is not to learn how to program, but \ rather how to solve problems. Instead of focusing on writing ", StyleBox["Mathematica", FontSlant->"Italic"], " code for standard functions from scratch, your goal should be to become a \ wizard with the ", Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]]], ". ", StyleBox["Never attempt to write your own definition of a simple function \ without first checking the ", FontColor->RGBColor[1, 0, 0]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[1, 0, 0]], StyleBox[" for whether ", FontColor->RGBColor[1, 0, 0]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[1, 0, 0]], StyleBox[" has the function built\[Hyphen]in.", FontColor->RGBColor[1, 0, 0]] }], "Text", CellChangeTimes->{{3.3920456603225756`*^9, 3.392045661173834*^9}, { 3.3920457583774824`*^9, 3.3920457782869053`*^9}, 3.392046440215111*^9, { 3.3920464888169355`*^9, 3.3920466109774656`*^9}, {3.392479851261984*^9, 3.392479874323648*^9}, {3.393604912239956*^9, 3.393604913967944*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Variables and assignment", "Section"], Cell[TextData[{ "Expressions in previous modules used symbols such as ", Cell[BoxData["\[Pi]"]], ", ", Cell[BoxData["\[ExponentialE]"]], ", and ", Cell[BoxData["\[ImaginaryI]"]], ". In ", StyleBox["Mathematica ", FontSlant->"Italic"], "(and other programming languages), the symbols ", Cell[BoxData["\[Pi]"]], ", ", Cell[BoxData["\[ExponentialE]"]], ", and ", Cell[BoxData["\[ImaginaryI]"]], " are placeholders that stand for well\[Hyphen]known constants. For \ example, ", Cell[BoxData["\[Pi]"]], " is the ratio of a circle's circumference to its diameter, ", Cell[BoxData["\[ExponentialE]"]], " stands for the base of the natural logarithm, and ", Cell[BoxData["\[ImaginaryI]"]], " stands for ", Cell[BoxData[ SqrtBox[ RowBox[{"-", "1"}]]]], ". Another important placeholder is ", Cell[BoxData[ RowBox[{"\[Infinity]", ","}]]], " which stands for the positive end of the number scale." }], "Text", CellChangeTimes->{{3.392045826317886*^9, 3.3920458281305647`*^9}, 3.3920562861856127`*^9, {3.458656573578125*^9, 3.458656635078125*^9}}], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " users can also define their own placeholders for important values. These \ placeholders are known as variables and by convention always start with a \ lowercase letter to distinguish them from ", StyleBox["Mathematica", FontSlant->"Italic"], "'s built\[Hyphen]in constants. Variables are given their values via the \ assignment operator ", Cell[BoxData["="]], ". For example, one can define two variables ", Cell[BoxData["r"]], " and ", Cell[BoxData["my\[Pi]"]], ":" }], "Text", CellChangeTimes->{{3.3920458387462525`*^9, 3.3920458568730407`*^9}, 3.45865667690625*^9}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"r", " ", "=", "10"}], "\[IndentingNewLine]", RowBox[{"my\[Pi]", " ", "=", " ", RowBox[{"4", "\[Pi]"}]}]}], "Input", CellChangeTimes->{{3.3920458604282947`*^9, 3.392045861559967*^9}}], Cell[BoxData["10"], "Output", CellChangeTimes->{3.3920458628318467`*^9}], Cell[BoxData[ RowBox[{"4", " ", "\[Pi]"}]], "Output", CellChangeTimes->{3.3920458628919353`*^9}] }, Open ]], Cell[TextData[{ "Now the surface area of a sphere with radius ", Cell[BoxData["r"]], " is ", Cell[BoxData[ RowBox[{"4", "\[Pi]", " ", SuperscriptBox["r", "2"]}]]], "." }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"area", " ", "=", " ", RowBox[{"my\[Pi]", " ", SuperscriptBox["r", "2"]}]}]], "Input", CellChangeTimes->{{3.3920458668978553`*^9, 3.392045867879306*^9}}], Cell[BoxData[ RowBox[{"400", " ", "\[Pi]"}]], "Output", CellChangeTimes->{3.3920458683900604`*^9}] }, Open ]], Cell[TextData[{ "One common practice of novice programmers is to redefine the value of a \ variable by performing multiple assignments to the variable. For example, \ we could redefine the value of ", Cell[BoxData["r"]], " and ", Cell[BoxData["area"]], " using ", Cell[BoxData["="]], "." }], "Text", CellChangeTimes->{{3.392480138839135*^9, 3.392480139131778*^9}, { 3.393609138689033*^9, 3.393609138808218*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"r", "=", "20"}]], "Input"], Cell[BoxData["20"], "Output", CellChangeTimes->{3.392045878094402*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"area", "=", RowBox[{"my\[Pi]", " ", SuperscriptBox["r", "2"]}]}]], "Input", CellChangeTimes->{{3.3920459030212393`*^9, 3.392045904172941*^9}}], Cell[BoxData[ RowBox[{"1600", " ", "\[Pi]"}]], "Output", CellChangeTimes->{{3.392045880027258*^9, 3.3920459048138885`*^9}}] }, Open ]], Cell[TextData[{ "While this practice can be useful for complex problems, it is best avoided \ by novice programmers, since redefining a variable forces the programmer to \ remember the ", StyleBox["current", FontSlant->"Italic"], " value of the variable. For now, we will write ", StyleBox["functional", FontSlant->"Italic"], " programs in ", StyleBox["Mathematica ", FontSlant->"Italic"], "in which a variable is assigned a value only once. Following this \ convention ensures that the value associated with a variable stays unchanged \ during a ", StyleBox["Mathematica", FontSlant->"Italic"], " computation. " }], "Text"], Cell[TextData[{ "One of the most powerful capabilities of ", StyleBox["Mathematica", FontSlant->"Italic"], " is that ", StyleBox["Mathematica", FontSlant->"Italic"], " can simplify expressions containing undefined variables. This method of \ computing is known as ", StyleBox["symbolic computation", FontSlant->"Italic"], ". For example you can ask ", StyleBox["Mathematica", FontSlant->"Italic"], " to evaluate ", Cell[BoxData[ RowBox[{ RowBox[{"3", "a"}], "+", RowBox[{"5", "a"}]}]]], ". Although the value of the variable ", Cell[BoxData["a"]], " is unknown, ", StyleBox["Mathematica", FontSlant->"Italic"], " is still able to answer that ", Cell[BoxData[ RowBox[{ RowBox[{"3", "a"}], "+", RowBox[{"5", "a"}]}]]], " is ", Cell[BoxData[ RowBox[{"8", "a"}]]], "." }], "Text", CellChangeTimes->{3.392480227643303*^9, 3.393609184974391*^9}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ RowBox[{"3", "a"}], "+", RowBox[{"5", "a"}]}]], "Input"], Cell[BoxData[ RowBox[{"8", " ", "a"}]], "Output"] }, Open ]], Cell[TextData[{ "This feature distinguishes ", StyleBox["Mathematica", FontSlant->"Italic"], " from many other languages such as ", Cell[BoxData[ RowBox[{"Visual", " ", "Basic"}]]], ", ", Cell[BoxData["C"]], ", and ", Cell[BoxData["Pascal"]], " which require all variables to have known values." }], "Text", CellChangeTimes->{{3.39248026286348*^9, 3.3924802710657053`*^9}, { 3.4601272455625*^9, 3.4601272694375*^9}, {3.46012730478125*^9, 3.4601273089375*^9}}], Cell[CellGroupData[{ Cell["Exercises", "Exercise", CellChangeTimes->{{3.4591665268125*^9, 3.459166528234375*^9}, 3.45916674196875*^9, 3.459169670578125*^9}], Cell[TextData[{ StyleBox["1. The equal sign ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["="], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is shorthand for the ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Set"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". The expression ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Set", "[", RowBox[{"x", ",", "a"}], "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" immediately evaluates the expression ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["a"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and then assigns that value to the variable ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Investigate what happens when the expression ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is not a variable name.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920461155152655`*^9, 3.3920462739594164`*^9}, { 3.392480308812008*^9, 3.392480309880588*^9}, {3.459169680859375*^9, 3.4591696810625*^9}}], Cell[TextData[{ StyleBox["2. ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" also supports a second type of assignment with the function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["SetDelayed"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" (", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[":="], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["). Use the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to explain the difference between ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Set"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["SetDelayed"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920461155152655`*^9, 3.3920463602269034`*^9}, { 3.459169684578125*^9, 3.459169684734375*^9}}], Cell[TextData[{ StyleBox["3. Give an example of a sequence of assignments that return a \ different final answer based on whether ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Set"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" or ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["SetDelayed"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is used.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920461155152655`*^9, 3.392046403590987*^9}, { 3.459169687203125*^9, 3.459169688171875*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Conditional expressions", "Section"], Cell[CellGroupData[{ Cell["Boolean expressions", "Subsection"], Cell[TextData[{ "Not all mathematical functions can be written as arithmetic expressions \ based on the operators ", Cell[BoxData[ FormBox["+", TraditionalForm]]], ", ", Cell[BoxData[ FormBox["-", TraditionalForm]]], ", and so on. Sometimes it is necessary to test for different cases inside \ an expression. In particular, we can direct the evaluation of an expression \ depending on whether a Boolean value is either ", Cell[BoxData["True"]], " or ", Cell[BoxData["False"]], ". (", Cell[BoxData[ FormBox[ StyleBox["True", FontFamily->"Courier"], TraditionalForm]]], " and ", Cell[BoxData[ FormBox[ StyleBox["False", FontFamily->"Courier"], TraditionalForm]]], " are ", StyleBox["Mathematica", FontSlant->"Italic"], " built-in constants.) We can build up expressions that return Boolean \ values using the relational operators ", Cell[BoxData["\[Equal]"]], ", ", Cell[BoxData["\[LessEqual]"]], ", ", Cell[BoxData[ FormBox["<", TraditionalForm]]], ", ", Cell[BoxData[ FormBox[">", TraditionalForm]]], ", ", Cell[BoxData["\[GreaterEqual]"]], " and ", Cell[BoxData["\[NotEqual]"]], ". For example the expression ", Cell[BoxData[ RowBox[{"a", "\[Equal]", "b"}]]], " will evaluate to ", Cell[BoxData["True"]], " if both variables ", Cell[BoxData["a"]], " and ", Cell[BoxData["b"]], " have defined values that are the same. This expression evaluates to ", Cell[BoxData["False"]], " if ", Cell[BoxData[Cell["a"]]], " and ", Cell[BoxData[Cell["b"]]], " have values that differ. Finally, if the variables ", Cell[BoxData["a"]], " and ", Cell[BoxData["b"]], " are undefined, then ", StyleBox["Mathematica", FontSlant->"Italic"], " will return the expression ", Cell[BoxData[ RowBox[{"a", "\[Equal]", "b"}]]], " denoting that this expression cannot be simplified further." }], "Text", CellChangeTimes->{ 3.39248040688727*^9, {3.392480475052897*^9, 3.392480476719702*^9}, 3.393609261873828*^9, {3.393609309966084*^9, 3.39360931118902*^9}, 3.45865685496875*^9}], Cell[CellGroupData[{ Cell["Exercises", "Exercise", CellChangeTimes->{{3.459166574515625*^9, 3.459166575609375*^9}, 3.4591667458125*^9, 3.45916969890625*^9}], Cell[TextData[{ StyleBox["1. Given a variable ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", write three expressions that test whether ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is positive, negative, or zero.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{ 3.458656751671875*^9, {3.459169702328125*^9, 3.45916970253125*^9}}], Cell[TextData[{ StyleBox["2. Given two variable ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["y"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", write an expression that returns ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["True"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" if and only if both ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["y"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" are positive; write an expression that returns ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["True"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" if either of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["y"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" are positive. Hint: Use the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and look up the functions ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["And"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Or"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920467094529943`*^9, 3.392046718987084*^9}, 3.3936093435200977`*^9, {3.4591697063125*^9, 3.4591697064375*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ Cell[BoxData["If"]], " expressions" }], "Subsection"], Cell[TextData[{ "We just saw how we can test certain equality and inequality conditions. \ Later we will learn more about built-in functions whose names end in the \ capital letter Q that can be used to test more complex conditions. Inside ", StyleBox["Mathematica", FontSlant->"Italic"], " expressions we can direct the evaluation depending on a condition using \ the ", Cell[BoxData["If"]], "-expression. This expression has the form ", Cell[BoxData[ RowBox[{"If", "[", RowBox[{"b", ",", "ex1", ",", "ex2"}], "]"}]]], ". An ", Cell[BoxData[Cell["If"]]], " expression is evaluated as follows: First, ", StyleBox["Mathematica", FontSlant->"Italic"], " evaluates the expression ", Cell[BoxData["b"]], ". If the value of ", Cell[BoxData["b"]], " is ", Cell[BoxData["True"]], ", the value of the whole ", Cell[BoxData["If"]], "-expression is the value of ", Cell[BoxData["ex1"]], ". If the value of ", Cell[BoxData[Cell["b"]]], " is ", Cell[BoxData["False"]], ", the value of the ", Cell[BoxData["If"]], "-expression is the value of ", Cell[BoxData["ex2"]], ". Otherwise, the value of the ", Cell[BoxData["If"]], "-expression is the expression itself. An example of when the third case \ occurs is when ", Cell[BoxData["b"]], " contains undefined variables. For example, " }], "Text", CellChangeTimes->{{3.392480535542296*^9, 3.392480537749681*^9}, { 3.393609384526578*^9, 3.3936093852466288`*^9}, {3.393609420402421*^9, 3.393609420917075*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"If", "[", RowBox[{ RowBox[{"z", "\[Equal]", "z"}], ",", "1", ",", "2"}], "]"}], "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"z", "\[NotEqual]", "z"}], ",", "1", ",", "2"}], "]"}], "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{"z", ",", "1", ",", "2"}], "]"}]}], "Input"], Cell[BoxData["1"], "Output"], Cell[BoxData["2"], "Output"], Cell[BoxData[ RowBox[{"If", "[", RowBox[{"z", ",", "1", ",", "2"}], "]"}]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.45916660046875*^9, 3.45916660309375*^9}, 3.459166749609375*^9}], Cell[TextData[{ StyleBox["Given a variable ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[Cell["y"]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", write an expression that computes the absolute value of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[Cell["y"]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" using an ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["If"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" expression.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{3.4591666085625*^9}] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Defining New Mathematica Functions", "Section"], Cell[TextData[{ "We already saw how we can use functions to compute complicated values \ inside ", StyleBox["Mathematica", FontSlant->"Italic"], ". For example, using ", Cell[BoxData["Product"]], " to compute the product of a sequence of numbers was much easier than \ typing in the entire sequence by hand. We can also define our own functions \ in ", StyleBox["Mathematica", FontSlant->"Italic"], " and then use these new functions to compute more complicated expressions. \ To reemphasize: in working with functions, there are two distinct steps: ", StyleBox["function definition", FontSlant->"Italic"], " and ", StyleBox["function evaluation", FontSlant->"Italic"], "." }], "Text", CellChangeTimes->{{3.3920456061124635`*^9, 3.392045609237081*^9}, { 3.392046889228669*^9, 3.392046984068825*^9}, 3.392480663377405*^9}], Cell[TextData[{ "In the previous problem, we created an expression that computed the \ absolute value of a variable ", Cell[BoxData["y"]], ". We could define a new function ", Cell[BoxData["myAbs"]], " that takes a single parameter ", Cell[BoxData["y"]], " and computes the absolute value of ", Cell[BoxData["y"]], ". (Of course, in practice, we would just use the built-in ", StyleBox["Mathematica", FontSlant->"Italic"], " function ", Cell[BoxData["Abs"]], ".) By convention, user\[Hyphen]defined functions in ", StyleBox["Mathematica", FontSlant->"Italic"], " always have names that begin with lower case letters. This convention \ distinguishes user\[Hyphen]defined functions from built\[Hyphen]in functions. \ " }], "Text", CellChangeTimes->{{3.3920456061124635`*^9, 3.392045609237081*^9}, 3.392046889228669*^9, {3.3920469927115974`*^9, 3.392047024288262*^9}, 3.392480686827145*^9, {3.3924811736731853`*^9, 3.39248117879049*^9}, { 3.392481363674556*^9, 3.392481375114152*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"myAbs", "[", "y_", "]"}], ":=", RowBox[{"If", "[", RowBox[{ RowBox[{"y", "<", "0"}], ",", RowBox[{"-", "y"}], ",", "y"}], "]"}]}]], "Input", CellChangeTimes->{{3.392047417849872*^9, 3.3920474214552*^9}}], Cell[TextData[{ "The underscore ", Cell[BoxData["_"]], " after the variable ", Cell[BoxData["y"]], " tells ", StyleBox["Mathematica", FontSlant->"Italic"], " that ", Cell[BoxData["y_"]], " is a ", StyleBox["pattern", FontSlant->"Italic"], " (i.e., ", Cell[BoxData["y"]], " is an parameter for the function). In ", StyleBox["Mathematica", FontSlant->"Italic"], ", patterns are used to give names to parts of an expression that have a \ particular structure. Anytime that ", StyleBox["Mathematica", FontSlant->"Italic"], " subsequently encounters an expression of the form ", Cell[BoxData[ RowBox[{"myAbs", "[", "\[Ellipsis]", "]"}]]], ", ", StyleBox["Mathematica", FontSlant->"Italic"], " will evaluate the ", Cell[BoxData["\[Ellipsis]"]], " expression and assign its value temporarily to the variable ", Cell[BoxData["y"]], ". Then, ", Cell[BoxData["myAbs"]], " will evaluate the right-hand side of its definition (", Cell[BoxData[ RowBox[{"If", "[", RowBox[{ RowBox[{"y", " ", "<", " ", "0"}], ",", " ", RowBox[{"-", "y"}], ",", " ", "y"}], "]"}]]], ") using this temporary value for ", Cell[BoxData["y"]], " and return this value as its answer." }], "Text", CellChangeTimes->{ 3.392047034964039*^9, {3.392047245725505*^9, 3.392047523395849*^9}, { 3.3920477396253963`*^9, 3.3920477518234224`*^9}, 3.3920562965409155`*^9, { 3.392480747452964*^9, 3.392480773780159*^9}, 3.392481250322494*^9, { 3.392481411964602*^9, 3.3924814215315943`*^9}, 3.3936095164621983`*^9, 3.408993868606782*^9, 3.45865713409375*^9}], Cell[TextData[{ "We can now evaluate ", Cell[BoxData["myAbs"]], " using various expressions as input," }], "Text", CellChangeTimes->{ 3.392047034964039*^9, {3.392047245725505*^9, 3.392047533270442*^9}, 3.392481168320038*^9, {3.3924812902110567`*^9, 3.392481290529231*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"myAbs", "[", RowBox[{"1", "+", "1"}], "]"}], "\[IndentingNewLine]", RowBox[{"myAbs", "[", RowBox[{"0", "*", "1"}], "]"}], "\[IndentingNewLine]", RowBox[{"myAbs", "[", RowBox[{ RowBox[{"-", "1"}], "-", "1"}], "]"}]}], "Input"], Cell[BoxData["2"], "Output"], Cell[BoxData["0"], "Output"], Cell[BoxData["2"], "Output"] }, Closed]], Cell[TextData[{ "To repeat, the definition of ", Cell[BoxData["myAbs"]], " causes evaluation of subsequent instances of the expression ", Cell[BoxData[ RowBox[{"myAbs", "[", "\[Ellipsis]", "]"}]]], " (such as the three above) in the following manner: First, the argument ", Cell[BoxData["\[Ellipsis]"]], " to ", Cell[BoxData["myAbs"]], " is evaluated. Next, the value of this expression is assigned to the ", StyleBox["temporary", FontSlant->"Italic"], " variable ", Cell[BoxData["y"]], ". Finally, the value of the expression ", Cell[BoxData[ RowBox[{"If", "[", RowBox[{ RowBox[{"y", "<", "0"}], ",", RowBox[{"-", "y"}], ",", "y"}], "]"}]]], " is returned as the value of ", Cell[BoxData[ RowBox[{"myAbs", "[", "\[Ellipsis]", "]"}]]], "." }], "Text", CellChangeTimes->{ 3.3920455993524733`*^9, {3.3920475517076893`*^9, 3.392047553470294*^9}, 3.3920563023695297`*^9, {3.392480863498815*^9, 3.392480881286132*^9}}], Cell[TextData[{ "Creating functions in ", StyleBox["Mathematica", FontSlant->"Italic"], " is your first real step towards programming. To reduce the number of \ errors that you will make in defining new functions, here are the Three Laws \ of Function Definition for ", StyleBox["Mathematica", FontSlant->"Italic"], " (in homage to ", ButtonBox["Asimov's Three Laws of Robotics", BaseStyle->"Hyperlink", ButtonData->{ URL["http://en.wikipedia.org/wiki/Three_Laws_of_Robotics"], None}, ButtonNote->"http://en.wikipedia.org/wiki/Three_Laws_of_Robotics"], ")." }], "Text", CellChangeTimes->{{3.3920470970557985`*^9, 3.3920471885009375`*^9}, { 3.392480933778686*^9, 3.392480934682522*^9}, 3.45865717521875*^9, { 3.46012740534375*^9, 3.46012740534375*^9}}], Cell[TextData[{ StyleBox["1. Always use square brackets in the function definition and \ function evaluation.", FontWeight->"Bold", FontColor->RGBColor[1, 0, 0]], " " }], "Text", CellChangeTimes->{{3.392047193468278*^9, 3.392047220207794*^9}, { 3.3920476178854876`*^9, 3.3920476880291467`*^9}, 3.392047723621746*^9, { 3.393609600620633*^9, 3.393609608898992*^9}}, FontWeight->"Bold", FontColor->RGBColor[1, 0, 0]], Cell[TextData[{ "For example, use ", Cell[BoxData[ RowBox[{ RowBox[{"f", "[", "x_", "]"}], ":=", RowBox[{"x", "+", "3"}]}]]], " instead of using ", Cell[BoxData[ RowBox[{ RowBox[{"f", RowBox[{"(", "x_", ")"}]}], "=", RowBox[{"x", "+", "3"}]}]]], "." }], "Text", CellChangeTimes->{{3.392047193468278*^9, 3.392047220207794*^9}, { 3.3920476178854876`*^9, 3.3920476880291467`*^9}, 3.392047723621746*^9}], Cell[TextData[{ "2. Always use ", Cell[BoxData[":="]], " instead of ", Cell[BoxData["="]], " in your function definition." }], "Text", CellChangeTimes->{{3.392047231484459*^9, 3.3920472372229395`*^9}, { 3.3920477301614103`*^9, 3.3920477333961906`*^9}, {3.392047778182376*^9, 3.3920477954178467`*^9}, {3.392047854675419*^9, 3.3920478548056107`*^9}, { 3.392048040109455*^9, 3.3920480767435937`*^9}}, FontWeight->"Bold", FontColor->RGBColor[1, 0, 0]], Cell[TextData[{ "For example, use ", Cell[BoxData[ RowBox[{ RowBox[{"f", "[", "x_", "]"}], ":=", RowBox[{"x", "+", "3"}]}]]], " instead of using ", Cell[BoxData[ RowBox[{ RowBox[{"f", "[", "x_", "]"}], "=", RowBox[{"x", "+", "3"}]}]]], ". Using ", Cell[BoxData["SetDelayed"]], " (", Cell[BoxData[":="]], ") causes your function not to be evaluated until you call it. This \ behavior mimics how most other programming languages work. (Later you will \ make use of ", Cell[BoxData["="]], " once you have a rock solid understanding of the difference between how ", Cell[BoxData[":="]], " and ", Cell[BoxData["="]], " behave." }], "Text", CellChangeTimes->{{3.392047193468278*^9, 3.392047220207794*^9}, { 3.3920476178854876`*^9, 3.3920476880291467`*^9}, 3.392047723621746*^9, { 3.3920480849557295`*^9, 3.3920482080075774`*^9}, {3.3924809702786283`*^9, 3.392480999419283*^9}, {3.3936096552451897`*^9, 3.393609655744314*^9}, 3.458657200515625*^9}], Cell["\<\ 3. Always remember to use an underscore after the parameters to your function.\ \>", "Text", CellChangeTimes->{{3.392047231484459*^9, 3.3920472372229395`*^9}, { 3.3920477301614103`*^9, 3.3920477333961906`*^9}, {3.392047778182376*^9, 3.3920477954178467`*^9}, {3.392047854675419*^9, 3.3920478548056107`*^9}, { 3.392048040109455*^9, 3.392048040199589*^9}, 3.392481517885528*^9}, FontWeight->"Bold", FontColor->RGBColor[1, 0, 0]], Cell[TextData[{ "For example, use ", Cell[BoxData[ RowBox[{ RowBox[{"f", "[", "x_", "]"}], ":=", RowBox[{"x", "+", "3"}]}]]], " instead of using ", Cell[BoxData[ RowBox[{ RowBox[{"f", "[", "x", "]"}], "=", RowBox[{"x", "+", "3"}]}]]], ". This is a common mistake even for experienced ", StyleBox["Mathematica", FontSlant->"Italic"], " users. A tip-off that this is the problem is if ", StyleBox["Mathematica", FontSlant->"Italic"], " acts like it doesn't recognize your definition. For example," }], "Text", CellChangeTimes->{{3.392047193468278*^9, 3.392047220207794*^9}, { 3.3920476178854876`*^9, 3.3920476880291467`*^9}, 3.392047723621746*^9, { 3.3920478270846443`*^9, 3.3920479126711254`*^9}, 3.392056310391384*^9, { 3.392481018675473*^9, 3.392481020515278*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"f", "[", "x", "]"}], ":=", RowBox[{"x", "+", "3"}]}]], "Input", CellChangeTimes->{{3.3920479147742333`*^9, 3.3920479334918947`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"f", "[", "3", "]"}]], "Input", CellChangeTimes->{{3.3920479147742333`*^9, 3.3920479334918947`*^9}}], Cell[BoxData[ RowBox[{"f", "[", "3", "]"}]], "Output", CellChangeTimes->{{3.392047928113947*^9, 3.3920479367467046`*^9}}] }, Open ]], Cell[TextData[{ "Note that ", StyleBox["Mathematica", FontSlant->"Italic"], " actually does have a definition for ", Cell[BoxData[ RowBox[{"f", "[", "x", "]"}]]], " stored. However, without the underscore after the variable ", Cell[BoxData["x"]], ", the definition matches only the exact expression ", Cell[BoxData[ RowBox[{"f", "[", "x", "]"}]]], " and nothing else. For example. " }], "Text", CellChangeTimes->{{3.3920479502867146`*^9, 3.392048034080546*^9}, { 3.392048234666975*^9, 3.3920482373509417`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"f", "[", "x", "]"}]], "Input", CellChangeTimes->{{3.392048004476797*^9, 3.392048005027611*^9}}], Cell[BoxData[ RowBox[{"3", "+", "x"}]], "Output", CellChangeTimes->{3.3920480054081736`*^9}] }, Open ]], Cell[TextData[{ "Finally, it is also possible to define functions of several variables. For \ example, the function ", Cell[BoxData[ RowBox[{"bothPos", "[", RowBox[{"x", ",", "y"}], "]"}]]], " returns the Boolean value ", Cell[BoxData["True"]], " if and only if both ", Cell[BoxData["x"]], " and ", Cell[BoxData["y"]], " are positive. Note that these definitions also follow the three laws \ given above." }], "Text", CellChangeTimes->{{3.3920475834746346`*^9, 3.392047607349918*^9}, { 3.392481062474525*^9, 3.392481068319648*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"bothPos", "[", RowBox[{"x_", ",", "y_"}], "]"}], ":=", RowBox[{"And", "[", RowBox[{ RowBox[{"x", ">", "0"}], ",", RowBox[{"y", ">", "0"}]}], "]"}]}]], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"bothPos", "[", RowBox[{"1", ",", "1"}], "]"}], "\[IndentingNewLine]", RowBox[{"bothPos", "[", RowBox[{ RowBox[{"-", "1"}], ",", "1"}], "]"}]}], "Input"], Cell[BoxData["True"], "Output"], Cell[BoxData["False"], "Output"] }, Open ]], Cell[CellGroupData[{ Cell["Exercises", "Exercise", CellChangeTimes->{{3.45916675834375*^9, 3.45916675975*^9}, 3.459169753671875*^9}], Cell[TextData[{ StyleBox["Write functions that perform the following calculations. Remember \ to append an underscore ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["_"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" after the names of parameters on the left-hand side of the \ function definition.", FontColor->RGBColor[0.5, 0, 0.5]], " ", StyleBox["For each of these function definitions, include at least 2 calls \ to the function to test the correctness of your definition.", FontColor->RGBColor[1, 0, 0]] }], "ExerciseText", CellChangeTimes->{{3.392048662949897*^9, 3.392048692553646*^9}, { 3.392056313505987*^9, 3.392056323090151*^9}, 3.392481603029922*^9, { 3.46012783659375*^9, 3.460127838*^9}}], Cell[TextData[{ StyleBox["1. Write a function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"double", "[", "x", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that returns twice the value of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.459169756625*^9, 3.459169756765625*^9}}], Cell[TextData[{ StyleBox["2. Write a function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"foobar", "[", "x", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that returns the value of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["xth"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" power.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920485247156124`*^9, 3.3920485479699783`*^9}, { 3.3920486025606527`*^9, 3.3920486350787086`*^9}, {3.45916975953125*^9, 3.45916975965625*^9}}], Cell[TextData[{ StyleBox["3. Write a function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"myPower", "[", RowBox[{"x", ",", "y"}], "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that returns the value of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["yth"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" power.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920485097935605`*^9, 3.392048510023901*^9}, { 3.392048623802044*^9, 3.3920486313832474`*^9}, {3.393609757186714*^9, 3.393609757301322*^9}, {3.45916976246875*^9, 3.459169762609375*^9}}], Cell[TextData[{ StyleBox["4. Redefine the function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["foobar"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to use your new function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["myPower"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920485097935605`*^9, 3.392048510023901*^9}, { 3.3920485936174364`*^9, 3.3920485988351474`*^9}, {3.392048642699971*^9, 3.3920486586034737`*^9}, {3.3920487302693825`*^9, 3.39204873065996*^9}, 3.458657583828125*^9, {3.4591697664375*^9, 3.45916976659375*^9}}], Cell[TextData[{ StyleBox["5. Write a function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"ascending", "[", RowBox[{"x", ",", "y", ",", "z"}], "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that returns ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["True"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" if and only if ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["x"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["y"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["z"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" are in ascending numerical order. For example,", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920484987872953`*^9, 3.392048503364059*^9}, { 3.45916976959375*^9, 3.45916976978125*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"ascending", "[", RowBox[{"1", ",", "2", ",", "3"}], "]"}], "\[IndentingNewLine]", RowBox[{"ascending", "[", RowBox[{"3", ",", "2", ",", "1"}], "]"}]}], "Input"], Cell[BoxData["True"], "Output"], Cell[BoxData["False"], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Exercises", "Exercise", CellChangeTimes->{{3.45916684559375*^9, 3.4591668466875*^9}, 3.459169777203125*^9}], Cell[TextData[StyleBox["The mathematical factorial function is defined as \ follows:", FontColor->RGBColor[0.5, 0, 0.5]]], "ExerciseText", CellChangeTimes->{3.4591668425*^9}], Cell[TextData[{ Cell[BoxData[ RowBox[{ RowBox[{"fact", "[", "n", "]"}], " ", "="}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"{", GridBox[{ {"1", RowBox[{ RowBox[{"if", " ", "n"}], " ", "==", " ", "0"}]}, { RowBox[{"n", " ", RowBox[{"fact", "[", RowBox[{"n", "-", "1"}], "]"}]}], "Otherwise"} }]}]], FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText"], Cell[TextData[{ StyleBox["Note that this is an example of a ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["recursive", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" definition, since ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["fact"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is defined in terms of itself. Note that this definition is not \ always self\[Hyphen]referential, since if ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"n", "\[Equal]", "0"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", then ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"fact", "[", "0", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is exactly one. The definition ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{ RowBox[{"fact", "[", "0", "]"}], "=", "1"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is the ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["base case", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" for the recursive definition. The base case serves to keep the \ definition from being infinitely self\[Hyphen]referential for \ non\[Hyphen]negative integers. As a simple thought exercise, you should try \ to compute the values of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["fact"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" on paper for small values of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["n"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" such as ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"1", ",", "2", ",", "3", ",", "4", ",", "5"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3924818309198513`*^9, 3.392481844817951*^9}, { 3.39360980959122*^9, 3.393609811247924*^9}, 3.460127805234375*^9}], Cell[TextData[{ StyleBox["1. Write a ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["fact"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that takes one argument and computes the mathematical factorial \ function. Your function should use an ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["If"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" statement to distinguish between the case when ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"n", "\[Equal]", "0"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"n", ">", "0"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Test your function on various non\[Hyphen]negative integers.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{ 3.3920487501788054`*^9, {3.459169779921875*^9, 3.45916978003125*^9}}], Cell[TextData[{ StyleBox["2. Use the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to find ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["'s built-in factorial function. Check your answers from part 1).", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920482660032845`*^9, 3.3920482708905067`*^9}, { 3.4591697824375*^9, 3.459169782578125*^9}, {3.46012775559375*^9, 3.4601277556875*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercises", "Exercise", CellChangeTimes->{{3.45916690028125*^9, 3.459166901546875*^9}, 3.459169798296875*^9}], Cell[TextData[StyleBox["The Fibonacci numbers are defined recursively as \ follows:", FontColor->RGBColor[0.5, 0, 0.5]]], "ExerciseText"], Cell[TextData[{ Cell[BoxData[ RowBox[{ RowBox[{"fib", "[", "n", "]"}], " ", "="}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"{", GridBox[{ {"0", RowBox[{ RowBox[{"if", " ", "n"}], "\[Equal]", "0"}]}, {"1", RowBox[{ RowBox[{"if", " ", "n"}], "\[Equal]", "1"}]}, { RowBox[{ RowBox[{"fib", "[", RowBox[{"n", "-", "2"}], "]"}], "+", RowBox[{"fib", "[", RowBox[{"n", "-", "1"}], "]"}]}], "Otherwise"} }]}]], FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText"], Cell[TextData[{ StyleBox["1. Write a ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["fib"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that takes a non\[Hyphen]negative integer as a parameter and \ computes the corresponding Fibonacci number. Test your functions on various \ positive integers. (Hint: You will need either to nest ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["If"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["s or use the Boolean function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Or"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Use the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to determine the syntax and behavior of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ FormBox[ StyleBox["Or", FontFamily->"Courier New"], TraditionalForm]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".)", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.392048403476444*^9, 3.3920484064007654`*^9}, 3.393609864501337*^9, {3.459169793734375*^9, 3.45916979459375*^9}, { 3.460127736*^9, 3.4601277364375*^9}}], Cell[TextData[{ StyleBox["2. Use the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to find ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["'s built-in function for Fibonacci numbers. Check the correctness \ of your function in part 1).", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920483927806377`*^9, 3.39204839616564*^9}, { 3.459169790296875*^9, 3.45916979134375*^9}, 3.460127771984375*^9}], Cell[TextData[{ StyleBox["3. Evaluate your function for Fibonacci numbers for larger and \ larger integer arguments. Try evaluating ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"fib", "[", "40", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". You may notice that the evaluation takes a long time. Hit ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Alt", "+", "."}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to abort ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["'s evaluation process. Try running the built-in Fibonacci function \ with the same arguments. Do you notice a difference?", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920483752246933`*^9, 3.3920483825655413`*^9}, { 3.459169806765625*^9, 3.459169807796875*^9}}], Cell[TextData[{ StyleBox["4.", FontColor->RGBColor[0.5, 0, 0.5]], " ", StyleBox["Bonus:", FontColor->RGBColor[1, 0, 0]], " ", StyleBox["Can you think of a way to improve the speed of your function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Fib"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["? Look up ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"memo", " ", "functions"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" in the master index of the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920482821070824`*^9, 3.3920482907198105`*^9}, { 3.392048366171314*^9, 3.392048366171314*^9}, 3.459166862484375*^9, { 3.459169803265625*^9, 3.4591698356875*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.459166906796875*^9, 3.459166908*^9}}], Cell[TextData[{ StyleBox["Define a variable ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["y"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" with the value ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["3"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Next, compute ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"myAbs", "[", "4", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" as defined above. What is the value of the variable ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["y"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" after evaluating ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"myAbs", "[", "4", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["? Did the value of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["y"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" change? Explain what happened.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.393609919260989*^9, 3.39360991960376*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Lists", "Section"], Cell[TextData[{ "So far we have seen only ", StyleBox["atomic", FontSlant->"Italic"], " values in ", StyleBox["Mathematica ", FontSlant->"Italic"], "(i.e., data consisting of only a single piece of information), such as \ integers, real numbers, and booleans. However, the most important aggregate \ data type in ", StyleBox["Mathematica", FontSlant->"Italic"], " is the ", StyleBox["list", FontSlant->"Italic"], ". A list consists of a sequence of items enclosed by curly braces and \ separated by commas. For example, the list consisting of the numbers ", Cell[BoxData["1"]], ", ", Cell[BoxData["2"]], ", ", Cell[BoxData["3"]], ", and ", Cell[BoxData["4"]], " has the form" }], "Text", CellChangeTimes->{{3.3920490313343*^9, 3.3920490339681926`*^9}, { 3.392049164230696*^9, 3.3920491685370603`*^9}, {3.3920500039015727`*^9, 3.392050010090719*^9}, {3.3924837878969097`*^9, 3.39248381967351*^9}, 3.45865776084375*^9}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}]], "Input", CellChangeTimes->{{3.3920500129449368`*^9, 3.3920500132654104`*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}]], "Output", CellChangeTimes->{3.392050015118149*^9}] }, Open ]], Cell[TextData[{ "Note that internally, ", StyleBox["Mathematica", FontSlant->"Italic"], " represents this list in the form ", Cell[BoxData[ RowBox[{"List", "[", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "]"}]]], "." }], "Text", CellChangeTimes->{{3.392054760160433*^9, 3.3920547849270334`*^9}, 3.3924838410181847`*^9}], Cell[CellGroupData[{ Cell["Creating lists", "Subsection", CellChangeTimes->{{3.392049355202917*^9, 3.392049357366114*^9}}], Cell[TextData[{ "There are several ways to create lists in ", StyleBox["Mathematica", FontSlant->"Italic"], ". The easiest way is simply to enumerate the entries in the list as done \ above. " }], "Text", CellChangeTimes->{{3.392049362503707*^9, 3.3920495686783943`*^9}, { 3.3920499249749336`*^9, 3.392049987216916*^9}, {3.3920502891230764`*^9, 3.3920503060080295`*^9}, {3.392051135333617*^9, 3.3920511364352455`*^9}, { 3.392483859951921*^9, 3.3924838680277767`*^9}}], Cell[CellGroupData[{ Cell["Built-in functions", "Subsubsection", CellChangeTimes->{{3.392051140300958*^9, 3.3920511451981955`*^9}}], Cell[TextData[{ "The ", StyleBox["Mathematica", FontSlant->"Italic"], " function ", Cell[BoxData["Range"]], " is also a very useful function for creating simple lists of numbers. Be \ aware that ", Cell[BoxData[ FormBox[ StyleBox["Range", FontFamily->"Courier New"], TraditionalForm]]], " has many variants to handle more complex demands." }], "Text", CellChangeTimes->{{3.392049362503707*^9, 3.3920495686783943`*^9}, { 3.3920499249749336`*^9, 3.392049987216916*^9}, {3.3920502891230764`*^9, 3.3920503060080295`*^9}, {3.392051135333617*^9, 3.3920511364352455`*^9}, { 3.3924838919744873`*^9, 3.392483900379138*^9}, {3.458657809765625*^9, 3.45865782109375*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Range", "[", "4", "]"}]], "Input", CellChangeTimes->{{3.3920499897406454`*^9, 3.3920499947981195`*^9}, { 3.3920500277968855`*^9, 3.3920500278970337`*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}]], "Output", CellChangeTimes->{{3.392050025062845*^9, 3.392050028437833*^9}}] }, Open ]], Cell[TextData[{ "Unfortunately, life is not always so simple. In many cases, we wish to \ build a function that processes lists in some manner. In ", StyleBox["Mathematica", FontSlant->"Italic"], ", there are two standard ways to create lists: ", StyleBox["recursively", FontSlant->"Italic"], " or ", StyleBox["iteratively. ", FontSlant->"Italic"] }], "Text", CellChangeTimes->{{3.392049362503707*^9, 3.3920495686783943`*^9}, { 3.3920499249749336`*^9, 3.39204994393295*^9}, 3.3936100456718197`*^9, 3.4089942658877897`*^9}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.3920512833523617`*^9, 3.3920512871479707`*^9}, { 3.45916693215625*^9, 3.45916693340625*^9}}], Cell[TextData[{ StyleBox["Use the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to find a variant of the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Range"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" function that produces the list ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"{", RowBox[{"4", ",", "3", ",", "2", ",", "1"}], "}"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920512980641026`*^9, 3.3920513256048026`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Recursion", "Subsubsection", CellChangeTimes->{{3.392051148803523*^9, 3.3920511523888216`*^9}}], Cell[TextData[{ "Recursive constructions for lists use the functions ", Cell[BoxData["Append"]], " (or ", Cell[BoxData["Prepend"]], ") to add members to the end (or beginning) of the list. To illustrate this \ process, here is a simple recursive definition of a list" }], "Text", CellChangeTimes->{{3.3920491769695215`*^9, 3.392049201445693*^9}, { 3.392049254123541*^9, 3.392049340841694*^9}, {3.392049555528962*^9, 3.392049706532116*^9}, {3.3920498457678804`*^9, 3.3920498642552013`*^9}, { 3.3920501779788265`*^9, 3.392050192370094*^9}, 3.3920563274966626`*^9}], Cell[TextData[{ StyleBox["A list is either the empty list ", FontColor->RGBColor[1, 0, 0]], Cell[BoxData[ RowBox[{"{", "}"}]], FontColor->RGBColor[1, 0, 0]], StyleBox[" or ", FontColor->RGBColor[1, 0, 0]], Cell[BoxData[ RowBox[{"Append", "[", RowBox[{"l", ",", "x"}], "]"}]], FontColor->RGBColor[1, 0, 0]], StyleBox[", where ", FontColor->RGBColor[1, 0, 0]], Cell[BoxData["l"], FontColor->RGBColor[1, 0, 0]], StyleBox[" is also a list.", FontColor->RGBColor[1, 0, 0]] }], "Text", CellChangeTimes->{{3.3920491769695215`*^9, 3.392049201445693*^9}, { 3.392049254123541*^9, 3.392049340841694*^9}, {3.392049555528962*^9, 3.392049778107892*^9}, {3.392050057460723*^9, 3.392050072272612*^9}, { 3.3920501839176025`*^9, 3.3920501847288017`*^9}, {3.392050978822323*^9, 3.3920509876754065`*^9}, {3.458657958328125*^9, 3.458657963625*^9}}], Cell[TextData[{ "Here is a function that create a list of integers from ", Cell[BoxData["1"]], " to ", Cell[BoxData["n"]], "." }], "Text", CellChangeTimes->{{3.3920500753571706`*^9, 3.3920500972795677`*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"makeList", "[", "n_", "]"}], ":=", RowBox[{"If", "[", RowBox[{ RowBox[{"n", "\[Equal]", "0"}], ",", RowBox[{"{", "}"}], ",", RowBox[{"Append", "[", RowBox[{ RowBox[{"makeList", "[", RowBox[{"n", "-", "1"}], "]"}], ",", "n"}], "]"}]}], "]"}]}]], "Input", CellChangeTimes->{{3.392050099232454*^9, 3.3920501480546036`*^9}, { 3.392050207762841*^9, 3.392050208674188*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"makeList", "[", "4", "]"}]], "Input", CellChangeTimes->{{3.3920501394218464`*^9, 3.392050141384747*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}]], "Output", CellChangeTimes->{ 3.392050149536794*^9, {3.3920502046482387`*^9, 3.3920502115584507`*^9}}] }, Open ]], Cell[TextData[{ "Note that this definition of ", Cell[BoxData["makeList"]], " has a structure very similar to the recursive definition of factorial in \ the previous section. The definition checks whether ", Cell[BoxData[ RowBox[{"n", "\[Equal]", "0"}]]], " (the base case) and returns our definition of what a list of length zero \ looks like (", Cell[BoxData[ RowBox[{"{", "}"}]]], ") when ", Cell[BoxData[ RowBox[{"n", "\[Equal]", "0"}]]], ". Otherwise, makeList calls itself recursively using the smaller value ", Cell[BoxData[ RowBox[{"n", "-", "1"}]]], ". By repeatedly decreasing ", Cell[BoxData["n"]], ", ", Cell[BoxData["makeList"]], " eventually hits the case ", Cell[BoxData[ RowBox[{"n", "\[Equal]", "0"}]]], " and stops." }], "Text", CellChangeTimes->{{3.392050922499088*^9, 3.392051118088132*^9}, 3.3920563346071706`*^9, {3.392484057697773*^9, 3.392484181214408*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.3920512833523617`*^9, 3.3920512871479707`*^9}, { 3.459166952421875*^9, 3.459166953484375*^9}}], Cell[TextData[{ StyleBox["Use ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Prepend"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and modify the definition of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["makeList"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to produce the list of numbers from ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["n"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["1"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" in decreasing order.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920512980641026`*^9, 3.3920514050121517`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Iteration", "Subsubsection", CellChangeTimes->{{3.3920511602504396`*^9, 3.392051161221875*^9}}], Cell[TextData[{ "Another method for creating lists is to construct the list ", StyleBox["iteratively", FontSlant->"Italic"], " using the ", Cell[BoxData["Table"]], " function. Here is an alternative definition of ", Cell[BoxData["makeList"]], " using ", Cell[BoxData["Table"]], "." }], "Text", CellChangeTimes->{{3.3920503831420193`*^9, 3.392050412295102*^9}, { 3.392050467376502*^9, 3.3920505197138467`*^9}, {3.392484272092114*^9, 3.392484275794186*^9}, 3.458657896921875*^9, 3.4586628795*^9}], Cell[BoxData[ RowBox[{ RowBox[{"makeList", "[", "n_", "]"}], ":=", RowBox[{"Table", "[", RowBox[{"i", ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "n"}], "}"}]}], "]"}]}]], "Input", CellChangeTimes->{{3.392050099232454*^9, 3.3920501480546036`*^9}, { 3.392050207762841*^9, 3.392050208674188*^9}, {3.39205052754542*^9, 3.3920505342353067`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"makeList", "[", "4", "]"}]], "Input", CellChangeTimes->{{3.3920505381510935`*^9, 3.3920505401740828`*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}]], "Output", CellChangeTimes->{3.3920505404544973`*^9}] }, Open ]], Cell[TextData[{ StyleBox["The function ", FontColor->RGBColor[1, 0, 0]], Cell[BoxData["Table"], FontColor->RGBColor[1, 0, 0]], StyleBox[" is the most important Mathematica function that you will learn in \ this class.", FontColor->RGBColor[1, 0, 0]], " One of your first goals in the class is to become a skilled user of ", Cell[BoxData["Table"]], ". To this end, let's examine ", Cell[BoxData["Table"]], " in more detail. In our example, ", Cell[BoxData["Table"]], " takes two parameters: the expression ", Cell[BoxData["i"]], " and an ", StyleBox["iterator ", FontSlant->"Italic"], Cell[BoxData[ RowBox[{"{", RowBox[{"i", ",", "1", ",", "4"}], "}"}]]], ". In ", StyleBox["Mathematica", FontSlant->"Italic"], ", an iterator expresses a range of values that a variable should take on \ during some type of computation. In this case, the variable ", Cell[BoxData["i"]], " should take on the values from ", Cell[BoxData["1"]], " to ", Cell[BoxData["4"]], ", increasing by the (default) stride of ", Cell[BoxData["1"]], ". The ", Cell[BoxData["Table"]], " command then creates a list with the expression ", Cell[BoxData["i"]], " taking each of these values." }], "Text", CellChangeTimes->{{3.3920505476651535`*^9, 3.3920506837362413`*^9}, { 3.392051180360158*^9, 3.3920512654258695`*^9}, {3.392051420985758*^9, 3.392051442747918*^9}, {3.3924843187023067`*^9, 3.392484363927094*^9}, { 3.393610268902853*^9, 3.393610294257617*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.3920512833523617`*^9, 3.3920512871479707`*^9}, { 3.459166958953125*^9, 3.459166959984375*^9}}], Cell[TextData[{ StyleBox["Modify the iterator in ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["makeList"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to generate the list in reverse order.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920512980641026`*^9, 3.3920514050121517`*^9}, { 3.3920514550060334`*^9, 3.3920514805237436`*^9}, {3.3924844190832567`*^9, 3.3924844337950087`*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Taking lists apart", "Subsection", CellChangeTimes->{{3.3920498273907223`*^9, 3.3920498310861835`*^9}}], Cell[TextData[{ "Often you will need to extract various pieces of a list. ", StyleBox["Mathematica", FontSlant->"Italic"], " supports multiple functions to perform this task. In the recursive style, \ ", StyleBox["Mathematica", FontSlant->"Italic"], " supports the functions ", Cell[BoxData["First"]], " and ", Cell[BoxData["Last"]], " that return the first and last elements of a list. The functions ", Cell[BoxData["Rest"]], " and ", Cell[BoxData["Most"]], " return the list with either its first or its last element removed. For \ example," }], "Text", CellChangeTimes->{{3.392051497308549*^9, 3.392051510698336*^9}, { 3.392051562434793*^9, 3.3920515953734703`*^9}, {3.3920516302650337`*^9, 3.3920516680108147`*^9}, {3.3920541502190537`*^9, 3.392054190017869*^9}, { 3.3924844590100727`*^9, 3.3924844936723127`*^9}, {3.393610313697445*^9, 3.393610314428721*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"First", "[", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}], "]"}], "\[IndentingNewLine]", RowBox[{"Last", "[", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}], "]"}], "\[IndentingNewLine]", RowBox[{"Rest", "[", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}], "]"}], "\[IndentingNewLine]", RowBox[{"Most", "[", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}], "]"}]}], "Input", CellChangeTimes->{{3.3920541929522057`*^9, 3.392054217157977*^9}}], Cell[BoxData["1"], "Output", CellChangeTimes->{3.3920542174383917`*^9, 3.392484509307588*^9}], Cell[BoxData["4"], "Output", CellChangeTimes->{3.3920542174383917`*^9, 3.392484509324312*^9}], Cell[BoxData[ RowBox[{"{", RowBox[{"2", ",", "3", ",", "4"}], "}"}]], "Output", CellChangeTimes->{3.3920542174383917`*^9, 3.39248450941887*^9}], Cell[BoxData[ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]], "Output", CellChangeTimes->{3.3920542174383917`*^9, 3.39248450951089*^9}] }, Open ]], Cell["\<\ These functions will be particularly useful when you are manipulating lists \ recursively.\ \>", "Text", CellChangeTimes->{{3.3920542299669065`*^9, 3.392054253752056*^9}, 3.392484523278194*^9}], Cell[TextData[{ "For iterative applications, ", StyleBox["Mathematica", FontSlant->"Italic"], " supports the function ", Cell[BoxData[ RowBox[{"Part", "[", RowBox[{"l", ",", "i"}], "]"}]]], " that returns the ", Cell[BoxData["i"]], "th element of the list ", Cell[BoxData["l"]], ". This function is used so often that ", StyleBox["Mathematica", FontSlant->"Italic"], " supports the shorthand form ", Cell[BoxData[ RowBox[{"l", "[", RowBox[{"[", "i", "]"}], "]"}]]], " or ", Cell[BoxData[ RowBox[{"l", "\[LeftDoubleBracket]", "i", "\[RightDoubleBracket]"}]]], ". (The special bracket ", Cell[BoxData["\[LeftDoubleBracket]"]], " is formed by typing ", Cell[BoxData[ RowBox[{"Escape", " ", "[", RowBox[{"[", " ", "Escape"}]}]]], ".) Remember this operation, you will use it often." }], "Text", CellChangeTimes->{{3.392054261803955*^9, 3.3920543283122425`*^9}, { 3.3920543640750933`*^9, 3.3920544141991673`*^9}, {3.392054556859993*^9, 3.392054574445982*^9}, 3.3920563408463907`*^9, {3.458663066453125*^9, 3.458663066546875*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"l", "=", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}]}], "\n", RowBox[{"Part", "[", RowBox[{"l", ",", "3"}], "]"}], "\[IndentingNewLine]", RowBox[{"l", "[", RowBox[{"[", "3", "]"}], "]"}], "\[IndentingNewLine]", RowBox[{"l", "\[LeftDoubleBracket]", "3", "\[RightDoubleBracket]"}]}], "Input", CellChangeTimes->{{3.3920543411912746`*^9, 3.392054359628522*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}]], "Output", CellChangeTimes->{{3.3920543355729723`*^9, 3.392054360279484*^9}}], Cell[BoxData["3"], "Output", CellChangeTimes->{{3.3920543355729723`*^9, 3.3920543602995133`*^9}}], Cell[BoxData["3"], "Output", CellChangeTimes->{{3.3920543355729723`*^9, 3.392054360329558*^9}}], Cell[BoxData["3"], "Output", CellChangeTimes->{{3.3920543355729723`*^9, 3.392054360359602*^9}}] }, Open ]], Cell[TextData[{ "A common error when using ", Cell[BoxData["Part"]], " is to try and access a portion of the list that does not exist. For \ example, since list indices start at ", Cell[BoxData["1"]], ", trying to take the ", Cell[BoxData["0"]], "th element in the list yields nonsense (the value ", Cell[BoxData["List"]], "). Asking for an element after the end of the list yields an error." }], "Text", CellChangeTimes->{{3.39205464012304*^9, 3.3920547499453373`*^9}, { 3.3920563432198987`*^9, 3.3920563480069733`*^9}, {3.392484582689405*^9, 3.392484605511735*^9}, {3.458663084296875*^9, 3.458663109140625*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"l", "\[LeftDoubleBracket]", "0", "\[RightDoubleBracket]"}], "\[IndentingNewLine]", RowBox[{"l", "\[LeftDoubleBracket]", "5", "\[RightDoubleBracket]"}]}], "Input", CellChangeTimes->{{3.3920547266208677`*^9, 3.392054735574099*^9}}], Cell[BoxData["List"], "Output", CellChangeTimes->{3.392054736114898*^9}], Cell[BoxData[ RowBox[{ RowBox[{"Part", "::", "\<\"partw\"\>"}], ":", " ", "\<\"Part \\!\\(5\\) of \\!\\({1, 2, 3, 4}\\) does not exist. \ \\!\\(\\*ButtonBox[\\\"\[RightSkeleton]\\\", ButtonStyle->\\\"Link\\\", \ ButtonFrame->None, ButtonData:>\\\"paclet:ref/message/General/partw\\\", \ ButtonNote -> \\\"Part::partw\\\"]\\)\"\>"}]], "Message", "MSG", CellChangeTimes->{3.3920547366056232`*^9}], Cell[BoxData[ RowBox[{ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}], "\[LeftDoubleBracket]", "5", "\[RightDoubleBracket]"}]], "Output", CellChangeTimes->{3.392054736635668*^9}] }, Open ]], Cell[TextData[{ "This overview just skims the surface of ", StyleBox["Mathematica", FontSlant->"Italic"], "'s capabilities concerning lists. Spend a couple of minutes browsing the \ section ", ButtonBox["List Manipulation", BaseStyle->"Link", ButtonData->"paclet:guide/ListManipulation"], " in the ", Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]]], " to get a better idea of ", StyleBox["Mathematica", FontSlant->"Italic"], "'s list capabilities." }], "Text", CellChangeTimes->{{3.3920548080011325`*^9, 3.392054896241535*^9}, { 3.3920563539858084`*^9, 3.392056357781418*^9}}], Cell[CellGroupData[{ Cell["Exercises", "Exercise", CellChangeTimes->{{3.459166973171875*^9, 3.45916697428125*^9}, 3.45916985996875*^9}], Cell[TextData[{ StyleBox["1a. Generate a list consisting of the first hundred prime numbers \ using ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Table"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". As usual, ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" has a built\[Hyphen]in function that will make this problem easy \ to solve.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.392054919195457*^9, 3.3920549217892904`*^9}, { 3.393610408515752*^9, 3.393610410105043*^9}, {3.459169865421875*^9, 3.459169866984375*^9}}], Cell[TextData[{ StyleBox["1b. In your previous solution, the length of the list was \ hard-coded to be ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["100"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Instead, build a function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"primeList", "[", "n", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that builds a list of the first ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["n"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" prime numbers.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.392054919195457*^9, 3.3920549217892904`*^9}, { 3.392054984792397*^9, 3.3920550373801117`*^9}, 3.45866319196875*^9, { 3.45916986940625*^9, 3.459169871109375*^9}}], Cell[TextData[{ StyleBox["2a. Write a function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"sqrList", "[", "l", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that takes a list ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["l"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" of length ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["10"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" as input and returns a new list consisting of the squares of the \ elements in ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["l"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". While there are many ways to solve this problem, I suggest a \ solution of the form", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.392054971472713*^9, 3.3920549715928907`*^9}, { 3.392055052822933*^9, 3.3920550753762627`*^9}, {3.392484664472193*^9, 3.3924846677640963`*^9}, {3.459169874015625*^9, 3.45916988565625*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"sqrList", "[", "l_", "]"}], ":=", RowBox[{"Table", "[", RowBox[{"\[Ellipsis]", ",", RowBox[{"{", RowBox[{"i", ",", "1", ",", "10"}], "}"}]}], "]"}]}]], "Input", CellChangeTimes->{{3.392054948588895*^9, 3.392054966755742*^9}, { 3.392055078230481*^9, 3.392055101064225*^9}}], Cell[TextData[{ StyleBox["Your task is to replace the expression ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["\[Ellipsis]"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" by an appropriate expression involving ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["l"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["i"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText"], Cell[TextData[{ StyleBox["2b. In the previous problem, I restricted the input list ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["l"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to have a length of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["10"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" so that the upper bound in the iterator ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"{", RowBox[{"i", ",", "1", ",", "10"}], "}"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" was fixed. In this problem, I would like you to revise your \ solution so that the input list ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["l"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" can have any length. To accomplish this task, you will need to \ replace the number ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["10"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" in the iterator ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"{", RowBox[{"i", ",", "1", ",", "10"}], "}"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" with an expression involving ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["l"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that returns the length of the list ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["l"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Use the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to find such a function.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.392054971472713*^9, 3.3920549715928907`*^9}, { 3.392055052822933*^9, 3.3920552562635803`*^9}, 3.3920563618674564`*^9, { 3.392484716449411*^9, 3.392484718968495*^9}, {3.3936104924328012`*^9, 3.393610500390998*^9}, {3.459169888453125*^9, 3.45916989003125*^9}}] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Basic Graphics", "Section"], Cell[TextData[{ "The eye is probably the most potent sensory organ for human beings and as \ we all know a single picture is worth a thousand words. For these and other \ reasons, we want to be able to produce visual representations of scientific, \ mathematical, or technical problems. The following two functions in ", StyleBox["Mathematica", FontSlant->"Italic"], " are the workhorses for making plots." }], "Text", CellChangeTimes->{{3.392484751937332*^9, 3.3924847599651318`*^9}}], Cell[CellGroupData[{ Cell[TextData[Cell[BoxData["ListPlot"]]], "Subsection"], Cell[TextData[{ "In ", StyleBox["Mathematica", FontSlant->"Italic"], " the function ", Cell[BoxData["ListPlot"]], " can be used to create plots of lists. ", Cell[BoxData[ RowBox[{"ListPlot", "[", "l", "]"}]]], " creates a plot of values ", Cell[BoxData[ RowBox[{"l", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]]], ", ", Cell[BoxData[ RowBox[{"l", "\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]]], ", ..., ", Cell[BoxData[ RowBox[{"l", "\[LeftDoubleBracket]", "n", "\[RightDoubleBracket]"}]]], "at the parameter values ", Cell[BoxData[ RowBox[{"1", ",", " ", "2", ",", " ", "...", ",", " ", "n"}]]], "." }], "Text", CellChangeTimes->{{3.392055300158449*^9, 3.3920553163023067`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{"{", RowBox[{"1", ",", "4", ",", "9", ",", "16", ",", "25", ",", "36"}], "}"}], "]"}]], "Input"], Cell[BoxData[ GraphicsBox[{{}, {Hue[0.67, 0.6, 0.6], PointBox[{{1., 1.}, {2., 4.}, {3., 9.}, {4., 16.}, {5., 25.}, {6., 36.}}]}, {}}, AspectRatio->NCache[GoldenRatio^(-1), 0.6180339887498948], Axes->True, AxesOrigin->{0, 0}, PlotRange->{{0, 6.}, {0., 36.}}, PlotRangeClipping->True, PlotRangePadding->{ Scaled[0.02], Scaled[0.02]}]], "Output", CellChangeTimes->{3.3920553277091637`*^9}] }, Open ]], Cell["You can use the following form to join the points by lines:", "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "4", ",", "9", ",", "16", ",", "25", ",", "36"}], "}"}], ",", RowBox[{"Joined", "\[Rule]", "True"}]}], "]"}]], "Input"], Cell[BoxData[ GraphicsBox[{{}, {}, {Hue[0.67, 0.6, 0.6], LineBox[{{1., 1.}, {2., 4.}, {3., 9.}, {4., 16.}, {5., 25.}, {6., 36.}}]}}, AspectRatio->NCache[GoldenRatio^(-1), 0.6180339887498948], Axes->True, AxesOrigin->{0, 0}, PlotRange->{{0, 6.}, {0., 36.}}, PlotRangeClipping->True, PlotRangePadding->{ Scaled[0.02], Scaled[0.02]}]], "Output", CellChangeTimes->{3.392055330653515*^9, 3.392484829354491*^9}] }, Open ]], Cell[TextData[{ "The extra parameter ", Cell[BoxData[ RowBox[{"Joined", "\[Rule]", "True"}]]], " is an ", StyleBox["option", FontSlant->"Italic"], " to the function ", Cell[BoxData["ListPlot"]], ". Most of the plotting functions in ", StyleBox["Mathematica", FontSlant->"Italic"], " have numerous options that allow the user to control the appearance of a \ plot. The ", Cell[BoxData[ RowBox[{"Documentation", " ", "Center"}]]], " contains lots of information on appropriate options." }], "Text", CellChangeTimes->{{3.392055334559287*^9, 3.3920553559609146`*^9}, 3.3920563716318865`*^9}], Cell[CellGroupData[{ Cell["Exercises", "Exercise", CellChangeTimes->{{3.459167022140625*^9, 3.459167024890625*^9}, 3.4591698970625*^9}], Cell[TextData[{ StyleBox["1. Write a function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"fibTable", "[", "n", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that computes a list of the first ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["n"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" Fibonacci numbers using ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Table"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Use ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["'s built-in function for Fibonacci numbers, not your own \ implementation.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.459169926375*^9, 3.45916992734375*^9}}], Cell[TextData[{ StyleBox["2. Create a plot of the Fibonacci numbers from ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["1"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["20"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" using your function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"fibTable", " "}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["and the built in function", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{" ", "ListPlot"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Join the points with line segments.", FontColor->RGBColor[0.5, 0, 0.5]], "\n3.", StyleBox[" Write a function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"fibRatio", "[", "n", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that creates a table containing the ratio of two successive \ Fibonacci numbers ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ FractionBox[ RowBox[{"fib", "[", RowBox[{"i", "+", "1"}], "]"}], RowBox[{"fib", "[", "i", "]"}]]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Use ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["ListPlot"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to draw the first ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["19"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ratios. What do you think the ratio will be for larger values of", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{" ", "i"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["? The limit is a built-in constant in ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" called the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["GoldenRatio"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.392055394638072*^9, 3.3920554115931287`*^9}, { 3.393610601354596*^9, 3.3936106384171658`*^9}, {3.459169929203125*^9, 3.45916993234375*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[TextData[Cell[BoxData["Plot"]]], "Subsection"], Cell[TextData[{ "The function ", Cell[BoxData[ RowBox[{"ListPlot", " "}]]], "can be used to create plots of discrete sequences, i.e., a list of numbers. \ Often we want to create visual representations of continuous functions such \ as the function ", Cell[BoxData[ SuperscriptBox["x", "2"]]], ". ", StyleBox["Mathematica", FontSlant->"Italic"], " can create plots of such continuous functions using the function ", Cell[BoxData[ RowBox[{"Plot", "[", RowBox[{"expr", ",", RowBox[{"{", RowBox[{"x", ",", "min", ",", "max"}], "}"}]}], "]"}]]], ". Here, ", Cell[BoxData["expr"]], " is a ", StyleBox["Mathematica", FontSlant->"Italic"], " expression containing the variable ", Cell[BoxData["x"]], " and ", Cell[BoxData[ RowBox[{"{", RowBox[{"x", ",", "min", ",", "max"}], "}"}]]], " is an iterator that specifies that ", Cell[BoxData["x"]], " should vary from ", Cell[BoxData["min"]], " to ", Cell[BoxData["max"]], ". This function will create a plot of the values of ", Cell[BoxData["expr"]], " with ", Cell[BoxData["x"]], " varying between the range ", Cell[BoxData["min"]], " and ", Cell[BoxData["max"]], ". For example:" }], "Text", CellChangeTimes->{{3.3920554383727036`*^9, 3.392055468407089*^9}, 3.392484931390963*^9, {3.3924849923772907`*^9, 3.392485017007979*^9}, { 3.393610696604452*^9, 3.393610712724518*^9}, {3.3936107469915743`*^9, 3.393610755977509*^9}, 3.458665012609375*^9}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Plot", "[", RowBox[{ SuperscriptBox["x", "2"], ",", RowBox[{"{", RowBox[{"x", ",", "1", ",", "6"}], "}"}]}], "]"}]], "Input"], Cell[BoxData[ GraphicsBox[{{}, {}, {Hue[0.67, 0.6, 0.6], LineBox[CompressedData[" 1:eJwV0ns01GkYwPFxlwYzjTGL2ojUTJJQKOt9TEvsyCVJxq4dtywt1RQbTi0V B+USK6TYxDpLhJJcYzTF5FJYiaSb2Vrhx7hUg9m3P57zPZ/zPH8+BgFH9wfL k0gkVzxfq7w9SI9EItDugI6dX2up2k93UiaQlYW0KJNMoMAxe0oG7l7HimWm DoFaU9cr6ePmm6efCTEnUMx/w1MIl2H0Q+W6YAJN3/AQ/B5EoJCwq8KoxwQa 0kahsg4Ccc/X9I/kz6C/l3TqVi7NoKzJ4vDjHAma33E8v9tBgmpqR4Y+7VxE w24njRtfzKFjPh4K9RZS5Bstsq44soBqW7xJM2EylEARZB1d+wkZ3AsramXJ g1NScLZ71WcUP13Mcy5UhJrwsjG5w1JUmeXv9UukCtQHsKvraMtI3jmZ0XlE DbjWicyLxSto0XLi44C3Ojxt3hGZu4UEg+Zpt0u9KdDmtrI5ZoEExqPq7E4p FeJCj+n7NcuB9/KgI7OcBrRdFrasbHm48/lcmXo2HWa/cMoT/BSg6d/u0XI+ A0r4tXad1orwfffPpa1pOrCJ30HX1FQCvm2CMLtOD+b9t8O7QSX4k5hy/al3 HeSoTqRdv6UMOrJmbTZdHxIKv5hNRatAfEnpiraNAeyd5C5u8FIFyk1i7nLc BhgSBMlxTVYBL7xSN6XbEGqTHvjpz6+CsvmXnEMaG+GwUuSrnh41OBkbX+8U aQyB/Nha8pXVcNAglzzSswm2hWzpcfEng7ml8Z6ILUzoXWOyOtJGHZ50vHW9 UMWCxsqz+SYKGtDgZzYn2WgCsXIs965RDRhJiAl9JdwKai9inCLuaMKxc3nK 6aHb4EQ/ReRmR4HyNHXIPG0GnD/IZ1+UUYBX1X7t+Y/bwXnf7bdmFCrUdNs4 njI3B1ORaUjiCSpsrK5rvK9qAaeL+pbcxqiw8D6q9e6CBczyDWxf71kDfYlK guHnlvC0IDdVXLUGhJVN+WX/7IBBQUPSbW0aWH2z/4raw53QXlZdEh9DgziP ++Jrd61Ad+rJQLuYBrdSJDxqmzXkMPMrujlacCMi68CHFhs405qay6nXgm99 p98UPNsFLnpeJSIjOsxJJy+8J3YDRDzypSbToUWdLQpcsoWCtQFGXfN0eBeS NL5oaAeG7aWXXX21gavZttXgOwTpWwWsvhZtmKZOjPWxAIIlik87WQxQvGLp 2SgE8K+e1U9OZ8C+L3aSoUP28Eikeq5piQH3JAIdPak9hO15ndGJ/avn23JT bI2m+oJB7AofDTs29sGKsAYCe8DnfEAotji9a8ZoGe83E+V12IpeGbxU7Euk R3BgyR7YYww7vxUGmOQd56cu43tvgnMEmysSK13HTnnS4XMKm1Z0OO8Odl9b dGQmNtMhsXUE27945OZD7Nc+DpqsFXv8/4V622QMSGwWVD7ENnrzG9MW+6j3 G/YwdgfX3coZezyH/GwSm+Ii5xmIfYIcL9OS2UOhaWBKDrbugw/ugdjs0t25 xdi8TL/xKGzxeq2/qrEtVV5Gp2CbUoVtj7HPvFsqqsbuS77WO4Q953LVSogd JR81Oo696OvYNYStG+s6MYudQJXyPmK3SIw/y7ALwhvmZdj/AzSxStc= "]]}}, AspectRatio->NCache[GoldenRatio^(-1), 0.6180339887498948], Axes->True, AxesOrigin->{1., 0}, PlotRange->{{1, 6}, {0., 35.99999877551021}}, PlotRangeClipping->True, PlotRangePadding->{ Scaled[0.02], Scaled[0.02]}]], "Output", CellChangeTimes->{3.392055481045767*^9}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.45916705675*^9, 3.459167057875*^9}}], Cell[TextData[{ StyleBox["1. Write the function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"newFib", "[", "n", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" that computes the value of the expression ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ FractionBox[ RowBox[{ SuperscriptBox["GoldenRatio", "n"], "-", SuperscriptBox[ RowBox[{"(", RowBox[{"-", "GoldenRatio"}], ")"}], RowBox[{"-", "n"}]]}], SqrtBox["5"]]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Note that ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["newFib"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is defined for any real number ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["n"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Moreover, for non\[Hyphen]integral values of the ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["n"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", the value of the expression ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ SuperscriptBox[ RowBox[{"(", RowBox[{"-", "GoldenRatio"}], ")"}], RowBox[{"-", "n"}]]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is a complex number. ", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.4591699390625*^9, 3.459169940859375*^9}}], Cell[TextData[{ StyleBox["2. Build a table of the values of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"newFib", "[", "n", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" for integral values between ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["1"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["20"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Note that these values have a very small complex part because ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" is using a numerical approximation to ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["GoldenRatio"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" in its calculations. If these calculations could have been done \ exactly, the complex part of these numbers would have been exactly zero. \ Next, create a plot of the value of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["newFib"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" for ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["n"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" between ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["1"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["20."], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" Since the function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Plot"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" expects real numbers as input, use the ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Re"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to compute the real part of the values returned by ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["newFib"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Compare this plot to the discrete plot from the previous \ problem.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3924850984549227`*^9, 3.392485100070683*^9}, { 3.393610802267663*^9, 3.393610802585742*^9}, {3.45916994321875*^9, 3.4591699444375*^9}}] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Strings", "Section", CellChangeTimes->{{3.392055879965295*^9, 3.392055882168551*^9}}], Cell["\<\ To conclude this module, we will attack a slightly more complicated \ programming exercise that is relevant to a real world problem. A string is a \ sequence of characters enclosed by double quotes.\ \>", "Text", CellChangeTimes->{{3.392055569466436*^9, 3.3920556104469976`*^9}, { 3.392055682122921*^9, 3.392055697545713*^9}, {3.3920559045816736`*^9, 3.392055923309349*^9}}], Cell[BoxData["\"\\""], "Input", CellChangeTimes->{{3.3920559251019983`*^9, 3.392055930349754*^9}}], Cell[TextData[{ "The most common use for strings is as input to the ", Cell[BoxData["Print"]], " function" }], "Text", CellChangeTimes->{{3.392055944410533*^9, 3.392055969457548*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Print", "[", RowBox[{"10", ",", " ", "\"\< factorial is \>\"", ",", " ", RowBox[{"10", "!"}]}], "]"}]], "Input", CellChangeTimes->{{3.392055972001307*^9, 3.392055989537222*^9}}], Cell[BoxData[ InterpretationBox[ RowBox[{ "10", "\[InvisibleSpace]", "\<\" factorial is \"\>", "\[InvisibleSpace]", "3628800"}], SequenceForm[10, " factorial is ", 3628800], Editable->False]], "Print", CellChangeTimes->{3.3920559903183765`*^9}] }, Open ]], Cell[TextData[{ "You will not use ", Cell[BoxData["Print"]], " statements except occasionally to help in debugging your ", StyleBox["Mathematica", FontSlant->"Italic"], " functions. Instead, we shall rely on ", StyleBox["Mathematica", FontSlant->"Italic"], " to simply return the answer we desire. Here, we introduce strings to \ frame an interesting, real world problem: cryptography." }], "Text", CellChangeTimes->{{3.3920559992115183`*^9, 3.392056078809149*^9}, { 3.392056178796912*^9, 3.392056242931691*^9}, {3.392485189655324*^9, 3.392485225762377*^9}}], Cell[CellGroupData[{ Cell["Exercise: Cryptography", "Exercise", CellChangeTimes->{{3.459167077890625*^9, 3.4591670790625*^9}}], Cell[TextData[{ StyleBox["Your task is to write two functions ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"encrypt", "[", "s", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ RowBox[{"decrypt", "[", "s", "]"}]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Both functions take as input a string ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["s"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" consisting solely of lower case letters and blanks and returns a \ new string again consisting of these characters. Your task is build these \ functions so that ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["decrypt"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" in the inverse of ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["encrypt"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", i.e., ", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText"], Cell[BoxData[ StyleBox[ RowBox[{ RowBox[{"decrypt", "[", RowBox[{"encrypt", "[", "s", "]"}], "]"}], "\[Equal]", "s"}], FontColor->RGBColor[0.5, 0, 0.5]]], "NumberedEquation"], Cell[TextData[{ StyleBox["for all strings ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["s"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Your basic approach for both functions should be to use ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Characters"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to break a string into a list of characters. You can then use ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Rule"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" (", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["\[Rule]"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[") to replace each character by a new character. The exact \ replacement should be expressed as a list of 27 transformation rules, one for \ each possible character. Finally, the new list of characters is converted \ back to a string using ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["StringJoin"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". ", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3920561550518217`*^9, 3.3920561570647964`*^9}}], Cell[TextData[{ StyleBox["To help in your task, you should review the material on", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ButtonBox["Applying Transformation Rules", BaseStyle->"Link", ButtonData->"paclet:tutorial/ApplyingTransformationRules"], FontColor->RGBColor[0, 0, 1]], StyleBox[" using the function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Replace"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" (", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["/."], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[") and also review the material on strings in", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ButtonBox["Properties of Strings", BaseStyle->"Link", ButtonData->"paclet:tutorial/PropertiesOfStrings"], FontColor->RGBColor[0, 0, 1]], StyleBox[". In addition, you should examine the functions ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Characters"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["StringJoin"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[".", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.392055569466436*^9, 3.3920556104469976`*^9}, { 3.392055682122921*^9, 3.392055697545713*^9}, 3.3920559045816736`*^9, { 3.3920559393330297`*^9, 3.3920559395233107`*^9}, {3.392056115633569*^9, 3.3920561487024384`*^9}, {3.393610922555771*^9, 3.3936109240594063`*^9}}], Cell[TextData[{ StyleBox["Here is one example of a possible encryption and decryption \ function. It uses the special variable ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData[ FormBox[ StyleBox["%", FontFamily->"Courier New", FontSlant->"Plain"], TraditionalForm]], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", which automatically has the value of the previous computation.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.392056134381274*^9, 3.392056140370125*^9}, { 3.459167207484375*^9, 3.459167249109375*^9}, {3.460127539078125*^9, 3.460127605140625*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"encrypt", "[", "\"\\"", "]"}]], "Input"], Cell[BoxData["\<\"dstcktckxikcpnbpdkxpcclrp\"\>"], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"decrypt", "[", "%", "]"}]], "Input"], Cell[BoxData["\<\"this is my secret message\"\>"], "Output"] }, Open ]], Cell[TextData[StyleBox["Can you figure out what my encryption scheme is from \ this example? Your solution does not have to use the same scheme.", FontColor->RGBColor[0.5, 0, 0.5]]], "ExerciseText", CellChangeTimes->{{3.460127606921875*^9, 3.4601276255*^9}}] }, Open ]] }, Open ]] }, WindowSize->{1272, 1046}, WindowMargins->{{-2, Automatic}, {Automatic, 2}}, Magnification->2., FrontEndVersion->"7.0 for Microsoft Windows (32-bit) (November 10, 2008)", StyleDefinitions->"Classroom.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[545, 20, 196, 6, 109, "Title"], Cell[744, 28, 169, 5, 120, "Subtitle"], Cell[916, 35, 1099, 24, 212, "Text"], Cell[CellGroupData[{ Cell[2040, 63, 41, 0, 107, "Section"], Cell[2084, 65, 789, 23, 114, "Text"], Cell[CellGroupData[{ Cell[2898, 92, 179, 4, 201, "Input"], Cell[3080, 98, 28, 0, 91, "Output"], Cell[3111, 100, 46, 0, 91, "Output"], Cell[3160, 102, 45, 0, 91, "Output"] }, Open ]], Cell[3220, 105, 723, 19, 114, "Text"], Cell[CellGroupData[{ Cell[3968, 128, 300, 7, 160, "Input"], Cell[4271, 137, 28, 0, 91, "Output"], Cell[4302, 139, 46, 0, 91, "Output"], Cell[4351, 141, 45, 0, 91, "Output"] }, Open ]], Cell[4411, 144, 656, 19, 82, "Text"], Cell[CellGroupData[{ Cell[5092, 167, 117, 2, 74, "Exercise"], Cell[5212, 171, 1088, 26, 115, "ExerciseText"], Cell[6303, 199, 1271, 31, 181, "Text"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[7623, 236, 43, 0, 107, "Section"], Cell[7669, 238, 1077, 33, 154, "Text"], Cell[8749, 273, 654, 18, 114, "Text"], Cell[CellGroupData[{ Cell[9428, 295, 213, 4, 126, "Input"], Cell[9644, 301, 73, 1, 91, "Output"], Cell[9720, 304, 98, 2, 91, "Output"] }, Open ]], Cell[9833, 309, 190, 8, 48, "Text"], Cell[CellGroupData[{ Cell[10048, 321, 184, 4, 94, "Input"], Cell[10235, 327, 100, 2, 91, "Output"] }, Open ]], Cell[10350, 332, 423, 12, 81, "Text"], Cell[CellGroupData[{ Cell[10798, 348, 50, 1, 91, "Input"], Cell[10851, 351, 71, 1, 91, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[10959, 357, 174, 4, 94, "Input"], Cell[11136, 363, 125, 2, 91, "Output"] }, Open ]], Cell[11276, 368, 643, 18, 146, "Text"], Cell[11922, 388, 891, 34, 148, "Text"], Cell[CellGroupData[{ Cell[12838, 426, 85, 3, 91, "Input"], Cell[12926, 431, 50, 1, 91, "Output"] }, Open ]], Cell[12991, 435, 478, 15, 81, "Text"], Cell[CellGroupData[{ Cell[13494, 454, 140, 2, 74, "Exercise"], Cell[13637, 458, 1314, 37, 116, "ExerciseText"], Cell[14954, 497, 1060, 31, 82, "ExerciseText"], Cell[16017, 530, 546, 14, 82, "ExerciseText"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[16612, 550, 42, 0, 107, "Section"], Cell[CellGroupData[{ Cell[16679, 554, 41, 0, 86, "Subsection"], Cell[16723, 556, 2050, 72, 250, "Text"], Cell[CellGroupData[{ Cell[18798, 632, 140, 2, 74, "Exercise"], Cell[18941, 636, 487, 13, 48, "ExerciseText"], Cell[19431, 651, 1678, 50, 116, "ExerciseText"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[21158, 707, 71, 3, 88, "Subsection"], Cell[21232, 712, 1501, 45, 250, "Text"], Cell[CellGroupData[{ Cell[22758, 761, 339, 10, 160, "Input"], Cell[23100, 773, 28, 0, 91, "Output"], Cell[23131, 775, 28, 0, 91, "Output"], Cell[23162, 777, 89, 2, 91, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[23288, 784, 118, 2, 74, "Exercise"], Cell[23409, 788, 559, 16, 48, "ExerciseText"] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[24029, 811, 53, 0, 107, "Section"], Cell[24085, 813, 846, 22, 180, "Text"], Cell[24934, 837, 1018, 25, 182, "Text"], Cell[25955, 864, 255, 7, 91, "Input"], Cell[26213, 873, 1584, 49, 183, "Text"], Cell[27800, 924, 282, 7, 48, "Text"], Cell[CellGroupData[{ Cell[28107, 935, 266, 7, 160, "Input"], Cell[28376, 944, 28, 0, 186, "Output"], Cell[28407, 946, 28, 0, 186, "Output"], Cell[28438, 948, 28, 0, 186, "Output"] }, Closed]], Cell[28481, 951, 962, 28, 144, "Text"], Cell[29446, 981, 780, 19, 113, "Text"], Cell[30229, 1002, 429, 11, 46, "Text"], Cell[30661, 1015, 431, 15, 48, "Text"], Cell[31095, 1032, 462, 12, 48, "Text"], Cell[31560, 1046, 992, 29, 115, "Text"], Cell[32555, 1077, 448, 8, 46, "Text"], Cell[33006, 1087, 808, 22, 114, "Text"], Cell[33817, 1111, 171, 4, 91, "Input"], Cell[CellGroupData[{ Cell[34013, 1119, 124, 2, 91, "Input"], Cell[34140, 1123, 123, 2, 91, "Output"] }, Open ]], Cell[34278, 1128, 530, 15, 82, "Text"], Cell[CellGroupData[{ Cell[34833, 1147, 120, 2, 91, "Input"], Cell[34956, 1151, 94, 2, 91, "Output"] }, Open ]], Cell[35065, 1156, 551, 16, 115, "Text"], Cell[35619, 1174, 214, 7, 91, "Input"], Cell[CellGroupData[{ Cell[35858, 1185, 188, 5, 126, "Input"], Cell[36049, 1192, 31, 0, 91, "Output"], Cell[36083, 1194, 32, 0, 91, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[36152, 1199, 116, 2, 74, "Exercise"], Cell[36271, 1203, 729, 16, 114, "ExerciseText"], Cell[37003, 1221, 451, 13, 48, "ExerciseText"], Cell[37457, 1236, 676, 19, 48, "ExerciseText"], Cell[38136, 1257, 744, 20, 48, "ExerciseText"], Cell[38883, 1279, 654, 15, 48, "ExerciseText"], Cell[39540, 1296, 918, 27, 81, "ExerciseText"], Cell[CellGroupData[{ Cell[40483, 1327, 193, 4, 126, "Input"], Cell[40679, 1333, 31, 0, 91, "Output"], Cell[40713, 1335, 32, 0, 91, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[40794, 1341, 118, 2, 74, "Exercise"], Cell[40915, 1345, 176, 3, 47, "ExerciseText"], Cell[41094, 1350, 488, 18, 76, "ExerciseText"], Cell[41585, 1370, 1909, 52, 183, "ExerciseText"], Cell[43497, 1424, 1045, 29, 115, "ExerciseText"], Cell[44545, 1455, 617, 16, 81, "ExerciseText"] }, Open ]], Cell[CellGroupData[{ Cell[45199, 1476, 120, 2, 74, "Exercise"], Cell[45322, 1480, 138, 2, 47, "ExerciseText"], Cell[45463, 1484, 626, 23, 108, "ExerciseText"], Cell[46092, 1509, 1365, 37, 149, "ExerciseText"], Cell[47460, 1548, 624, 16, 81, "ExerciseText"], Cell[48087, 1566, 914, 22, 147, "ExerciseText"], Cell[49004, 1590, 896, 26, 82, "ExerciseText"] }, Open ]], Cell[CellGroupData[{ Cell[49937, 1621, 88, 1, 74, "Exercise"], Cell[50028, 1624, 1037, 30, 82, "ExerciseText"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[51114, 1660, 24, 0, 107, "Section"], Cell[51141, 1662, 956, 29, 147, "Text"], Cell[CellGroupData[{ Cell[52122, 1695, 162, 3, 91, "Input"], Cell[52287, 1700, 135, 3, 91, "Output"] }, Open ]], Cell[52437, 1706, 339, 11, 48, "Text"], Cell[CellGroupData[{ Cell[52801, 1721, 102, 1, 86, "Subsection"], Cell[52906, 1724, 482, 10, 80, "Text"], Cell[CellGroupData[{ Cell[53413, 1738, 111, 1, 70, "Subsubsection"], Cell[53527, 1741, 689, 18, 82, "Text"], Cell[CellGroupData[{ Cell[54241, 1763, 181, 3, 91, "Input"], Cell[54425, 1768, 159, 3, 91, "Output"] }, Open ]], Cell[54599, 1774, 544, 14, 80, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[55180, 1793, 145, 2, 74, "Exercise"], Cell[55328, 1797, 658, 19, 82, "ExerciseText"] }, Open ]], Cell[CellGroupData[{ Cell[56023, 1821, 102, 1, 70, "Subsubsection"], Cell[56128, 1824, 579, 11, 81, "Text"], Cell[56710, 1837, 867, 23, 48, "Text"], Cell[57580, 1862, 213, 7, 48, "Text"], Cell[57796, 1871, 446, 12, 91, "Input"], Cell[CellGroupData[{ Cell[58267, 1887, 129, 2, 91, "Input"], Cell[58399, 1891, 188, 4, 91, "Output"] }, Open ]], Cell[58602, 1898, 916, 27, 150, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[59555, 1930, 147, 2, 74, "Exercise"], Cell[59705, 1934, 696, 20, 81, "ExerciseText"] }, Open ]], Cell[CellGroupData[{ Cell[60438, 1959, 102, 1, 70, "Subsubsection"], Cell[60543, 1962, 514, 14, 82, "Text"], Cell[61060, 1978, 370, 9, 91, "Input"], Cell[CellGroupData[{ Cell[61455, 1991, 131, 2, 91, "Input"], Cell[61589, 1995, 137, 3, 91, "Output"] }, Open ]], Cell[61741, 2001, 1488, 43, 218, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[63266, 2049, 147, 2, 74, "Exercise"], Cell[63416, 2053, 438, 10, 48, "ExerciseText"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[63903, 2069, 110, 1, 86, "Subsection"], Cell[64016, 2072, 896, 23, 115, "Text"], Cell[CellGroupData[{ Cell[64937, 2099, 579, 16, 194, "Input"], Cell[65519, 2117, 94, 1, 91, "Output"], Cell[65616, 2120, 94, 1, 91, "Output"], Cell[65713, 2123, 148, 3, 91, "Output"], Cell[65864, 2128, 148, 3, 91, "Output"] }, Open ]], Cell[66027, 2134, 208, 5, 47, "Text"], Cell[66238, 2141, 1082, 33, 116, "Text"], Cell[CellGroupData[{ Cell[67345, 2178, 425, 10, 194, "Input"], Cell[67773, 2190, 161, 3, 91, "Output"], Cell[67937, 2195, 98, 1, 91, "Output"], Cell[68038, 2198, 96, 1, 91, "Output"], Cell[68137, 2201, 96, 1, 91, "Output"] }, Open ]], Cell[68248, 2205, 631, 14, 115, "Text"], Cell[CellGroupData[{ Cell[68904, 2223, 263, 5, 126, "Input"], Cell[69170, 2230, 73, 1, 91, "Output"], Cell[69246, 2233, 401, 7, 43, "Message"], Cell[69650, 2242, 206, 5, 91, "Output"] }, Open ]], Cell[69871, 2250, 608, 18, 81, "Text"], Cell[CellGroupData[{ Cell[70504, 2272, 119, 2, 74, "Exercise"], Cell[70626, 2276, 664, 17, 81, "ExerciseText"], Cell[71293, 2295, 792, 20, 82, "ExerciseText"], Cell[72088, 2317, 986, 25, 115, "ExerciseText"], Cell[73077, 2344, 328, 8, 91, "Input"], Cell[73408, 2354, 504, 15, 48, "ExerciseText"], Cell[73915, 2371, 1928, 50, 184, "ExerciseText"] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[75904, 2428, 33, 0, 107, "Section"], Cell[75940, 2430, 493, 9, 113, "Text"], Cell[CellGroupData[{ Cell[76458, 2443, 55, 0, 86, "Subsection"], Cell[76516, 2445, 728, 23, 82, "Text"], Cell[CellGroupData[{ Cell[77269, 2472, 154, 4, 91, "Input"], Cell[77426, 2478, 424, 13, 522, "Output"] }, Open ]], Cell[77865, 2494, 75, 0, 47, "Text"], Cell[CellGroupData[{ Cell[77965, 2498, 218, 6, 91, "Input"], Cell[78186, 2506, 437, 12, 522, "Output"] }, Open ]], Cell[78638, 2521, 612, 19, 115, "Text"], Cell[CellGroupData[{ Cell[79275, 2544, 119, 2, 74, "Exercise"], Cell[79397, 2548, 807, 23, 81, "ExerciseText"], Cell[80207, 2573, 2134, 63, 204, "ExerciseText"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[82390, 2642, 51, 0, 86, "Subsection"], Cell[82444, 2644, 1465, 48, 184, "Text"], Cell[CellGroupData[{ Cell[83934, 2696, 164, 5, 98, "Input"], Cell[84101, 2703, 2030, 39, 522, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[86168, 2747, 87, 1, 74, "Exercise"], Cell[86258, 2750, 1346, 41, 148, "ExerciseText"], Cell[87607, 2793, 2330, 64, 217, "ExerciseText"] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[89998, 2864, 92, 1, 107, "Section"], Cell[90093, 2867, 391, 7, 80, "Text"], Cell[90487, 2876, 113, 1, 91, "Input"], Cell[90603, 2879, 188, 5, 48, "Text"], Cell[CellGroupData[{ Cell[90816, 2888, 209, 4, 91, "Input"], Cell[91028, 2894, 258, 7, 43, "Print"] }, Open ]], Cell[91301, 2904, 577, 14, 114, "Text"], Cell[CellGroupData[{ Cell[91903, 2922, 107, 1, 74, "Exercise"], Cell[92013, 2925, 977, 27, 115, "ExerciseText"], Cell[92993, 2954, 187, 5, 51, "NumberedEquation"], Cell[93183, 2961, 1108, 27, 149, "ExerciseText"], Cell[94294, 2990, 1535, 39, 115, "ExerciseText"], Cell[95832, 3031, 621, 15, 81, "ExerciseText"], Cell[CellGroupData[{ Cell[96478, 3050, 92, 1, 91, "Input"], Cell[96573, 3053, 60, 0, 91, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[96670, 3058, 60, 1, 91, "Input"], Cell[96733, 3061, 60, 0, 91, "Output"] }, Open ]], Cell[96808, 3064, 261, 3, 80, "ExerciseText"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)