(*^ ::[ 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, ""; fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times"; fontset = input, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, 12, "Courier"; fontset = output, output, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-4, 12, "Courier"; fontset = message, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier"; fontset = print, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier"; fontset = info, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier"; fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, 12, "Courier"; fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, 10, "Times"; fontset = header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, ""; fontset = leftheader, inactive, L2, 12, "Times"; fontset = footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, 12, ""; fontset = leftfooter, inactive, L2, 12, "Times"; fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times"; fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, ""; fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Courier"; fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 14, "Times"; fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, ""; fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Courier"; fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, ""; fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, ""; paletteColors = 128; automaticGrouping; currentKernel; ] :[font = title; inactive; preserveAspect; startGroup] Example 04 Functions :[font = subsubtitle; inactive; preserveAspect] Copyright ã 1993 by Bill Titus, Carleton College, Department of Physics and Astronomy, Northfield, MN 55057-4025 September 6, 1993 ;[s] 3:0,0;10,1;11,0;133,-1; 2:2,16,12,Times,2,14,0,0,0;1,16,12,Symbol,0,14,0,0,0; :[font = section; inactive; preserveAspect; startGroup] Topics and Skills :[font = special1; inactive; preserveAspect; endGroup] 1. Properties of a function: square brackets and the head. 2. Using ?? for extended help. 3. Function attributes: Listable and Protected. 4. Sqrt[], N[], and Precision[]. 5. Nesting MMA functions. 6. Trig functions and the MMA constant Pi. 7. Exp[], Log[], and BesselJ[]. 8. Head[] and FullForm[]. 9. List[]. 10. User-defined functions with Set: patterns, SetAttributes[], Remove[], and usage statements. ;[s] 29:0,0;72,1;74,0;151,1;157,0;160,1;163,0;171,1;182,0;252,1;254,0;260,1;265,0;268,1;273,0;281,1;290,0;296,1;303,0;308,1;318,0;324,1;330,0;366,2;369,0;383,1;398,0;401,1;409,0;433,-1; 3:15,16,12,Times,0,14,0,0,0;13,15,11,Courier,0,14,0,0,0;1,15,11,Helvetica,0,14,0,0,0; :[font = section; inactive; preserveAspect; startGroup] Problem :[font = special1; inactive; preserveAspect] A ball is dropped from rest a height 100 m above the ground. How long does it take to hit the ground and what's its speed upon impact? :[font = special1; inactive; preserveAspect] The basic equations, assuming the coordinate system is chosen with its x-axis pointing vertically upward and its origin at the location from where the ball is dropped, are: :[font = special1; inactive; preserveAspect] v[t] = v[0] - gt x[t] = v[0]t - (1/2)gt2 ;[s] 3:0,1;45,2;46,1;48,-1; 3:0,16,12,Times,0,14,0,0,0;2,15,11,Courier,0,14,0,0,0;1,23,15,Courier,32,14,0,0,0; :[font = special1; inactive; preserveAspect] In our case, with to denoting the time it takes to hit the ground, x[to] = -100, g = 9.8, and v[0] = 0. So :[font = special1; inactive; preserveAspect] v[to] = -gto x[to] = -(1/2)gto2 ;[s] 3:0,0;4,1;38,2;40,-1; 3:1,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0;1,23,15,Courier,32,14,0,0,0; :[font = special1; inactive; preserveAspect] and :[font = special1; inactive; preserveAspect; endGroup] to = Ö[-2x[to]/g] v[to] = -gto = -Ö[-2x[to]g] ;[s] 6:0,0;3,1;8,2;9,1;39,2;40,1;51,-1; 3:1,16,12,Times,0,14,0,0,0;3,15,11,Courier,0,14,0,0,0;2,16,12,Symbol,0,14,0,0,0; :[font = section; inactive; preserveAspect; startGroup] Solution using Set and Sqrt[] ;[s] 4:0,0;16,1;19,0;26,2;33,-1; 3:2,19,14,Times,1,18,0,0,0;1,19,14,Helvetica,1,18,0,0,0;1,18,13,Courier,1,18,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Step 1 - The following code solves our problem using Set, the symbols xo, to, vo, and g, and the built-in MMA square root function, Sqrt[]. Look the code over and then activate the cell. ;[s] 5:0,0;54,2;57,0;139,1;145,0;195,-1; 3:3,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0; :[font = input; preserveAspect; endGroup] Clear[xo, to, vo, g] g = 9.8; xo = -100.0; to = Sqrt[-2xo/g] vo = Sqrt[-2xo*g] :[font = subsection; inactive; preserveAspect; startGroup] Comment 1 :[font = special1; inactive; preserveAspect; endGroup; endGroup] 1. Note that the code is structured so that it could easily be modified if xo or g were changed. 2. Was it necessary to enter xo as the real -100.0, or could you've have typed in the integer -100? Try it out. 3. What would happen if xo where entered as postive number instead of a negative one? Try it out. 4. The code uses two built-in MMA functions, Clear[] and Sqrt[]. What do these functions have in common? 5. Square brackets are used to contain the arguments of functions. 6. The "head" is the name of the function. MMA functions start with captial letters; if they contain more than one word, each word is captialized; abbreviations are usually not used unless there are standard ones available. ;[s] 5:0,0;371,1;379,0;384,1;390,0;729,-1; 2:3,16,12,Times,0,14,0,0,0;2,15,11,Courier,0,14,0,0,0; :[font = section; inactive; preserveAspect; startGroup] Built-in Functions: Sqrt[], N[], and Precision[] ;[s] 6:0,0;21,1;27,0;30,1;33,0;40,1;52,-1; 2:3,19,14,Times,1,18,0,0,0;3,18,13,Courier,1,18,0,0,0; :[font = subsection; inactive; preserveAspect] Step 1 - Get help on the MMA function Sqrt[]. Describe what you find. Now repeat your request for help using two question marks instead of one. ;[s] 3:0,0;39,1;45,0;147,-1; 2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Comment 1 :[font = special1; inactive; preserveAspect; endGroup] 1. Two question marks gives extended help. 2. The Sqrt[] function has two attributes: Protected and Listable. What do you think these attributes do? How could you find out? 3. Many built-in MMA functions are listable. Why is this a useful property? ;[s] 3:0,0;53,1;59,0;258,-1; 2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 2 - Apply Sqrt[] to the list {1, 3, 9}. Describe what happens. ;[s] 5:0,0;16,1;23,0;36,1;45,0;71,-1; 2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Comment 2 :[font = special1; inactive; preserveAspect; endGroup] 1. Why wasn't the square root of 3 evaluated? 2. What if you wanted to numerically evaluate the square root of 3. One method is to use reals, i.e, replace 3 by 3.0. Try it. 3. Another method is to use the MMA function N[]. ;[s] 3:0,0;231,1;234,0;236,-1; 2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 3 - Get help on N[]. Describe what you find and use it to determine the numerical value for the square root of 3 both with and without the second argument that's mentioned for N[]. ;[s] 5:0,0;22,1;25,0;186,1;189,0;191,-1; 2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Comment 3 :[font = special1; inactive; preserveAspect; endGroup] 1. In MMA, the precision of a number is not limited by "machine accuracy". 2. The MMA function Precision[] can be used to find the precision of a symbol or number. ;[s] 3:0,0;99,1;111,0;169,-1; 2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 4 - Get help on Precision[]. Then apply Precision[] to a specific integer, rational, real, complex number, string, and symbol. What do you find? ;[s] 5:0,0;22,1;33,0;48,1;59,0;157,-1; 2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Comment 4 :[font = special1; inactive; preserveAspect; endGroup] 1. The default precision of reals can vary depending on the type of computer on which MMA is running. What's the default percision of reals on the Mac? under NeXTStep? :[font = subsection; inactive; preserveAspect; startGroup] Step 5 - What would you expect for the results of the following code? Try it out. :[font = input; preserveAspect; endGroup] Precision[N[Sqrt[3]]] Precision[N[Sqrt[3], 50]] :[font = subsection; inactive; preserveAspect; startGroup] Comment 5 :[font = special1; inactive; preserveAspect; endGroup; endGroup] 1. Note that MMA functions can be nested. Where have you used this property before? Why is this property useful? :[font = section; inactive; preserveAspect; startGroup] Some Other Built-in Functions :[font = subsection; inactive; preserveAspect] Step 1 - What do you think are some of MMA's built-in trig functions? In what unit is it likely that angles are measured? Evaluate some of the trig functions for angles you know. Use the fact that MMA has a built-in constant called Pi. ;[s] 3:0,0;236,1;238,0;240,-1; 2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Comment 1 :[font = special1; inactive; preserveAspect; endGroup] 1. Did you try Tan[Pi/2]? If not, do so. 2. ComplexInfinity refers to a quantity with infinite magnitude, but undetermined complex phase. 3. Note that Pi is treated as an exact constant. So, for example, Pi/4 is not evaluated numerically. 4. Did you apply a trig function to a complex number? If not, do so. So what other functions are surely built into MMA? ;[s] 9:0,0;17,1;26,0;49,1;64,0;159,1;161,0;215,1;219,0;375,-1; 2:5,16,12,Times,0,14,0,0,0;4,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 2 - What are two ways to find the numerical values of a trig function whose argument is an integer or rational? Do it for Sin[1] and Sin[Pi/5]. ;[s] 5:0,0;129,1;135,0;142,1;151,0;153,-1; 2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 3 - Come up with some other elementary functions that are built into MMA and try them out. :[font = subsection; inactive; preserveAspect; startGroup] Comment 3 :[font = special1; inactive; preserveAspect; endGroup] 1. Make sure you examine the functions Exp[] and Log[]. ;[s] 5:0,0;41,1;46,0;53,1;58,0;60,-1; 2:3,16,12,Times,0,14,0,0,0;2,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 4 - Suppose you thought there might be a Bessel function defined within MMA. How might you find it? :[font = subsection; inactive; preserveAspect; startGroup] Comment 4 :[font = special1; inactive; preserveAspect; endGroup] 1. If you guess incorrectly using help, sometimes the error message tells you what you want. 2. You can use help with the * wild card. For example ?B* gives all MMA commands and symbols that start with B while ?*B* yields all the MMA commands and symbols that contain the letter B. Try this out and then get help on the Bessel function of the first kind. ;[s] 11:0,0;125,1;126,0;153,1;156,0;210,1;211,0;220,1;224,0;291,1;292,0;368,-1; 2:6,16,12,Times,0,14,0,0,0;5,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 5 - The elementary arithmetic operators are also functions and thus have heads. Try Plus[3, 5] in a cell below. What do you find? Now try the MMA command Head[3 + 5] and Head[a + 2]. What do you find? ;[s] 7:0,0;91,1;101,0;165,1;176,0;183,1;194,0;215,-1; 2:4,16,12,Times,1,14,0,0,0;3,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Comment 5 :[font = special1; inactive; preserveAspect; endGroup] 1. Did you get help on Head[]? If not, do so. 2. Why didn't Head[3 + 5] yield Plus? 3. MMA stores basic arithmetic operations as functions. You can find out internal forms of combinations of symbols, numbers, and functions using the MMA function FullForm[]. ;[s] 9:0,0;25,1;31,0;65,1;76,0;85,1;89,0;256,1;266,0;268,-1; 2:5,16,12,Times,0,14,0,0,0;4,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 6 - Get help of the MMA function FullForm[]. Then apply Head[] and FullForm[] to each of the following functions: a*z, a-z, a^z, a/z, and (a + z^4)/10. What can you conclude? ;[s] 17:0,0;39,1;49,0;65,1;71,0;78,1;88,0;126,1;129,0;132,1;135,0;138,1;141,0;144,1;147,0;156,1;168,0;194,-1; 2:9,16,12,Times,1,14,0,0,0;8,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Comment 6 :[font = special1; inactive; preserveAspect; endGroup] 1. Plus[], Times[], and Power[] are the basic arithmetic functions. 2. Note that FullForm[] tells you the order in which arithmetic operations are performed. ;[s] 9:0,0;4,1;10,0;13,1;20,0;27,1;34,0;87,1;97,0;165,-1; 2:5,16,12,Times,0,14,0,0,0;4,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 7 - Find the head of {1, 2, 3}. What conclusion can you make? ;[s] 3:0,0;27,1;36,0;69,-1; 2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Comment 7 :[font = special1; inactive; preserveAspect; endGroup; endGroup] 1. The MMA notation stuff[...] is called an expression. ;[s] 3:0,0;22,1;32,0;59,-1; 2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0; :[font = section; inactive; preserveAspect; startGroup] Practice Problems :[font = subsection; inactive; preserveAspect] Generate a table of zero order Bessel functions of the first kind for x between 0.0 and 5.0 in steps of 0.5. Produce a table with a column for x and a column for BesselJ[0, x]. Use a single line of code. :[font = subsection; inactive; preserveAspect; endGroup] Generate a table of six numerical values of sin[x] for x uniformly spaced between 0 and Pi/2. Use a single line of code. How many levels of function nesting did you use? :[font = section; inactive; preserveAspect; startGroup] User-Defined Functions :[font = subsection; inactive; preserveAspect; startGroup] Step 1 - You're now going to define your own function corresponding to f[x] = e-x sin[x]. Do so by typing in the following code in a cell below: ;[s] 3:0,0;80,1;82,0;149,-1; 2:2,16,12,Times,1,14,0,0,0;1,24,16,Times,33,14,0,0,0; :[font = special3; inactive; preserveAspect; endGroup] Clear[f, x] f[x_] = Exp[-x] Sin[x] :[font = subsection; inactive; preserveAspect; startGroup] Comment 1 :[font = special1; inactive; preserveAspect; endGroup] 1. The underscores (also called a "blank") implies that x is the argument of f[]. The combination x_ is a simple named pattern; patterns are symbols that have underscore(s). 2. Functions can be defined with more than one argument. To do so, just separate the arguments with commas. 3. In a function definition, the underscore(s) only appear on the left hand side of the assignment statement. ;[s] 7:0,0;59,1;60,0;82,1;85,0;105,1;107,0;404,-1; 2:4,16,12,Times,0,14,0,0,0;3,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 2 - Get extended help on the function you just created. :[font = subsection; inactive; preserveAspect] Step 3 - Try out your function to make sure it works with both numbers and symbols. Describe what you find. What happens if you apply it to a list? to an argument with two symbols separated by commas? :[font = subsection; inactive; preserveAspect; startGroup] Comment 3 :[font = special1; inactive; preserveAspect; endGroup] 1. What's going on with the list argument? 2. Why didn't f[1,2] do anything interesting? If MMA can't evaluate something, it will echo what you typed back to the screen. 3. Go back and see what happens if you forget to put in the underscore in the definition of f[x]. Why do you get the results you do in this case? 4. Now, you're going to be using f[] later on, so go to Step 1, replace x by x_ on the left hand side of the equation, and activate the cell. ;[s] 11:0,0;60,1;66,0;269,1;273,0;359,1;362,0;400,1;401,0;407,1;409,0;473,-1; 2:6,16,12,Times,0,14,0,0,0;5,15,11,Courier,0,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Step 4 - Suppose you have a function g[] that isn't listable. What would you get if you applied it to the list {1, 2}? Do so by entering the following code: ;[s] 5:0,0;38,1;41,0;116,1;121,0;164,-1; 2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0; :[font = input; preserveAspect; endGroup] Clear[g] g[{1, 2}] :[font = subsection; inactive; preserveAspect] Step 5 - You can make g[] listable by giving it the attribute Listable using the MMA function SetAttributes[]. To so and then prove that you were successful. However, get help on SetAttributes[] before you begin. ;[s] 7:0,0;23,1;26,0;97,1;112,0;186,1;201,0;221,-1; 2:4,16,12,Times,1,14,0,0,0;3,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 6 - Get extended help on your function g[]. What do you find? ;[s] 3:0,0;46,1;49,0;70,-1; 2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 7 - Now suppose you want to remove your definition of g[]. Try doing so with Clear[] and then check the result with ??g . What do your find? ;[s] 7:0,0;61,1;64,0;86,1;93,0;127,1;130,0;153,-1; 2:4,16,12,Times,1,14,0,0,0;3,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect] Step 8. Get help on the MMA function Remove[]. Apply it to g[] and see what happens. Summarize the difference between Clear[] and Remove[]. ;[s] 9:0,0;39,1;47,0;63,1;66,0;125,1;132,0;139,1;147,0;149,-1; 2:5,16,12,Times,1,14,0,0,0;4,15,11,Courier,1,14,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Step 9 - You can document you own functions so that when you use help, a "usage statement" is printed out. Produce a usage statement for your function f[] by typing in the following. Then verify that that your usage statement is indeed present when you ask for help and extended help. ;[s] 3:0,0;154,1;158,0;289,-1; 2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0; :[font = special3; inactive; preserveAspect; endGroup] f::usage = "f[x] corresponds to Exp[-x] * Sin[x]" :[font = subsection; inactive; preserveAspect; startGroup] Comment 9 :[font = special1; inactive; preserveAspect; endGroup; endGroup] 1. Note the difference between help and extended help when a usage statement is present. :[font = section; inactive; preserveAspect; startGroup] Practice Problem :[font = subsection; inactive; preserveAspect; endGroup] Return to the free fall problem. Define your own functions (including usage statements) that yield t and v as functions of both xo and g. Start with Clear[t, v, xo, g]. Get extended help on your function and then use your functions to determine t and v[t] when xo = -100 and g = 9.8. What do you see as the advantage of using functions over symbols? ;[s] 3:0,0;161,1;179,0;371,-1; 2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0; :[font = section; inactive; preserveAspect; startGroup] Save Your Notebook :[font = special1; inactive; preserveAspect; endGroup; endGroup] Remove any output cells from this notebook and then use Save As to store your notebook under the name myEx04. ;[s] 5:0,0;57,1;64,0;105,1;111,0;113,-1; 2:3,16,12,Times,0,14,0,0,0;2,15,11,Helvetica,0,14,0,0,0; ^*)