(*^ ::[ Information = "This is a Mathematica Notebook file. It contains ASCII text, and can be transferred by email, ftp, or other text-file transfer utility. It should be read or edited using a copy of Mathematica or MathReader. If you received this as email, use your mail application or copy/paste to save everything from the line containing (*^ down to the line containing ^*) into a plain text file. On some systems you may have to give the file a name ending with ".ma" to allow Mathematica to recognize it as a Notebook. The line below identifies what version of Mathematica created this file, but it can be opened using any other version as well."; FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2"; MacintoshStandardFontEncoding; fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e8, 24, "Times"; fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e6, 18, "Times"; fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, e6, 14, "Times"; fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, a20, 18, "Times"; fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, a15, 14, "Times"; fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, a12, 12, "Times"; fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times"; fontset = input, noPageBreakInGroup, preserveAspect, groupLikeInput, M42, N23, bold, B65535, L-5, 12, "Courier"; fontset = output, output, inactive, noPageBreakInGroup, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; fontset = message, inactive, noPageBreakInGroup, preserveAspect, groupLikeOutput, M42, N23, R65535, L-5, 12, "Courier"; fontset = print, inactive, noPageBreakInGroup, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; fontset = info, inactive, noPageBreakInGroup, preserveAspect, groupLikeOutput, M42, N23, B65535, L-5, 12, "Courier"; fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, 12, "Courier"; fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, 10, "Geneva"; fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = leftheader, inactive, L2, 12, "Times"; fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7, 12, "Times"; fontset = leftfooter, inactive, L2, 12, "Times"; fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times"; fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; currentKernel; ] :[font = smalltext; inactive; preserveAspect] Copyright (C) 1997 Rich Neidinger, John Swallow, and Todd Will. Free for distribution to college and university instructors for personal, non-commercial use. If these notebooks are used in a course, the authors request $20 per student. :[font = title; inactive; preserveAspect] 3. Defining Variables and Functions :[font = smalltext; inactive; preserveAspect] Last revision: August 25 1996 :[font = text; inactive; preserveAspect] 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. :[font = section; inactive; Cclosed; preserveAspect; startGroup] Variable Declaration and Initialization: Set :[font = text; inactive; preserveAspect] 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 "mess = 7", Mathematica does several things. First, Mathematica evaluates the right-hand side of the expression (yielding an Integer, 7). Then Mathematica declares a new variable (by which we mean that Mathematica will remember from now on that a certain name is significant), named "mess". Then Mathematica assigns this variable an initial value, the Integer 7. Finally, the result 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 "mess"), any time we use "mess", Mathematica will use the Integer 7. We have made, then, a global declaration and have initialized the variable to the value 7. The word "global" refers to the fact that wherever 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. ;[s] 15:0,0;356,1;367,0;448,1;457,0;602,1;610,0;671,1;677,0;921,1;927,0;948,1;960 ,0;1032,1;1040,0;1240,-1; 2:8,13,9,Times,0,12,0,0,0;7,13,9,Times,2,12,0,0,0; :[font = text; inactive; preserveAspect] Let's consider a few examples. :[font = input; preserveAspect] a = 20.71 :[font = input; preserveAspect] b = a^2+a-1 :[font = text; inactive; preserveAspect] Notice here that the value of "b" is not saved as "a^2+a-1", but as a Real value of around 448.614. Remember, the right-hand side is evaluated first, and then the new value is assigned to the variable. :[font = text; inactive; preserveAspect] If we change the value of "a", the variable "b" will not change, as follows: :[font = input; preserveAspect] a = 19 :[font = input; preserveAspect] b :[font = text; inactive; preserveAspect] We can ask Mathematica what it knows about a variable, or, for that matter, any "name". Let's ask what it knows about "b": :[font = input; preserveAspect] ?b :[font = text; inactive; preserveAspect] What Mathematica is telling us is that "b" is globally declared, and its latest value is 448.6141. :[font = subsection; inactive; preserveAspect] Functional Forms :[font = text; inactive; preserveAspect] In Mathematica, there are usually several ways to accomplish any operation. For the value assignment operation we have performed above, we have an alternative functional form, Set[ ]. (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 arguments, or parameters. If the number of arguments is greater than 1, then the arguments are separated by commas (as in "N[Pi,50]"). For our function Set[ ], 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, "mess = 7" can be accomplished in functional form by evaluating "Set[mess,7]". Remember that just as with the assignment operation using the equals sign, this second argument will be evaluated first before the assignment happens. ;[s] 7:0,0;160,1;170,0;411,1;420,0;424,1;435,0;963,-1; 2:4,13,9,Times,0,12,0,0,0;3,13,9,Times,2,12,0,0,0; :[font = input; preserveAspect] Set[a,20.71] :[font = text; inactive; preserveAspect] Finally, we consider one more example. :[font = input; preserveAspect] bigmess = Expand[(x+2)^10] :[font = input; preserveAspect] bigmess :[font = text; inactive; preserveAspect; endGroup] All of the operators which we have seen have special functional forms. Here is a list of the operators, together with their functional forms: a>b Greater[a,b] a>=b GreaterEqual[a,b] a