Learning Mathematica Basics—Functions I

Solve each of the following problems using Mathematica exclusively.

1.	Set the variable x equal to 5 using the command x=5.

2.	Now define the function f(x)=x^2+3 using the command f[x]=x^2+3.  (Note:  just like with Mathematica's own built-in functions, when you create a user defined function you enclose it's arguments in brackets, not parentheses.)

Note: Now where did that 28 come from?

3.	Now calculate f(4) using the command f[4].

Note:  Apparently the function f does not even know how to find f(4).

4.	Now calculate f(5) using the command f[5].

Note:  Observe what happened here.  The function f can find f(5).  So, because of the predefined value of x, the definition of f got screwed up. This shouldn't happen this way.  Now you may think that the solution to this would be to avoid giving x a value in the first place, but consider the next problem.

5.	Define the function g(y) = y^2+3 using the command g[y]=y^2+3.

6.	Now calculate g(4) using the command g[4].

Note:  Well, it still didn't work.  But look at the next problem

7.	Now calculate g(y) using the command g[y].

Note:  It would appear that a function only remembers its definition for the value, or the variable, originally used in the definition, (in our case f(5) and g(y).)  The reason for this was our failure to use a wild-card "blank" in our definition.  Look at the next problem.

8. Define the function h(x) = x^2+3 using the command h[x_]=x^2+3.

Note: Oops!  That 28 looks ominous!  We've seen it show up before.  Well, let's try the function out anyway.

9.	Now calculate h(4), h(5), and h(6) using the commands h[4], h[5], and h[6].

Note:  A moment's reflection should verify that only h(5) here is correct.  So let's consider what we've discovered.  Every one of these gave an answer, but all of the answers were given as if we'd asked for h(5).  This is slighly better than before we used the wild-card "blank,"  (when we got no answer at all most of the time.)  But the predefined x=5 is still "leaking" into our definition.  Avoiding presetting x=5 is still a poor approach——there will be situations when we really do want to do this.  So what's the fix?

10.  Define the function k(x) = x^2+3 using the command k[x_]:=x^2+3.

11.	Now calculate k(4) using the command k[4].

Note: At last, a function that works as expected!  Both the blank and the colon were key to the definition.  The blank denotes the variable being used as a wildcard, i.e. it tags the variable used as being totally unimportant, a dummy, or guinea-pig variable.  The colon denotes the left-to-right assignment as being delayed, i.e. the value of x at the time the function is defined is unimportant.  The value in the brackets when the function is called (i.e. evaluated later) is what counts.  Using both of these constructs when defining functions will be the norm, however, just occasionally, one or both of them may be left out for a reason.

12.	Now define the function p(x) = x^3 - x^2, using the constructs discovered above, and evaluate the following: p(3), p(7), p(-2), p(a), p(t^2), and p(sin(x)).

13.   The way we define functions of more than one variable is basically the same as we discovered above.  For example, g(x,y,z) would normally be defined in Mathematica by starting the command with g[x_,y_,z_]:=  (unless really did want to use immediate assignment, which on occasion you might.)

Define the function z(x,y)=x^2 sin(x^2+y^2) using wildcards, and delayed assignment.

14.   Evaluate the following:  z(1,2), z(2,d), z(7,-3).  (No need to use N, I don't want decimals.)

15.   To finish up, let's look at some pictures.  First, some two-dimensional graphs.  Since the function z(x, y) depends on two variables, a complete graph would require three dimensions.  In oder to drop back into two dimensions we use slicing through the function.  One way of doing this is to fix the value of either x or y, and let the other one act as the only variable.

To start with let's fix the x-value at x=4.  This means we'll be plotting z(4, y) against y.  If we use the interval  -2π≤y≤2π the command would be:

Plot[z[4,y],{y,-2Pi,2Pi}]

Go ahead and enter it.

16.   Now you graph the slice through the function where you fix the y-value at y=3, i.e. z(x, 3).  Use the interval -2π≤x≤2π again.

17.   As you might imagine, with slices as weird looking as these, the actual three-dimensional graph must be quite interesting.  Later we'll learn in more detail how to do both kinds of graph.  For now, just to satisfy your curiosity, issue the following three-dimensional plotting command:

Plot3D[z[x,y],{x,-Pi,Pi},{y,-Pi,Pi}, 
		PlotPoints->25,
		ViewPoint->{1.457, -1.815, 2.456}]

Note:  If you wish, redo the last plot below with different choices for the domain values of x and y.