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

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.

Problem

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?

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:

v[t] = v[0] - gt
   x[t] = v[0]t - (1/2)gt2

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

v[to] =  -gto
  x[to] =  -(1/2)gto2

and

to = ÷[-2x[to]/g]
  v[to] = -gto = -÷[-2x[to]g]

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

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.

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

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?

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

Comment 2

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

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

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.

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

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?

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

1.  Note that MMA functions can be nested.  Where have you used this property before?  Why is this property useful?

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

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?

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

1.  Make sure you examine the functions  Exp[]  and  Log[].

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

Comment 4

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.

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

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

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

1.  Plus[],  Times[], and  Power[] are the basic arithmetic functions. 
2.  Note that  FullForm[]  tells you the order in which arithmetic operations are performed.

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

Comment 7

1.  The MMA notation  stuff[...]  is called an expression.

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:

Clear[f, x]
	f[x_] = Exp[-x] Sin[x]

Comment 1

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.

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

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.

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.

f::usage = "f[x] corresponds to  Exp[-x] * Sin[x]"

Comment 9

1.  Note the difference between help and extended help when a usage statement is present.

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

Remove any output cells from this notebook and then use  Save As  to store your notebook under the name  myEx04.