Introduction To Constructing and Using Functions in Mathematica
Introduction To Constructing and Using Functions in Mathematica
FUNCTIONS IN MATHEMATICA
Different Types of Equal Signs
We have discussed that there are a number of different types of equal signs in Mathematica. The
single equal sign, as for example x = 3, immediately assigns the value of 3 to the variable x, and
the value of x will remain equal to 3 until we redefine or clear the value of x. So, if we set x = 3
and then try to solve the trivial equation :
In[22]:=
x 3;
Solvex 4 0, x
General::ivar : 3 is not a valid variable.
Out[23]=
SolveFalse, 3
We get the error message that 3 is not a valid variable. If we clear the value of x :
In[24]:=
Out[25]=
Clearx
Solvex^2 5 x 6 0, x
x 2, x 3
We obtain the solutions we expect. In fact, we encountered a second type of equal sign, the
double equal sign (==); we use this in Mathematica when we are testing whether one side of an
expression equals the other side. This makes the double equal sign the appropriate symbol to
use in solving equations
Defining Functions in Mathematica
(For more information on this topic, see functions in the online Doc Center.)
Suppose we want to write the Mathematica version of a simple function, say
f (x) = x2 . We write this as:
In[28]:=
Clearx
fx_ : x ^ 2
mFunctions.nb
It is important to put the underscore in the brackets on the left, but no underscore on the right.
(In Mathematica jargon, this use of underscore is called a "blank"). In essence, we have created
our own (albeit not very novel) function. We can evaluate this function :
In[31]:=
f7
Out[31]=
49
fq
fq ^2
fz^3 z^2 z 1
Out[35]=
q2
Out[36]=
q4
1 z z2 z3
Out[37]=
mFunctions.nb
Let' s review each step of this program. The first step clears the value of fib so that no possible
prior values of fib will be carried through. The second line defines the first and second
Fibonacci numbers to equal one. Notice the use of semi - colons; semi - colons in Mathematica
are used to suppress output. By inserting semi - colons, the output will not include the input
statements fib[1] = 1 or fib[2] = 1.
Finally, the third statement defines our function; the nth Fibonacci number is the sum of the two
prior Fibonacci numbers. Let' s see if it all works; let' s calculate the next five Fibonacci numbers . We can do this inelegantly as :
In[57]:=
fib3
fib4
fib5
fib6
fib7
Out[57]=
Out[58]=
Out[59]=
Out[60]=
Out[61]=
13
Or more compactly :
In[62]:=
DoPrintfibi, i, 3, 7
2
3
5
8
13
mFunctions.nb
Fib. no.
13
Timingfib35
The solution set in output statement 91 consists of the time (in secs) required to compute the 35
th Fibonacci number. This computation took so long because Mathematica did not store any
prior results. In other words, if we were to do this calcuation by hand, we would simply add the
two most recent Fibonacci numbers to produce the next number in the sequence. In essence, by
writing numbers on a page or storing values on our calculators, we store each value and merely
need to add to previous values.
Not so with Mathematica; in order to get Mathematica to store prior results, we make use of
this simple trick (uh, technique) :
In[92]:=
Out[95]=
Clearfib
fib1 1; fib2 1;
fibn_ : fibn fibn 1 fibn 2
Timingfib35
0., 9 227 465
You might have noticed that calculation went a bit faster. What is the difference in the two
programs? Look in the third line. Notice that we inserted "fib[n]" between the declaration of
the function ("fib[n_]:=") and the definition of Fibonacci numbers ("fib[n-1]+fib[n-2]"). This
insertion instructs Mathematica to store all previously calculated values of "fib[n]"; the computation of Fibonacci numbers occurs much more quickly when Mathematically does not have to
start from n=1 each time a new number is computed.
Your turn
mFunctions.nb
Try to write a program that will compute n!. Check your results against the values computed
from the Mathematica function for factorial, e.g. :
In[119]:=
20
Out[119]=