(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 62618, 2240]*) (*NotebookOutlinePosition[ 63703, 2275]*) (* CellTagsIndexPosition[ 63659, 2271]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["3. Defining Variables and Functions", "Title", Evaluatable->False, AspectRatioFixed->True], Cell["Last revision: January 15 1998", "SmallText", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ In this section we cover various ways to store a value or an \ expression in a variable (a place inside the computer) as well as ways to \ store a list of instructions inside a function.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Variable Declaration and Initialization: Set", "Section", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "We have already learned how to give a value a name: we decide on a \ consecutive sequence of letters (the \"name\"), follow it by a single equals \ sign, and then place the value on the right. Let's consider this operation \ in a little more detail. When we give Mathematica an instruction such as \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess = 7", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", Mathematica does several things. First, Mathematica", Evaluatable->False, AspectRatioFixed->True], StyleBox[" evaluates ", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ "the right-hand side of the expression (yielding an Integer, 7). Then \ Mathematica", Evaluatable->False, AspectRatioFixed->True], StyleBox[" declares", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " a new variable (by which we mean that Mathematica will remember from now \ on that a certain name is significant), named \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". Then Mathematica ", Evaluatable->False, AspectRatioFixed->True], StyleBox["assigns ", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox["this variable an initial value, the Integer 7. Finally, the ", Evaluatable->False, AspectRatioFixed->True], StyleBox["result", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " of our operation is whatever value was assigned, in this case 7, so \ Mathematica gives us a 7 back (in our Output cell). From now on (unless we \ change \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\"), any time we use \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", Mathematica will use the Integer 7. We have made, then, a ", Evaluatable->False, AspectRatioFixed->True], StyleBox["global", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" declaration and have", Evaluatable->False, AspectRatioFixed->True], StyleBox[" initialized", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " the variable to the value 7. The word \"global\" refers to the fact that \ ", Evaluatable->False, AspectRatioFixed->True], StyleBox["wherever", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " we use the name now, it will always refer to the variable we have \ created; the word \"initialized\" indicates that, upon creation, it was first \ assigned a particular value, in this case the Integer 7.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Let's consider a few examples.", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["a = 20.71", "Input", AspectRatioFixed->True], Cell["b = a^2+a-1", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Notice here that the value of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["b", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is not saved as \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["a^2+a-1", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", but as a ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Real", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ " value of around 448.614. Remember, the right-hand side is evaluated \ first, and then the new value is assigned to the variable. ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["If we change the value of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["a", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", the variable \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["b", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" will not change, as follows:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["a = 19", "Input", AspectRatioFixed->True], Cell["b", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "We can ask Mathematica what it knows about a variable, or, for that \ matter, any \"name\". Let's ask what it knows about \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["b", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\":", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["?b", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["What Mathematica is telling us is that \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["b", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is globally declared, and its latest value is 448.6141.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Functional Forms", "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "In Mathematica, there are usually several ways to accomplish any \ operation. For the value assignment operation we have performed above, we \ have an alternative ", Evaluatable->False, AspectRatioFixed->True], StyleBox["functional", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" form, ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Set[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ ". (Functional form refers to using a name with brackets to complete an \ operation, rather than using a special symbol, like the equals sign.) \ Sometimes functions have expressions placed in between the brackets, and we \ call them ", Evaluatable->False, AspectRatioFixed->True], StyleBox["arguments", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[", or", Evaluatable->False, AspectRatioFixed->True], StyleBox[" parameters", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ ". If the number of arguments is greater than 1, then the arguments are \ separated by commas (as in \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["N[Pi,50]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\"). For our function ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Set[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ ", the first argument is the name we want to declare and initialize. The \ second argument is the value to which we want to initialize the name. In \ short, then, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess = 7", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" can be accomplished in functional form by evaluating \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Set[mess,7]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\". Remember that just as with the assignment operation using the equals \ sign, this second argument will be evaluated first before the assignment \ happens. ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Set[a,20.71]", "Input", AspectRatioFixed->True], Cell["Finally, we consider one more example.", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["bigmess = Expand[(x+2)^10]", "Input", AspectRatioFixed->True], Cell["bigmess", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "All of the operators which we have seen have special functional forms. \ Here is a list of the operators, together with their functional forms:\n\n", Evaluatable->False, AspectRatioFixed->True], StyleBox[ "a>b Greater[a,b]\na>=b GreaterEqual[a,b]\naFalse, AspectRatioFixed->True, FontFamily->"Courier"] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Variable (Re)Assignment after Initialization", "Section", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Once a variable, or any name, is declared and initialized, it may \ be redefined or reassigned. Once we do so, Mathematica essentially forgets \ the first value, and from that point forward until another redefinition, the \ name will refer to the new value.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["mess = 9", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["If we execute the statement above, then \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" will then refer to the value 9, of Integer type. Although this is the \ desired behavior, here is a caveat: if we return to an earlier cell in our \ notebook and reevaluate each cell, we may get different answers than we did \ before. For instance, if earlier cells included the instructions \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess=8", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["bigmess = mess^10", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\", the first time we evaluate them we would find that bigmess is ", Evaluatable->False, AspectRatioFixed->True], StyleBox["8^10", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[". However, if we execute \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess = 9", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" and ", Evaluatable->False, AspectRatioFixed->True], StyleBox["go back", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" to and reevaluate the second of the first two cells (\"", Evaluatable->False, AspectRatioFixed->True], StyleBox["bigmess = mess^10", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\"), then we will find that ", Evaluatable->False, AspectRatioFixed->True], StyleBox["bigmess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" has changed to ", Evaluatable->False, AspectRatioFixed->True], StyleBox["9^10", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[".", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Modifying a Variable's Value with Arithmetic Operations", "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "Frequently we will want to take a variable's value, do something simple to \ it, such as add 3 to it or multiply it by 4, and put the new value back into \ the variable. We are, then, reassigning the value, based on the old value. \ We can accomplish these operations using the equals sign just as before, \ remembering that the right-hand side is evaluated ", Evaluatable->False, AspectRatioFixed->True], StyleBox["before", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" the assignment is made. Consider the following cell:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ mess = 10; mess = mess + 10\ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "(Note that the semicolon suppresses the output of the line on which it \ occurs and separates the two lines.) What happens in the second line is that \ \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess + 10", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" is evaluated to the Integer 20, and then this is reassigned to \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". For the mathematically inclined, the statement \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x=x+10", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" will seem an inconsistency, but for (we) computer programmers it is \ crucial to remember that the equals sign invokes an action (assignment) and \ does not claim an equality. (Recall that the double equals sign \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["==", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is the equality test.)", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Here are some more examples of arithmetic value modification:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["mess = mess * 10", "Input", AspectRatioFixed->True], Cell["mess = mess / 20", "Input", AspectRatioFixed->True], Cell["mess = 2 + 10 / 5 + mess / 2", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "There are other assignment operators which we will use from time to time, \ and these operators are essentially shorthand for longer expressions. They \ are used when we want to take and old value of a variable, perform one \ arithmetic operation to it, and replace the value. The operator \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["+=", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" adds the (evaluated) right-hand side to the variable on the left and \ reassigns it. As an example:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ mess = 8; mess += 1\ \>", "Input", AspectRatioFixed->True], Cell["Other examples: ", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["mess -= 1", "Input", AspectRatioFixed->True], Cell["mess *= (2+10)", "Input", AspectRatioFixed->True], Cell["mess /= 4", "Input", AspectRatioFixed->True], Cell["\<\ Of course, if we wish to assign a value to a variable based on \ other variables, then these shorthand operations will not help. For \ instance, there is no shorthand for the following expression.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["mess2 = mess * 2", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "For completeness' sake, here are the functional forms of these operators:\n\ \n", Evaluatable->False, AspectRatioFixed->True], StyleBox[ "a+=b AddTo[a,b]\na-=b SubtractFrom[a,b]\na*=b MultiplyBy[a,b]\n\ a/=b DivideBy[a,b]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell["Increment, Decrement, PreIncrement, PreDecrement", "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "One very common way in which computer programmers change values of \ variables is to add 1 or subtract 1 from a variable; this is useful in loops, \ where we do a certain operation (say) 10 times. In that instance we will \ need to initialize a variable to 1, add 1 each time we do the operation, and \ continue doing so until the variable reaches 10. Adding one to a variable is \ called ", Evaluatable->False, AspectRatioFixed->True], StyleBox["incrementing", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" the variable, while subtracting one is called ", Evaluatable->False, AspectRatioFixed->True], StyleBox["decrementing ", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " the variable. There are some useful shorthand expressions for \ incrementing and decrementing, namely \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["++", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["--", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\". You can use either one by placing it either immediately before or \ immediately after the name of the variable, as for instance \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["i++", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["i--", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["++i", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["--i", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". As you might expect, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["++", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" adds one (the Integer 1) to the variable, while \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["--", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" takes 1 away. ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["There is one subtle difference between placing a \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["++", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" or \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["--", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" expression before and placing it after the name: ", Evaluatable->False, AspectRatioFixed->True], StyleBox["when", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" the incrementing or decrementing happens. If you put the \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["++", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" or \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["--", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" ", Evaluatable->False, AspectRatioFixed->True], StyleBox["before", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" the variable, then the incrementing or decrementing happens ", Evaluatable->False, AspectRatioFixed->True], StyleBox["before", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" the value is used in the current expression; if you place it ", Evaluatable->False, AspectRatioFixed->True], StyleBox["after", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[", then the incrementing or decrementing happens ", Evaluatable->False, AspectRatioFixed->True], StyleBox["afterwards", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[". ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Let's look at some examples. Suppose \"mess\" starts out as 8:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["mess = 8", "Input", AspectRatioFixed->True], Cell["Now let's add one to it and find the new value:", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["++mess", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["What happened is that we asked for the value of ", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[", but the \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["++", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" had to happen before the value was used in the evaluation of the line, \ so \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" was incremented to 9 and then returned to us. Now let's ask for the \ value of ", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" with a increment afterwards:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["mess++", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["We still get 9, but \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" is really now 10! To convince ourselves of this, let's find out what \ mess is:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["mess", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["What happened? When we executed \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess++", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", the expression's value was that of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", which was 9, and ", Evaluatable->False, AspectRatioFixed->True], StyleBox["after", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" the evaluation happened (and we were returned the value 9), ", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" was incremented. Then we asked for the value of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", and the expression \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" evaluated to its new value, namely, 10.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "Functional forms for the increment and decrement operators are as follows:\ \n\n", Evaluatable->False, AspectRatioFixed->True], StyleBox[ "a++ Increment[a]\na-- Decrement[a]\n++a PreIncrement[a]\n--a \ PreIncrement[a]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "We can always find the functional form for an expression by using the \ function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["FullForm[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". For instance, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["FullForm[aFalse, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" will return \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Less[a,b]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\".", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Deassignment, Undeclaration", "Section", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "What if we want to get rid of a variable's value? For instance, what if \ we want to have a polynomial \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" be the expression \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x^2+x+1", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" has already been declared and initialized to the Integer 5? Then if we \ set \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" to be \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x^2+x+1", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" then we will get 31, not \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x^2+x+1", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\"! One solution is to tell Mathematica to forget about the value of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\". There are several ways to accomplish this, one of which is to use the \ function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Clear[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". The function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Clear[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" erases the value of each name appearing as an argument to it. For \ example:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["mess = 9", "Input", AspectRatioFixed->True], Cell["Clear[mess]", "Input", AspectRatioFixed->True], Cell["mess", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["(A shorthand for ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Clear[mess]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" is \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["mess =.", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" Yes, include the period right after the equals. Actually, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["=.", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is a shorthand for the function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Unset[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", which is somewhat different from \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Clear[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\", but we will ignore the differences for now. The most powerful of \ Mathematica's undeclaration functions is \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Remove[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". When \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Clear[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" erases a value, Mathematica still remembers that the name has been used \ somewhere (so that it is still ", Evaluatable->False, AspectRatioFixed->True], StyleBox["declared", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox["); \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Remove[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" instructs Mathematica to forget that it ever saw the name.)", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell["Function Definition: SetDelayed", "Section", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["Let's think again about how we defined \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["b", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" above. What if, instead of computing the value of ", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(TraditionalForm\`a\^2 + a - 1\)]], " ", StyleBox["with the value of ", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(TraditionalForm\`a\)]], StyleBox[ " which is currently defined, we wish to have Mathematica compute the value \ of the function ", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(TraditionalForm\`x\^2 + x - 1\)]], StyleBox[" for ", Evaluatable->False, AspectRatioFixed->True], StyleBox["any", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" given value of ", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(TraditionalForm\`x\)]], StyleBox["? The best way to do this in Mathematica is to ", Evaluatable->False, AspectRatioFixed->True], StyleBox["define a function", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[". A", Evaluatable->False, AspectRatioFixed->True], StyleBox[" function", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " typically has several arguments or parameters, and the function operates \ by taking the values it is given for those arguments and using them in some \ expression or sequence of operations. When we define a function, we must \ give Mathematica a name for each of these parameters, but we must distinguish \ these names from the", Evaluatable->False, AspectRatioFixed->True], StyleBox[" global", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " names which we might already have declared. Here's how to do it: First, \ on the left-hand side of the function definition, when names for each \ argument are given between the brackets (separated by commas, as usual), \ attach an underscore (\"", Evaluatable->False, AspectRatioFixed->True], StyleBox["_", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\") immediately following each one. Second, instead of using \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["=", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", use colon-equals (\"", Evaluatable->False, AspectRatioFixed->True], StyleBox[":=", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", the ", Evaluatable->False, AspectRatioFixed->True], StyleBox["delayed assignment operator ", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox["). For example:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[f] f[x_] := x^2 + x - 1 \ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "This line tells Mathematica several things. First, it must declare a new \ name, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\"; second, this instance of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" may take exactly one argument or parameter; third, if \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" is evaluated with an expression in between the brackets, that \ expression will (temporarily) be called \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\"; finally, the function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" will take this new \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" and use it in the expression on the right-hand side to provide an \ answer. An important fact to remember is that, unlike the scenario with the \ assignment operator \"=\", the right-hand side is ", Evaluatable->False, AspectRatioFixed->True], StyleBox["not ", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox["evaluated at all. Let's use \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\":", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["f[2]", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["There are some terms appropriate here. A ", Evaluatable->False, AspectRatioFixed->True], StyleBox["formal parameter ", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ "is a name assigned inside a function declaration to be used for one of the \ arguments of the function. Formal parameters in Mathematica are distinct \ from global names. In the case above, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is a formal parameter. An ", Evaluatable->False, AspectRatioFixed->True], StyleBox["actual parameter", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ ", on the other hand, is a value with which the function is actually \ evaluated. In the case above, the \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["2", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" in \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f[2]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" was an actual parameter. Actual parameters need not be Integers, of \ course; we can call \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" with anything we like inside the brackets:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["f[y]\t(* y is called an actual parameter *)", "Input", AspectRatioFixed->True], Cell["f[a]\t(* a is called an actual parameter *)", "Input", AspectRatioFixed->True], Cell["f[7]\t(* 7 is called an actual parameter *)", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Even more, we can use \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" as an actual parameter, and Mathematica will realize that it is \ distinct from the formal parameter \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\"! ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["f[x]\t(* here, the x is an actual parameter *)", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["As you may have noticed, using \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["(*", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["*)", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" around some text in an input cell allows you to compose a comment that \ Mathematica will ignore. When our code gets especially complicated, we will \ comment more often.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["The functional form of the colon-equals operator is ", Evaluatable->False, AspectRatioFixed->True], StyleBox["SetDelayed[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[". The following two input cells are then equivalent:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[myFunction] myFunction[x_] := Sin[x] + Cos[x]\ \>", "Input", AspectRatioFixed->True], Cell["SetDelayed [myFunction[x_], Sin[x] + Cos[x] ]", "Input", AspectRatioFixed->True], Cell["\<\ In order to reinforce the need for underscores and the colon-equals \ sign symbol, let's look at what can happen when you don't do one or the other \ of our requirements. \ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Leaving Out the Underscore", "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "If we leave out the underscore, Mathematica no longer sees the argument as \ a formal parameter into which any expression can be substituted. Instead, it \ simply remembers the name of the function and decides that if the parameter \ is ever ", Evaluatable->False, AspectRatioFixed->True], StyleBox["exactly ", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " the one we specify, the function will return whatever right-hand side we \ wrote down. For instance:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Clear[bad]", "Input", AspectRatioFixed->True], Cell["bad[x] := x^2 + x - 1", "Input", AspectRatioFixed->True], Cell["bad[x]", "Input", AspectRatioFixed->True], Cell["bad[y]", "Input", AspectRatioFixed->True], Cell["bad[7]", "Input", AspectRatioFixed->True], Cell["\<\ This is clearly not the behavior we want in a general \ function.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell["Using = Instead of := (Set Instead of SetDelayed)", "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ If we use equals instead of colon-equals, Mathematica will \ immediately evaluate the right-hand side of the equals sign and then assign \ the resulting expression to our function. While this sometimes produces the \ desired outcome, it can be a dangerous operation. Consider the following \ sequence of steps:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ x = 2 h[x_] = x^2+x+1\ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Mathematica has declared a function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["h", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" which takes one formal parameter \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", but the expression the function will evaluate with \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is not \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x^2+x+1", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" but the Integer 7! This example explains the meaning of the functional \ name \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["SetDelayed", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\": Mathematica ", Evaluatable->False, AspectRatioFixed->True], StyleBox["delays", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " evaluating the right-hand side until after the function is used; it \ does not evaluate the right-hand side when the function is defined. Let's \ convince ourselves that \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["h", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" has an undesirable definition:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["h[1]", "Input", AspectRatioFixed->True], Cell["h[2]", "Input", AspectRatioFixed->True], Cell["h[y]", "Input", AspectRatioFixed->True], Cell["?h", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "It is certainly true that in our example above, evaluating the command \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Remove[x]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" before defining the function using the equals sign would have made \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["h", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" a well-defined function. However, there are situations in which \ deciding whether the equals sign will work can be quite delicate, and we hope \ that the first impulse will be to use colon-equals.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell["The Naming of Functions", "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "One good programming principle is name the functions we create differently \ from functions which are basic to the programming language itself. For \ instance, we probably shouldn't try to define our own function called \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Plot[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\". Thankfully, Mathematica prevents us from doing so (by giving all of \ its own functions the \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Protected", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" attribute, as we will see later). Still, we should distinguish our own \ functions from those of Mathematica by using a different convention in \ function naming: we shouldn't capitalize the first letter of our function \ name. We are free, however, to capitalize the first letters of successive \ words in the function name, in order to make their names more readable. Some \ examples of good names for user-defined functions are \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["myFunction", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["plotMyFunction", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["takeMyDerivative", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["initializeMyVariables", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\".", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "One caveat is that, in Mathematica, functions can have the same names as \ variables, and if this happens, confusion erupts. For instance, if we define \ \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f[x_] := x^2", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" and then evaluate \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["f = x + 2", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", Mathematica will try to remember both. Let's try it:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[func,x] func[x_] := x^2 func = x + 2\ \>", "Input", AspectRatioFixed->True], Cell["?func", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Hence, to refer to the", Evaluatable->False, AspectRatioFixed->True], StyleBox[" function ", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ "defined above, it will be necessary to use brackets and put some \ expression between the brackets. To plot the function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["func", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", then, we must specify \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["func[x]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", not \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["func", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", but if \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["func", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" is already defined separately as a variable, then Mathematica will make \ a mistake and try unsuccessfully to evaluate the expression \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x+2[x]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". Notice that in the plot below we get a plot of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x+2", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", not \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x^2", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\":", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Plot[func,{x,-1,1}]", "Input", AspectRatioFixed->True], Cell["\<\ The solution is never to have two expressions with the same name, \ and always to refer to a function using brackets.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Using Set for Function Definition (Beware.)", "Section", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "You may wish to skip this section for a while, maybe even for a week or \ two. If you're unsure, just go ahead an close the cell group! The purpose \ of this section is to show you that you may in fact ", Evaluatable->False, AspectRatioFixed->True], StyleBox["want", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" to use the assignment operator \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["=", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" to define a function. We'll give two examples, one where to do so \ would make the situation easier, another where it would be a mistake.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Example 1: Where = is good", "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["First, we introduce the derivative function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["D[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\", which differentiates the first argument with respect to the variable \ stated in the second argument. (For the purposes of this example, we need \ only know that the derivative function applied to a polynomial returns \ another polynomial.) Let's try to define a function finds the derivative of \ a certain polynomial (let's say ", Evaluatable->False, AspectRatioFixed->True], StyleBox["x^2+x-1", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[", for simplicity) and then substitutes a particular value for ", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ " inside the new polynomial. The parameter of our function, then, will be \ this particular value. We might write", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[g] g[x_] := D[-1+x+x^2, x]\ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "But this would be a terrible mistake. Why? Because when we ask for, say, \ \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["g[3]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" the value 3 would be substituted into the entire right-hand side ", Evaluatable->False, AspectRatioFixed->True], StyleBox["everywhere", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" there is an \"x\", and so Mathematica will try to evaluate \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["D[11,3]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\", which, first, doesn't make sense (since the second argument is not a \ variable) and, second, gives us an error. What we want is to find the \ derivative (with respect to \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\") first, and then evaluate at the value of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". We might try the following:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[g] deriv = D[-1+x+x^2, x]; g[x_] := deriv\ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Here we simply find the derivative, assign it to the name \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["deriv", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", and have \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["g", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" defined to use \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["deriv", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" as its right-hand side. But this doesn't work either, since \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["g", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is not defined as \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["1+2x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" (the derivative of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["-1+x+x^2", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\"), but simply as \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["deriv", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". We ask, why wasn't \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["1+2x", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" substituted for \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["deriv", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" in the second line? Because ", Evaluatable->False, AspectRatioFixed->True], StyleBox["SetDelayed", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" (or \"", Evaluatable->False, AspectRatioFixed->True], StyleBox[":=", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\") doesn't evaluate the right-hand side before it defines the function. \ To check,", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["?g", "Input", AspectRatioFixed->True], Cell["\<\ Now we are upset. We can force Mathematica to evaluate \"deriv\" \ before it defines the function. We can do so as follows: \ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[g] deriv = D[-1+x+x^2,x]; g[x_] := Evaluate[deriv]\ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "To do this, however, is basically to walk a long way around the simpler \ expression, using \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["=", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", which does basically the same thing:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[g] g[x_] = D[-1+x+x^2,x]\ \>", "Input", AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell["Example 2: Where = is bad", "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["In this example, we use the \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Random[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" function, which finds a random value of the type specified by the first \ argument, in an interval specified as \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["{begin, end}", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" in the second argument. (For instance, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Random[Real,{0,1}]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" produces a random approximate number between 0 and 1. See \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["?Random", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" for more information.) We wish to define a function taking one \ argument (say \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["n", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\") which returns a random integer from 1 to the number we specify (\"", Evaluatable->False, AspectRatioFixed->True], StyleBox["n", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\"). If we try defining the function with the assignment operator \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["=", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" we certainly have a problem, since Mathematica tries to evaluate the \ right-hand side before assigning the definition to the name of our function, \ and at that point Mathematica doesn't know a value for \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["n", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\":", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[rand,n]; rand[n_] = Random[Integer,{1,n}]\ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["But if we use the delayed assignment operator \"", Evaluatable->False, AspectRatioFixed->True], StyleBox[":=", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", all works out right.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Clear[rand]; rand[n_] := Random[Integer,{1,n}]\ \>", "Input", AspectRatioFixed->True], Cell["rand[100]", "Input", AspectRatioFixed->True], Cell["rand[50]", "Input", AspectRatioFixed->True] }, Closed]] }, Closed]] }, Open ]] }, FrontEndVersion->"Macintosh 3.0", ScreenRectangle->{{0, 832}, {0, 604}}, WindowToolbars->{}, WindowSize->{520, 508}, WindowMargins->{{52, Automatic}, {Automatic, 17}}, PrintingCopies->1, PrintingPageRange->{1, Automatic}, PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}}, ShowCellLabel->True, ShowCellTags->False, RenderingOptions->{"ObjectDithering"->True, "RasterDithering"->False}, MacintoshSystemPageSetup->"\<\ 00<0004/0B`000003809T?o>old" ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1731, 51, 100, 2, 136, "Title", Evaluatable->False], Cell[1834, 55, 100, 2, 26, "SmallText", Evaluatable->False], Cell[1937, 59, 258, 6, 46, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[2220, 69, 111, 2, 50, "Section", Evaluatable->False], Cell[2334, 73, 3217, 100, 233, "Text", Evaluatable->False], Cell[5554, 175, 94, 2, 30, "Text", Evaluatable->False], Cell[5651, 179, 52, 1, 22, "Input"], Cell[5706, 182, 54, 1, 22, "Input"], Cell[5763, 185, 846, 29, 38, "Text", Evaluatable->False], Cell[6612, 216, 548, 20, 25, "Text", Evaluatable->False], Cell[7163, 238, 49, 1, 22, "Input"], Cell[7215, 241, 44, 1, 22, "Input"], Cell[7262, 244, 437, 15, 25, "Text", Evaluatable->False], Cell[7702, 261, 45, 1, 22, "Input"], Cell[7750, 264, 405, 13, 25, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[8180, 281, 86, 2, 42, "Subsection", Evaluatable->False], Cell[8269, 285, 2441, 77, 116, "Text", Evaluatable->False], Cell[10713, 364, 55, 1, 22, "Input"], Cell[10771, 367, 102, 2, 25, "Text", Evaluatable->False], Cell[10876, 371, 69, 1, 22, "Input"], Cell[10948, 374, 50, 1, 22, "Input"], Cell[11001, 377, 570, 15, 168, "Text", Evaluatable->False] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[11620, 398, 111, 2, 30, "Section", Evaluatable->False], Cell[11734, 402, 330, 7, 38, "Text", Evaluatable->False], Cell[12067, 411, 51, 1, 22, "Input"], Cell[12121, 414, 2241, 74, 90, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[14387, 492, 125, 2, 42, "Subsection", Evaluatable->False], Cell[14515, 496, 727, 18, 64, "Text", Evaluatable->False], Cell[15245, 516, 78, 4, 30, "Input"], Cell[15326, 522, 1344, 41, 77, "Text", Evaluatable->False], Cell[16673, 565, 136, 5, 25, "Text", Evaluatable->False], Cell[16812, 572, 59, 1, 22, "Input"], Cell[16874, 575, 59, 1, 22, "Input"], Cell[16936, 578, 71, 1, 22, "Input"], Cell[17010, 581, 711, 19, 64, "Text", Evaluatable->False], Cell[17724, 602, 70, 4, 30, "Input"], Cell[17797, 608, 81, 2, 25, "Text", Evaluatable->False], Cell[17881, 612, 52, 1, 22, "Input"], Cell[17936, 615, 57, 1, 22, "Input"], Cell[17996, 618, 52, 1, 22, "Input"], Cell[18051, 621, 269, 6, 38, "Text", Evaluatable->False], Cell[18323, 629, 59, 1, 22, "Input"], Cell[18385, 632, 417, 14, 90, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[18839, 651, 118, 2, 42, "Subsection", Evaluatable->False], Cell[18960, 655, 2629, 86, 103, "Text", Evaluatable->False], Cell[21592, 743, 1999, 69, 64, "Text", Evaluatable->False], Cell[23594, 814, 126, 2, 25, "Text", Evaluatable->False], Cell[23723, 818, 51, 1, 22, "Input"], Cell[23777, 821, 111, 2, 25, "Text", Evaluatable->False], Cell[23891, 825, 49, 1, 22, "Input"], Cell[23943, 828, 1067, 38, 51, "Text", Evaluatable->False], Cell[25013, 868, 49, 1, 22, "Input"], Cell[25065, 871, 417, 15, 25, "Text", Evaluatable->False], Cell[25485, 888, 47, 1, 22, "Input"], Cell[25535, 891, 1422, 48, 51, "Text", Evaluatable->False], Cell[26960, 941, 413, 14, 90, "Text", Evaluatable->False], Cell[27376, 957, 790, 29, 38, "Text", Evaluatable->False] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[28215, 992, 94, 2, 30, "Section", Evaluatable->False], Cell[28312, 996, 2185, 78, 77, "Text", Evaluatable->False], Cell[30500, 1076, 51, 1, 22, "Input"], Cell[30554, 1079, 54, 1, 22, "Input"], Cell[30611, 1082, 47, 1, 22, "Input"], Cell[30661, 1085, 2146, 74, 77, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[32844, 1164, 98, 2, 30, "Section", Evaluatable->False], Cell[32945, 1168, 3137, 101, 129, "Text", Evaluatable->False], Cell[36085, 1271, 90, 4, 30, "Input"], Cell[36178, 1277, 2033, 69, 77, "Text", Evaluatable->False], Cell[38214, 1348, 47, 1, 22, "Input"], Cell[38264, 1351, 1687, 55, 77, "Text", Evaluatable->False], Cell[39954, 1408, 86, 1, 22, "Input"], Cell[40043, 1411, 86, 1, 22, "Input"], Cell[40132, 1414, 86, 1, 22, "Input"], Cell[40221, 1417, 608, 22, 38, "Text", Evaluatable->False], Cell[40832, 1441, 89, 1, 22, "Input"], Cell[40924, 1444, 691, 23, 38, "Text", Evaluatable->False], Cell[41618, 1469, 422, 13, 25, "Text", Evaluatable->False], Cell[42043, 1484, 102, 4, 30, "Input"], Cell[42148, 1490, 88, 1, 22, "Input"], Cell[42239, 1493, 244, 6, 38, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[42508, 1503, 96, 2, 42, "Subsection", Evaluatable->False], Cell[42607, 1507, 662, 19, 51, "Text", Evaluatable->False], Cell[43272, 1528, 53, 1, 22, "Input"], Cell[43328, 1531, 64, 1, 22, "Input"], Cell[43395, 1534, 49, 1, 22, "Input"], Cell[43447, 1537, 49, 1, 22, "Input"], Cell[43499, 1540, 49, 1, 22, "Input"], Cell[43551, 1543, 137, 5, 25, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[43725, 1553, 119, 2, 42, "Subsection", Evaluatable->False], Cell[43847, 1557, 384, 8, 51, "Text", Evaluatable->False], Cell[44234, 1567, 72, 4, 30, "Input"], Cell[44309, 1573, 1769, 60, 64, "Text", Evaluatable->False], Cell[46081, 1635, 47, 1, 22, "Input"], Cell[46131, 1638, 47, 1, 22, "Input"], Cell[46181, 1641, 47, 1, 22, "Input"], Cell[46231, 1644, 45, 1, 22, "Input"], Cell[46279, 1647, 837, 26, 51, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[47153, 1678, 93, 2, 42, "Subsection", Evaluatable->False], Cell[47249, 1682, 1987, 59, 116, "Text", Evaluatable->False], Cell[49239, 1743, 729, 23, 51, "Text", Evaluatable->False], Cell[49971, 1768, 93, 5, 40, "Input"], Cell[50067, 1775, 48, 1, 22, "Input"], Cell[50118, 1778, 1894, 66, 64, "Text", Evaluatable->False], Cell[52015, 1846, 62, 1, 22, "Input"], Cell[52080, 1849, 189, 5, 25, "Text", Evaluatable->False] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[52318, 1860, 110, 2, 30, "Section", Evaluatable->False], Cell[52431, 1864, 856, 25, 51, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[53312, 1893, 96, 2, 42, "Subsection", Evaluatable->False], Cell[53411, 1897, 1205, 34, 77, "Text", Evaluatable->False], Cell[54619, 1933, 83, 4, 30, "Input"], Cell[54705, 1939, 1454, 47, 64, "Text", Evaluatable->False], Cell[56162, 1988, 98, 5, 40, "Input"], Cell[56263, 1995, 2391, 86, 64, "Text", Evaluatable->False], Cell[58657, 2083, 45, 1, 22, "Input"], Cell[58705, 2086, 199, 5, 25, "Text", Evaluatable->False], Cell[58907, 2093, 107, 5, 40, "Input"], Cell[59017, 2100, 443, 15, 38, "Text", Evaluatable->False], Cell[59463, 2117, 81, 4, 30, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[59581, 2126, 96, 2, 42, "Subsection", Evaluatable->False], Cell[59680, 2130, 2206, 72, 90, "Text", Evaluatable->False], Cell[61889, 2204, 98, 4, 30, "Input"], Cell[61990, 2210, 379, 13, 25, "Text", Evaluatable->False], Cell[62372, 2225, 97, 4, 30, "Input"], Cell[62472, 2231, 52, 1, 22, "Input"], Cell[62527, 2234, 51, 1, 22, "Input"] }, Closed]] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)