Mathematica lesson 2:
 Using Mathematica as a scientific calculator.

Author's notice

This tutorial was written by 

Fred Goodman
Department of Mathematics
University of Iowa
Iowa City, IA 52242
goodman@math.uiowa.edu

This material may be freely copied and used for non-commercial purposes. Please retain this author's notice, and do not distribute any altered version of this material without the author's permission  (unless  the material is altered beyond recognition).

You can use Mathematica  first of all as an advanced scientific calculator,  
more or less right out of the box.  You will need to know a little basic syntax.  Double click on the following closed cell groups to read more.

Typing mathematical expressions

Addition and subtraction are indicated with + and -.  

Division is indicated with /.  

Multiplication is indicated with *, or just by putting two expressions next to one another with a space separating them.   You have to be careful to leave the space, however: 
x x  
is interpreted as x times x, 
but 
xx 
is interpreted as a new variable called xx.

10 10 x x
10 10 xx

Powers are indicated by ^.  x^2 means x squared.

x^2 x^5

The basic operations are performed  by Mathematica in the following order of priority:
1)  ^   (power)
2)  *  (multiplication)
3)  /   (division)
3)  +  (addition)
4)  - (subtraction)

Expressions can be  grouped with round parentheses in order to impose the desired order of operations. When in doubt, use parentheses to group expressions!

3  x +  y/a + b

(3  x  +  y)/(a + b)

3  +  7^2

(3 + 7)^2

You can also enter expressions using the input pallete, a grey box with a lot of funny symbols on it which is floating somewhere on your computer screen.  For example to type a fraction, you can  click on this fraction symbol in the pallette  (□/□)  (while your cursor is in an input cell).  This will place the symbol (□/□) in your input cell.  Now click on the numerator box and type whatever expression you like, for example 3x +7; then click on the denominator box and type any other expression you like, for example
2x + 4.  Here is what you get:

((3  x  +  7)/(2  x  +  4))

Try opening up an input cell and typing the expression.  ((4  x^2  +  3  x  +  5)/(2  y  +  z))

Instead of entering powers in the form x^3,  you can also enter powers by using the symbol (□^□) from the input palette.  Click on the first box and and fill in x, and click on the second, little box and fill in 3.  Here is what you get:

(x^3)

Similarly, you can enter square roots by using the (Sqrt[□])  from the input palette and other roots by using (□^(1/□)).    You can also type various Greek letters by clicking on them in the input pallette, like π.   You can type  (π/2)by using the fraction symbol and the symbol π from the pallette, or (Sqrt[α β]) by using the (Sqrt[□]) symbol from the pallette and then the Greek letters α and β.

You can get a prettier form of expressions which you type by clicking on the bracket of your input cell and then choosing 
Cell -> Convert To -> Standard form
from the main menu bar.  For example, if you have entered the following (making use of the fraction bar (□/□))

((4  x^2  +  3  x  +  5)/(2  y  +  z))

and you do the conversion as described, you get

((4 (x^2) + 3 x + 5)/(2 y + z))

Similarly, if  you have entered

(4  x^2  +  3 x  +  5)/(2  y  +  
          7)

and you do the conversion, you again get

((4 (x^2) + 3 x + 5)/(2 y + 7))

You can get expressions involving summation by using \(TraditionalForm\`∑\+\(□ = □\)\%□ □\)  from the input pallette.  Try writing this:      (∑_(i = 1)^10) (i^2) in an input cell and evaluating it:

(∑_(i = 1)^10) (i^2)

Numerical results

Mathematica will do exact rational arithmetic, or leave expressions in symbolic form, also sometimes when you don't expect this!

Sin[2]
(3/373)  +  (5/182)

To get (approximate) numerical results, you use the function N[ ].

N[Sin[2]]
N[(3/373)  +  (5/182)]

If an expression already involves a "floating point" number, the result will be given  numerically.

Sin[2.0]

The following calculates the sum of the reciprocals of the first 100 integers. You can see that sometimes exact rational arithmetic can be a little cumbersome!

(∑_(n = 1)^100) (1/n)
N[(∑_(n = 1)^100) (1/n)]

You can determine the number of decimal places displayed in numerical computations as follows.  The default is 6.

1000 N[(∑_(n = 1)^100) (1/n), 20]
N[1000 (∑_(n = 1)^100) (1/n), 100]

The symbox % stands for the last computed result.  It is convenient for doing further computation on the result of the previous computation

1000  (∑_(n = 1)^100) (1/n)
N[%, 20]
Sin[%]

Functions

Application of functions is always indicated by square brackets, and built-in Mathatica functions are always capitalized. For example you have to write Sin[x], and not sin(x).

Sin[π/4]
Log[10]
N[Log[10]]
Log[10.0]

Common Mathematical Constants

The  ratio of the diameter of a circle to twice the radius is denoted Pi or π. The base of the natural logarithm  is denoted E or e. The complex square root of -1 is denoted I or i.

N[π,  20]
N[E, 20]
Solve[(x^2) + x + 2 == 0, x]

Common Mathematical Functions

The triginometric functions are
 
Sin[ ], Cos[ ], Tan[ ], Cot[ ],  Sec[  ],  Csc[ ]

N[Sin[(π/7)]/Cos[(π/7)]]
N[Tan[(π/7)]]

The natural exponential function is Exp[x],  or E^x.

Exp[2.2]
(E^2.2)

Log[  ]  denotes the natural logarithm  (base E).

Log[10. ]

You can get information on any built-in Mathematica function by using  the ?  operation.

? Log

You  can see from this that to get the common logarithm (base 10), you use Log[10, x].

Log[10, 1000]

Abs[ ]  gives absolute value.  Sqrt[x]  or x^(1/2)  or (Sqrt[x]) gives square root.
 Factorial[n]  or n!  gives the factorial of an integer.  

The inverse trigonometric functions are denoted by ArcSin[  ],  etc.   The ? operation does not give any information on the range of the functions, so you'll have to  resort to experimentation or the printed documentation.

?ArcSin

ArcSin[Sin[((3 π)/4)]]
ArcSin[Sin[((5 π)/4)]]

Parentheses, brackets, and braces

In usual mathematical notation, parentheses, brackets and braces may sometimes be interchanged.  In Mathematica, they have very specific uses.

Parentheses (...)  are always used for grouping expressions, never for other purposes.

Brackets [..]   are always used for function application, never for other purposes.

Braces {...}  are used for making lists, never for grouping expressions.  Notice the output of the following expression.

mylist  =  
      Table[i^2,  {i,  1,  10}]

Double brackets [[..]]  are used to pick out an element of a list.

mylist[[4]]

Defining your own functions

For defining your own functions, you must use a very precise syntax:
for example to define a function called f[x] by the formula  2x - x^3, you write:

f[x_]  :=  2  x  -  x^3

Notice the underline next to the variable on the left side, and the colon followed by =.  You must use both of these features.  Once the function is defined, you can evaluate it on arbitrary input, graph it, etc.

f[3]

    f[-5.298765]

Let's draw the graph of the function.  The x interval from -3 to 3 contains all the important features of the graph:

Plot[f[x],  {x,  -3, 3}]

Numerical (approximate) calculation of the derivative.

Let's start by defining a function in Mathematica.

f[x_]  :=   3  x^2  +  
        x^3

Let's compute some average rates of change over some intervals beginning at x = 3.

\(\(f[3 +  .001] - f[3]\)\/ .001\)

\(\(f[3 +  .0001] - f[3]\)\/ .0001\)

\(\(f[3 +  .00001] - f[3]\)\/ .00001\)

In the following example, Mathematica does not display as many digits as it has accurately computed.  You can force it to display the answer with full computed accuracy by using InputForm[..].

h =  .000001
((f[3 + h] - f[3])/h)
InputForm[((f[3 + h] - f[3])/h) ]

Exact computation of the derivative

Mathematica still remembers the definition of the function f since we haven't done anything to remove the definition.

f[x]

I want to use h as a variable quantity again.  At the moment, Mathematica has memorized a value for h, namely h = .000001.

h
f[3 + h]

To get rid of this memorized value, we tell Mathematica to execute the command Clear[h]:

Clear[h]
h
f[3 + h]
Expand[%]

Here is a general expression for the average rate of change over the interval from 3 to 
3 + h, expressed in terms of the variable h.

((
f[3 + h] - f[3])/h)
Simplify[%]

What happens to this expression as h gets close to zero?  What is the exact derivative of the function f at x = 3?