Example 04
Functions

Copyright © 1993 by Bill Titus, Carleton College, 
Department of Physics and Astronomy, Northfield, MN  55057-4025
September 6, 1993

Topics and Skills

Problem

Solution using  Set  and  Sqrt[]

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.

Clear[xo, to, vo, g]
g  = 9.8;
xo = -100.0;
to = Sqrt[-2xo/g]
vo = Sqrt[-2xo*g]

Comment 1

Built-in Functions:  Sqrt[],  N[], and  Precision[]

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.

Comment 1

Step 2 - Apply  Sqrt[] to the list  {1, 3, 9}.  Describe what happens.

Comment 2

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[].

Comment 3

Step 4 - Get help on  Precision[].  Then apply  Precision[]  to a specific integer, rational, real, complex number, string, and symbol.  What do you find?

Comment 4

Step 5 -  What would you expect for the results of the following code?  Try it out.

Precision[N[Sqrt[3]]]
Precision[N[Sqrt[3], 50]]

Comment 5

Some Other Built-in Functions

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.

Comment 1

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].

Step 3 - Come up with some other elementary functions that are built into MMA and try them out.

Comment 3

Step 4 - Suppose you thought there might be a Bessel function defined within MMA.  How might you find it?

Comment 4

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?

Comment 5

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?

Comment 6

Step 7 - Find the head of  {1, 2, 3}.  What conclusion can you make?

Comment 7

Practice Problems

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.

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?

User-Defined Functions

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:

Comment 1

Step 2 - Get extended help on the function you just created.

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?

Comment 3

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:

Clear[g]
g[{1, 2}]

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.

Step 6 -  Get extended help on your function  g[].  What do you find?

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?

Step 8.  Get help on the MMA function  Remove[].  Apply it to  g[]  and see what happens.  Summarize the difference between  Clear[]  and  Remove[].

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.

Comment 9

Practice Problem

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?

Save Your Notebook