0% found this document useful (0 votes)
22 views

Introduction To Constructing and Using Functions in Mathematica

The document introduces functions in Mathematica. It discusses the difference between single and double equal signs, and how to define simple functions like f(x)=x^2. It then provides an example of writing a function to calculate Fibonacci numbers recursively. The key steps are to initialize the first two Fibonacci numbers to 1, and then define the nth number as the sum of the prior two numbers. Storing previously calculated values makes computations much faster. Finally, it prompts the reader to write a function to compute factorials.

Uploaded by

crod123456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Introduction To Constructing and Using Functions in Mathematica

The document introduces functions in Mathematica. It discusses the difference between single and double equal signs, and how to define simple functions like f(x)=x^2. It then provides an example of writing a function to calculate Fibonacci numbers recursively. The key steps are to initialize the first two Fibonacci numbers to 1, and then define the nth number as the sum of the prior two numbers. Storing previously calculated values makes computations much faster. Finally, it prompts the reader to write a function to compute factorials.

Uploaded by

crod123456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

INTRODUCTION TO CONSTRUCTING AND USING

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

We can evaluate expressions as well as numbers :


In[35]:=

fq
fq ^2
fz^3 z^2 z 1

Out[35]=

q2

Out[36]=

q4

1 z z2 z3

Out[37]=

A short program involving functions


Let' s see how we might construct a function to calculate Fibonacci numbers. You may know
that Fibonacci numbers are defined such that the nth Fibonacci number is equal to the sum of the
two prior numbers, or mathematically :
Fn = Fn-1 + Fn-2
In order to calculate these numbers, we need to establish what the first two Fibonacci numbers
are, since we need a starting point for our calculations. Fibonacci numbers are defined in such a
way that :
F1 = F2 = 1
What will we need to write a program to compute Fibonacci Numbers? We will need to construct a function that we can evaluate, and also will need to define the first two terms. If we call
our function fib, we write :
Clearfib
fib1 1; fib2 1;
fibn_ : fibn 1 fibn 2

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

Or with a bit more flourish :


In[86]:=

PrintStyle"i", Bold, FontSize 16, "


DoPrinti, "
", fibi, i, 3, 7

", Style"Fib. no.", Bold, FontSize 16

mFunctions.nb

Fib. no.

13

An Important Mathematica "Trick"


Now that we have a function written to compute Fibonacci numbers, we can, in principal, calculate any Fibonacci numbers we wish. As a test, calculate the value of the 35 th Fibonacci number ... be patient it will take a while. We can get a sense of the time it takes to produce this result
using :
In[91]:=
Out[91]=

22.781, 9 227 465

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

2 432 902 008 176 640 000

You might also like