Mathematica as a Calculator

The simplest use of Mathematica is as a calculator. Unlike standard calculators, there
is no limit to the size of numbers or the type of functions that Mathematica can handle.
An expression is evaluated by entering it in the usual mathematical notation. You can evaluate
the following expression by pressing the shift and return keys simultaneously.

In[1]:=

   2+ 11 * 8
  

Out[1]=

  90

Try other expressions to get used to the syntax used in Mathematica. Here are
a few examples.

In[2]:=

  
  2^67 -1

Out[2]=

  147573952589676412927

In[3]:=

  (11^23-6^23)/5

Out[3]=

  179085890705002863728743

In[4]:=

  17/29

Out[4]=

  17
  --
  29

Notice that the result of 17/29 is again 17/29. This is an important feature of Mathematica.
Mathematica represents mathematical values exactly, without any approximations. The
value of 17/29 is 17/29, so that is the result given by Mathematica. If you wish to see
a decimal approximation, then you can use the N function of Mathematica. The
command
N[ expression, places] gives a approximation of the expression to the specified number
of places.

In[5]:=

  
  N[17/29, 8]

Out[5]=

  0.5862069

Exercise: Compute the value of square root of 2 to 50
decimal places. The Mathematica function for evaluating the square root is
Sqrt[ expression].

In[6]:=

  
  

Mathematical constants like e, pi, and i are represented
by E, Pi, I in Mathematica. Mathematica has all built-in variables and functions starting with a
capital letter, hence it is a good idea to start all your variables and functions with a lower
case letter to avoid any confusion with an existing name.


Another feature of Mathematica is worth paying attention. Recall that we use the symbol
^ for exponentiation. Let us compute the cube root of -1.

In[7]:=

  (-1)^(1/3)

Out[7]=

      1/3
  (-1)

In[8]:=

  N[ (-1)^(1/3), 6]

Out[8]=

  0.5 + 0.866025 I

Observe that the approximation of the cube root of -1 is a complex number. The number
-1 has three cube roots, two of them complex. Mathematica works internally with complex
numbers, so it has selected a primitive complex cube root. To be able to compute
a real cube root of a negative real number, we have to define a function ourselves. We
will see how to accomplish this in a later section.

Exercise: Determine sin(pi/12) to 8 decimal places. Compute sin(x)/x for values of
x close to zero to observe the limit as x approaches 0. The Mathematica function for
evaluating sin(x) is Sin[x].

Here are examples of a few more useful functions. Quotient[a,b] gives the quotient
when a is divided by b and Mod[a,b] gives the remainder that has the same sign as b. The
two functions satisfy the relation

a= b Quotient[a,b] + Mod[a,b].

In[9]:=

   Quotient[ -34, 7]
  

Out[9]=

  -5

In[10]:=

  Mod[ -34, 7]

Out[10]=

  1

In[11]:=

  Quotient[ 34, -7]

Out[11]=

  -5

In[12]:=

  Mod[ 34, -7]

Out[12]=

  -1

Exercise: Verify that the relation between the Quotient and Remainder that is
specified above is valid.


Another useful function is Floor[ x], that returns the largest integer less than x. Here x has to
be an expression that evaluates to a real number, otherwise Floor doesn't do anything.
Related functions are Ceiling and Round; Ceiling returns the smallest integer greater
than the expression and Round returns the nearest integer. Here are a few examples.

In[13]:=

  Floor[ -4.5]

Out[13]=

  -5

In[14]:=

  Ceiling[-4.5]

Out[14]=

  -4

In[15]:=

  Round[ -4.6]

Out[15]=

  -5

In[16]:=

  Round[ -4.4]

Out[16]=

  -4

Exercise: What does Round do for numbers of the form integer + 1/2?

In[17]:=

  
  Floor[1/3]

Out[17]=

  0

In[18]:=

  Floor[Sqrt[2]]

Out[18]=

  Floor[Sqrt[2]]

Brackets, Parenthesis, and Braces

Brackets, parenthesis, and braces are intended for different purposes. Each has
a specific meaning and their use cannot be interchanged.

Paranthesis: These are used for grouping expressions. Careful use of parenthesis
is necessary to make clear the meaning of arithmetical expression. This is because multiplication
and division have a higher order of precedence than addition and subtraction.
For example, 3 + 7* 8 is not (3+7) *8 but 3 + (7*8). The expression
21/7-5 is (21/7) -5 and not 21/(7-5).

In[19]:=

  21/7-5

Out[19]=

  -2

In[20]:=

  21/(7-5)

Out[20]

In Mathematica (as in other programming languages) it is essential to use parenthesis
to make the meaning of arithemetical expressions transparent.


Exercise: What does a/b/c represent in Mathematica; (a/b)/c or a/(b/c)? What about
a/b*c?

Brackets: Square brackets are used for specifying arguments of functions. For example,
Floor takes a single argument, Floor[ x]. Square brackets cannot be used for grouping terms
in arithmetical functions as done in written mathematical work. Try it in a few examples to
see what happens.

In[21]:=

  2*[3+4]
  

  Syntax::sntxf: 
"2*" cannot be followed by
"[3+4]".







;[o]
Syntax::sntxf:
"2*" cannot be followed by
"[3+4]".

Braces: These are used for specifying lists. A list is an order collection of objects. Lists are
used for representing vectors, matrices and other mathematical collections. Many
functions will return lists as their result and other functions manipulate lists. List
manipulation is one of the most powerful features of Mathematica and is discussed in more
detail in a later section. Here are a few examples of their use.
We can compute the remainder obtained by the first few Fibonacci numbers using the list
manipulation features of Mathematica.

In[22]:=

  numbers= { 1,1,2,3,5,8,13,21,34};
  
  Mod[ numbers, 3]

Out[22]=

  {1, 1, 2, 0, 2, 2, 1, 0, 1}

Notice the semicolon at the end of the definition of the list named numbers above. The
purpose of the semicolon is suppress the output of the expression. Try this in a few of
your own computations.

Up to Tutorial