Part II. Calculations and Calculus with Mathematica
Section 1. Getting Started: Mathematica as a Calculator
Mathematica is one of a handful of computer software applications that "does mathematics". It works following an input/output paradigm. The user types the input in an Input cell, Mathematica processes the input and returns the result, referred to as the output. An input cell appears automatically at the end of every worksheet and can be obtained anywhere by clicking between cells.
To add 1 and 2 below, we clicked below this text cell, typed "1+2" and then pressed the [shift]-[return] key. PC users, press the [shift]-[Enter] key.
The input is displayed on the computer screen in a bold monospaced font. The output appears directly below the input.
In[2]:=
1+2
Out[2]=
About the input. When an input entry is made with no trailing punctuation Mathematica processes it and places the output in an output cell. Several entries can be made in one input cell. Simply press the [return] key and type in another entry. If a particular entry is terminated with a semicolon ( ; ) then Mathematica will process the entry but will not show the output. The output is said to be suppressed.
The next input cell contains three entries asking for three square roots. Multiplication is specified with an asterisk * (shift-8) and exponentiation with the carat symbol ^ (shift-6). The multiplication sign can be omitted and replaced by a space if so desired. The square root function is denoted Sqrt (remember, square brackets must be used for all Mathematica functions)j.
In[134]:=
Sqrt[2*3]
Sqrt[5];
Sqrt[3^4]
Out[134]=
Out[136]=
All three square roots were processed, but only the first and third outputs are shown because the second entry terminated with a semicolon suppressing the output.
The entry Sqrt[3*2] outputs as , the exact value. Mathematica always attempts to return exact values, if exact values are entered. Compare the first output to the output for the entry
Sqrt[2*3.0]
In[12]:=
Sqrt[2*3.0]
Out[12]=
Mathematica interprets Sqrt[2*3.0] as an approximate input and returns an approximate output in what is called "floating point form" (6 digit accuracy is the default). It is often desirable to have floating point output even though the input is exact. In that case use the N function ( evaluate as a floating point number) as follows.
In[13]:=
(Sqrt[20]+Sqrt[30])/(Sqrt[5]+Sqrt[7])
Out[13]=
This is an exact value. The entry
N[%]
will output the number in 6 digit floating point form. The percent sign refers to the most recent output.
In[14]:=
N[%]
Out[14]=
If a 4 digit approximation is adequate, then enter the following. Two percent signs refer to the second to most recent output.
In[15]:=
N[%%,4]
Out[15]=
The next entry generates a 60 digit approximation to π. Note that Mathematica uses
Pi
to denote the mathematical constant π. Be sure to capitalize the p.
In[31]:=
Pi
N[Pi,60]
Out[31]=
Out[32]=
Note that the output for Pi is π.
A 10 digit approximation to the famous number e appears next. There are two ways to get Mathematica to output e. One way is to enter E. The other way is to apply the exponential function Exp to the number 1. These ideas are illustrated below.
In[25]:=
E
N[E,10]
Out[25]=
Out[26]=
Note that Mathematica prints e in its output as e . The same output can be obtained as follows
In[29]:=
Exp[1]
N[Exp[1],10]
Out[29]=
Out[30]=
Parentheses for grouping must be the round kind: ( )
Parentheses are often essential when entering complicated expressions. For example, the calculation of
must be made as follows.
In[33]:=
(2^50-1)/(2^49+1)
Out[33]=
As usual, Mathematica outputs the exact value. Here is the same number in floating point form (10 digits).
In[34]:=
N[%,10]
Out[34]=
And here is the 20 digit floating point form.
In[35]:=
N[%%,20]
Out[35]=
When grouping terms in an entry, never use square brackets, [ ] , or set brackets, {}. Square brackets are reserved in Mathematica to enter arguments in functions and set brackets are used to make lists of things like
{ milk, eggs, sugar, butter }
Likewise, all of the familiar functions that you know from your calculus days should be entered into Mathematica using square brackets and they must be capitalized. Mathematica is case sensitive.
In[38]:=
x=Sin[Pi/3] - Log[2] + Exp[4]
Out[38]=
Observe that Mathematica simplified Sin[Pi/3] because it has a nice exact form. The following entry puts x into floating point form (5 digits).
In[39]:=
N[x,5]
Out[39]=
Log[2] is the Mathematica's notation for the base e or natural logarithm. If a base b logarithm is needed, b positive and not 1, use square brackets to enter
Log[b,x]
as illustrated below.
In[42]:=
Log[10,100]
Log[2,256]
Out[42]=
Out[43]=
Lists in Mathematica
Several expressions, entered as a sequence separated by commas, and enclosed in set brackets, is called a list. Lists can be made easily using the Table function. These ideas are illustrated below.
The next input cell enters the list of the first four factorials.
In[45]:=
{1!, 2!, 3!, 4! }
Out[45]=
Note that Mathematica calculates the values and outputs the values in the same list brackets. Any list can be processed like this. If floating point form is desired apply N.
In[52]:=
{Sin[1], Sin[2], Sin[3] }
N[%,2]
Out[52]=
Out[53]=
Important Observation: Mathematica assumes that the arguments in a trigonometric function are in radians, never degrees.
The Table function
The two lists above are of the form { f(1), f(2), f(3), ..., f(n) }. Such a list can be obtained quickly and easily using the Table function in the form
Table[f[k], {k,n}]
In[57]:=
Table[Sin[k],{k,3}]
Out[57]=
In[58]:=
Table[n!, {n,4}]
Out[58]=
The nth term in a list L can be obtained with the entry L[[n]].
In[82]:=
L=Table[3^k, {k,5}]
Out[82]=
In[83]:=
L[[4]]
Out[83]=
Suppose that the elements in a list are themselves lists, like the following example.
In[3]:=
L=Table[ {x,x/(1+x),Sin[x]}, {x,2,12,3}]
Out[3]=
Then L[[m,n]] refers to the nth member of the mth list. See below
In[7]:=
{L[[3,2]], L[[1,1]], L[[2,3]]}
Out[7]=
Using the Table function
The Table function is very handy. The following entries will give you a better idea of how it works.
1. Make the list of odd integers from 11 to 37.
In[64]:=
Table[k, {k,11,37,2}]
Out[64]=
2. Make the sequence of numbers from 1/3 to 37/3 in increments of 4/3. Convert it to floating point form (5 digits).
In[66]:=
Table[k, {k,1/3,37/3,4/3}]
N[%,5]
Out[66]=
Out[67]=
3. Make a sequence of 20 prime integers starting at 7.
In[70]:=
Table[Prime[k], {k,4,23}]
Out[70]=
That was neat, how did it work?
Mathematica has a function called Prime, whose value at n, Prime[n], is the nth prime. For example, Prime[1] is 2, which is the first prime; 7 is the fourth prime, so Prime[4] = 7. Mathematica has other integer functions like this. For example, the PrimeQ[n] function returns true when n is prime and false otherwise.
In[72]:=
PrimeQ[10001237]
Out[72]=
In[73]:=
PrimeQ[100001237]
Out[73]=
4. Make a list of the binomial coefficients in the 15th row of Pascal's triangle.
In[79]:=
Table[Binomial[15,n], {n,0,15}]
Out[79]=
Add them up.
In[80]:=
Sum[%[[k]],{k,16}]
Out[80]=
Check that this sum is the same as .
In[81]:=
2^15
Out[81]=
Wow, what just happened?
The Sum function will add the first n terms in a list L via the syntax
Sum[ L[[k]], {k,1,n} ]
That's nice, but what I meant was why is the sum equal to
Oh that.Well, the 15th row of Pascal's triangle contains the coefficients in the expansion of the expression
Replacing the letters a and b with the number 1 makes the number and, at the same time, has the effect of adding the coefficients in the expansion of
.
The Table function is very versatile
Examine the following examples to see the versatility of the Table function.
Table can make a list of lists as follows.
In[85]:=
Table[a^m+b^n, {m,2,4}, {n,4,6} ]
Out[85]=
Table can work with text like this.
In[89]:=
L = {me,you,him,us,you,them};
Table[ (L[[k]]+TheKing)/L[[k]], {k,6}]
Out[90]=
Several lists can be joined together with the Join function. The next input cell first creates three lists (output suppressed) then joins them into one long list.
In[14]:=
L1=Table[11,{3}]; L2=Table[4,{12}]; L3=Table[12,{13}];
S=Join[L1,L2,L3]
Out[15]=
The Product function multiplies the numbers in the list named S.
In[16]:=
Product[S[[k]],{k,28}]
Out[16]=
FactorInteger generates the prime factorization.
In[17]:=
FactorInteger[%]
Out[17]=
Interpret the output as .
I get it, Product is like Sum, only it multiplies, right?
Right. See the Help page.
The Help page?
Yes. For example, to learn about the FactorInteger function you can enter
In[109]:=
?FactorInteger
and click on More... for more information.
Moving towards symbolic calculations: Factoring in Mathematica.
The last example shows that the FactorInteger function factors integers. Mathematica's Factor function will factor polynomials. We will define a polynomial in the x variable named "Poly" below. However, before we define it we must free up the x variable since at the present time (in this Notebook) x is already in use.
In[112]:=
x
Out[112]=
Free x with the entry
Clear[x]
or the entry
x = .
In[113]:=
x = .
Here is the definition of Poly.
In[129]:=
Poly = Sum[ 2^k*x^k, {k,4}]
Out[129]=
The Factor function will factor Poly over the integers.
In[130]:=
Factor[Poly]
Out[130]=
Add the optional expression
Extension -> {I}
to factor Poly over the complex numbers of the form a + i b where a and b are integers. These are called Gaussian integers. (The symbol I represents the complex number i , i.e. .)
In[131]:=
Factor[Poly,Extension->{I}]
Out[131]=
Note that in the output Mathematica uses the symbol i to represent i .
To see the zeros of Poly use the Solve function as follows.
In[132]:=
Solve[Poly==0,x]
Out[132]=
Note two things.
1. The syntax for Solve is Solve[ equation, variable]. The equation to be solved must be in the form
lhs == rhs
to distinguish it from an equation defining the value of a variable.
2. The output is list of lists. Each list contains a solution in the form x -> a.
We will have more to say about Solve in the next section.
Created by Mathematica (September 11, 2004)