Introduction to Mathematica

The purpose of this lab is to introduce you to Mathematica .

What you  are now reading  is part of a Mathematica  notebook.  Notebooks are divided into cells. The square brackets on the right side of the screen separate distinct cells. For example, the paragraph above and the one you are reading are contained in separate text  cells. There are other kinds of cells: input  and output.  You can often identify an input cell by the boldface type--the five small cells directly below this paragraph are input cells.  Enter every input cell you encounter while doing a lab. You do this by clicking inside the cell and then pressing Enter  (not  Return). Before continuing, enter the next few cells, then go back and change the numbers; experiment. The best way to learn Mathematica  is to play with it.

10-12

4*13

6/2

3^2

The notebook interface allows you to group a set of cells together.  You can then manipulate the cells all at once.  In particular, you can HIDE a group of cells by double clicking on the delimeter bar at the right which indicates that  group.  This entire document is one group.  The introduction is a subgroup.  To hide the introduction, double click on the the second bar from the right.  To get the introduction back, double click on the bar again. (There will be a small arrowhead at the bottom ofof the delimeter once the group has been hidden).  Try it.

Now we'll look at what Mathematica  can do.  Each section below has had its text hidden, so you will have to double click on the bar at the right to open it befor you can read it.

Arithmetic

We have seen a few examples above. Here is another one.

4.12343/1.397+3.2689^3 - 1

Notice that the answer to this calculation is only an estimate.  To get more accuracy we specify the number of significant places we want, say 20, with the following command.  Here % refers to the output of the previous command.

N[%,20]

In general Mathematica  computes symbolically to give exact results.  If you want to have a numerical approximation then you use the special command, N[].  You can also type //N at the end of a  command to give a numerical answer rather  than a symbolic answer.

N[Pi]

If you're interested, here is p computed to 330 decimal places.  See how many you can get!

N[Pi, 330]

Algebra

Mathematica  does algebra.  We'll  do a few examples to get you started. Enter the next few cells to see what the Mathematica  commands do, then change the numbers and see what happens.

Solve[x^2-4==0, x] (*You need both equal signs.*)

FindRoot[Cos[x]-x==0,{x,.5}] (*Use FindRoot to find
 solutions to 'non-algebraic' equations.*)

Factor[x^2-4]

Expand[%] (*The % sign represents the last Mathematica output.*)

Mathematica  knows many standard functions. Note that Mathematica  functions always begin with capital letters and use square brackets instead of parentheses.

Cos[Pi/4]

Exp[10]

N[%, 20]

Sqrt[1000]//N

You can define you own functions in Mathematica..  Here are examples.

F[x_] = 2x^3 - 4x^2 + 15 (*The dash after the x tells 
                        Mathematica that x is a variable.*)

G[x_, y_] = (x^3 + 1)/(y^3 - 1)

F[-12]

G[131, -128]

When defining functions, you can use either F[x_] = ...    or F[x_] := ...   .  The definition F[x_]=.... is expanded immediately  but F[x_]:= is only expanded when it is needed.

Functions can also be defined recursively, for example:

H[0]=0; H[n_]:=n+H[n-1]

H[10]

Notice in this example that the non-colon type of definition, H[n_]=n+H[n-1], would not work.

Calculus

You can use Mathematica  to do calculus. Here's a derivative.

D[2^x, x]

Here's an integral.

Integrate[x/(x^3-1), x]

Now let's try differentiating back again.

D[%, x]

This gives the expression in a different algebraic form. We can get back our original form using  Simplify.

Simplify[%]

Here's another way to make Mathematica  differentiate.

Clear[f]   (*We're clearing any previous assignment yo x*)

f[x_]:= x^3

f'[x]

Many integrals do not have a "closed form solution".  If you give Mathematica  such a definite integral it will be returned unevaluated. You can still use N to get a numerical answer.

Integrate[Sin [Sin[x]], {x, 0, Pi}]

N[ % ]

Mathematica  can also solve differential equations. Here is an easy differential equation solved first in general and then with initial conditions.

DSolve[y''[t] == -y[t], y, t]

DSolve[{y''[t] == -y[t], y[0] == 5, y'[0] == 1}, y, t]

It is a mathematical fact that most differential equations do not have an explicit symbolic solution.  In these cases you can get a numerical approximation to the solution using NDSolve.  The interested reader should see page 7 of Wolfram's Mathematica  reference or refer to the example below:

NDSolve[{y''[x] + Sin[x]^2 y'[x] + y[x] == Cos[x]^2,
     y[0] == 1, y'[0] == 0}, y, {x, 0, 20}]

Plot[ Evaluate[ y[x] /. %], {x, 0, 20} ]

Plotting

Mathematica  plots the graphs of functions.

Plot[x^2-4, {x,-5,5}]

You should verify that the x-intercepts correspond to the solutions you found above.

Mathematica  knows many standard functions. Note that Mathematica  functions always begin with capital letters and use square brackets instead of parentheses.

Plot[{Sin[x], Log[x], Exp[x], Sqrt[x]},{x,0.01,4}]
(*Exp[x]=ex; Log[x]=ln(x); Sqrt[x]=Üx*)

Plot[Sin[x], {x, 0, 2Pi}]

Plot3D[x^4 - Sin[5 x] y^4 , {x, -1, 1}, {y, -5, 5}]

Useful Tips

1. If you forget how a particular Mathematica  function works, Mathematica  has on-line help. Type ? followed by the command.

?Plot

2. You can create your own input cells. For example,  use the mouse to position the pointer just below this text cell and above the next one (the pointer should become a horizontal bar). Click the mouse. You should get a horizontal line across the screen. Now type in 5+10 and hit enter.

3. If you don't remember the syntax for a particular command, Mathematica  can still help. Type the command. Press and hold down the apple key,  then, while still holding down the apple key, press the "i" key . You will get a template. As an example, create an input cell just below this one (as described above). Type Plot. Then press the apple key followed by the "i" key.

4. The Function Browser is very nice. Here's how you get to it.
Find the menu at the top of your screen; it starts with File and continues across the top of the screen. Click and hold on Help. A vertical submenu will appear. Drag the pointer to Open Function Browser...and release. Inside  the Function Browser you will see a vertical menu on the left side. Click on Algebraic Computation. A new menu will appear to the right. Click on Equation Solving. Another menu will appear to the right. Click on Solve. 
That's how the Function Browser works: You move from left to right, clicking as you go.