# Mathematica at WWU

## notes by Branko Ćurgus

Mathematica at WWU

These notes are written for
which is available on most campus computers. This is an old, but still powerful, version of this software.
• We also have
which is available in BH 215. This is the current version of this powerful computer algebra system. Only minor changes might be necessary for the commands below to work in Mathematica 8.

• Getting started

• You need to find a campus computer with Mathematica installed on it (for example BH 209, BH 215). You will find Mathematica as follows (this sequence might differ on different campus computers)
Start -> Programs -> Math Applications -> Mathematica 5.2
• When Mathematica starts up, it gives you a blank Mathematica file. A Mathematica file is called a notebook. As you start writing in a blank notebook Mathematica forms a cell; a thin blue frame will appear on the right of the notebook window. This is a cell bracket.
• Start writing something simple, 2+2 for example. After writing 2+2, hold down the Shift key and press Enter (this is abbreviated as Shift+Enter). This tells Mathematica to process your input. The result will appear in an output cell. Notice that the thin blue brackets of the input and output cells have differently designed tops.
• To add a new cell place the pointer in the notebook window below the last cell. Make sure that the pointer becomes a horizontal I-beam. Then click and a cell insertion bar will appear. Only then start typing. By default, new cells are input cells. To change the style of a cell: click the cell bracket; this highlights the bracket; then choose style from the menu item Format -> Style

It is essential that there is no text in input cells. Text and commands cannot be mixed.

Brackets in Mathematica

Before presenting a few examples of how to use Mathematica it is important to notice that there are five types of brackets in Mathematica:
• ( ) -- parentheses or round brackets -- for grouping terms --   ((E^(I Pi)) + 1)
• [ ] -- square brackets or box brackets -- for arguments of functions --   Cos[Pi/5]
• { } -- braces or curly brackets -- use them to put things together in a list --   {1, 0, Pi, E, I}
• [[ ]] -- double square brackets -- use them with lists --   {1, 0, Pi, E, I}[[4]]
(The double square brackets will appear nicer if you type   Esc[[Esc   and   Esc]]Esc.)
• (* *) -- starred parentheses -- for comments in Mathematica code --   (* this is ignored *)
Each example below uses some (or all) of these brackets. Please pay attention and make each example a learning experience.
What to do if things go wrong?

Eventually things will go wrong. When they do go wrong, there are 5 ordered actions that you can do.
1. Abort evaluation in the Kernel menu; the keyboard shortcut is Alt+.
(This means: hold down the Alt key, then press the . key shortly, and release the Alt key.)
2. Quit kernel in the Kernel menu,
3. Activate the Windows Task Manager by the keyboard shortcut Ctrl+Alt+Del, then highlight Mathematica Kernel in the Applications list and click End Task.
4. Exit Mathematica in the File menu; the keyboard shortcut is Alt+F4
5. Activate the Windows Task Manager by the keyboard shortcut Ctrl+Alt+Del, then highlight Mathematica in the Applications list and click End Task.

Using Mathematica

• You can use Mathematica as a calculator. For example to get the circumference of a circle circumscribed about a square with side 1, you type

Pi*2^(1/2)

and Shift+Enter. To get a numeric approximation type

N[Pi*2^(1/2)]

and, as always, Shift+Enter.
• Mathematica has hundreds of built in functions. Function names are always capitalized, for example the standard ones

Exp[x], Log[x], Sin[x], Cos[x], Abs[x], ...

or some which are for some mysterious reason absent from our curriculum:

Sign[x], Floor[x], Ceiling[x], UnitStep[x], ...

Help -> Built-in Functions -> Mathematical Functions
• I view all Mathematica commands as functions with several variables. Some examples are below.
• To plot one period of the sine function

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

Here Sin[x] is one variable, and the domain {x,0,2*Pi} is another variable. But this function can have many more variables. Options[Plot] will give you all.
• Plotting two functions with several options:

Plot[
{Sin[x],Cos[x]}, {x,0,2*Pi},
PlotStyle->{{Thickness[0.007],Blue},{Thickness[0.007],Green}},
PlotRange->{-2,2}
]

• Plotting a function of two variables:

Plot3D[ Sin[x]*Cos[y], {x,0,2*Pi}, {y,0,2*Pi} ]

plots a function of two variables.
• Find a derivative

D[Sin[3 x]^2 + Cos[4 x], x]

finds the derivative with respect of x.
• Simplify[D[Sin[3 x]^2 + Cos[4 x], x]]

finds an algebraically simpler form of the previous answer. Sometimes FullSimplify[] gives more desirable results.
• Integrate[-4 Sin[4 x] + 3 Sin[6 x], x]

finds an antiderivative.
• Table[Sin[x + s],{s, 0, 2 Pi, Pi/20}]

produces a list of forty-one shifts of the sine function.
• To plot all the functions from the previous item we need to use Evaluate[]:

Plot[ Evaluate[Table[Sin[x + s],{s, 0, 2 Pi, Pi/20}]], {x, 0 , 2 Pi} ]

• Notice that exchanging the positions of Plot[] and Table[] gives a different but useful result:

Table[ Plot[Evaluate[Sin[x + s]],{x, 0 , 2 Pi}], {s, 0, 2 Pi, Pi/20} ];

Also notice ; at the end of the input above. This suppresses Mathematica telling us that the outputs are Graphics cells.
• Often it is useful to define our own functions. It is a good practice to clear the name that you use for the function and the name of the variable before the definition:

Clear[ff,x];
ff[x_]:= Exp[(x^2)(Cos[x]-1)]

But much more complicated functions are common. For example functions that include graphing commands.
• Notice the use of semicolon (; ) in a cell. Its purpose is to separate different commands used in the same cell. Contrast this with the use of comma (, ) which is used to separate variables in a command which takes several variables.
• Also notice the use of colon (: ) in the definition of function. For the meaning of := check SetDelayed in the help file. Basically, if you use :=, then Mathematica will remember the formula and evaluate it only when we use the function later on. I prefer to use := in definitions of functions, in particular complicated ones. Often, like in the simple example above, := and = are interchangeable. However, this is not always the case. Here is one example in which = yields a meaningful function while := does not make sense: Say I want to graph three random lines with y-intercepts and slopes in the interval [0,1]. I can define

Clear[f1, f2, f3, x];
f1[x_] = Random[] + x*Random[];
f2[x_] = Random[] + x*Random[];
f3[x_] = Random[] + x*Random[];

Now you are thinking that I defined the same function each time. Try graphing:

Plot[
{f1[x],f2[x],f3[x]}, {x,-5,5},
PlotStyle->{{Thickness[0.007],Red}, {Thickness[0.007],Green}, {Thickness[0.007],Blue}},
PlotRange->{-5,5}, AspectRatio->Automatic
]

and you will see that these are different functions since in each definition Mathematica assigned different random numbers as slopes and y-intercepts. I am writing all of this to point out that

Clear[g,x];
g[x_]:= Random[] + x*Random[];

does not make sense! With this definition Mathematica selects random numbers as slope and y-intercept whenever you calculate, for example, g[1]. Try evaluating it twice. You get different numbers! Thus, as defined, g[x] is not a function.
• The sinc function is not in Mathematica's library of functions. (In fact, it has been added in version 6.0.) To define it set

Clear[Sinc,x];
Sinc[x_]:= If[x==0, 1, Sin[x]/x]

Clearly Sinc is not a bijection. However, in the spirit of inverse trigonometric functions one can define ArcSinc[x] for certain real values of x. This is a nice exercise is using Interpolation[] function in Mathematica.
• By default Mathematica warns us of similar spelling. I do not find this useful. Therefore I switch this feature off by Off[General::"spell"]. I just copy the first part of the warning message, that is General::"spell" and paste it into Off[] command.

• Functions whose domain is the set of positive integers can be defined recursively. Here is an example. Why I choose this long name Repeat will be clear after you define and explore this function.

Clear[Repeat, n];
Repeat[1] = 1;
Repeat[n_]:= Repeat[n - Repeat[n - 1]] + 1

But much more complicated functions are common. For example functions that include graphing commands.
• If you try to calculate Repeat[100] you will notice that Mathematica takes quite a long time to calculate this number. Try

Timing[Repeat[60]]

On my computer it takes 2.735 seconds to calculate that Repeat[60] = 11. The reason for this is that as defined above the function Repeat[] does not remember any values that it calculates. We can make Mathematica remember all the function values it finds by changing the syntax in the definition:

Clear[Repeat, n];
Repeat[1] = 1;
Repeat[n_]:= Repeat[n] = Repeat[n - Repeat[n - 1]] + 1

You will easily notice that this definition is much faster.
• Test what this function does using

Table[Repeat[k], {k, 1, 106} ]

or by plotting its graph (which consists of points with integer coordinates)

Show[
Graphics[{
{PointSize[0.015], Table[Point[{k, Repeat[k]}], {k, 1, 106}]}
}],
PlotRange -> {{0, 107}, {0, 15.5}},
Frame -> True, ImageSize -> 400
];

But sometimes we want to control more details of the plot:

Show[
Graphics[{ (* the start of the graphics, it is a list *)
{ (* the grid lines are in this list *)
Thickness[0.003], GrayLevel[0.8],
Table[Line[{{0, j}, {120, j}}], {j, 1, 15}],
Table[Line[{{j(j + 1)/2, 0}, {j(j + 1)/2, 16}}], {j, 1, 15}]
}, (* the next list are the points on the graph*)
{PointSize[0.015], Blue, Table[Point[{k, Repeat[k]}], {k, 1, 106}]}
}], (* the end of the graphics *)
PlotRange -> {{0, 107}, {0, 15.5}},
Frame -> True,
FrameTicks -> {Table[j(j + 1)/2, {j, 1, 15}], Range[1, 15], {}, Range[1, 15]},
ImageSize -> 400
];

Please notice the indentation of the parts of the command. That helps me keep me identify the code for each part of the picture. You can copy-and-paste (^C then ^V) this gray box into your Mathematica notebook.
• I used the Repeat[] function above as an illustration for a recursive definition. But there are explicit formulas that define the same function. Such formulas are called "closed form expressions" for a recursive sequence. Here are few closed form expressions for Repeat[]:

Clear[Rf1, Rf2, Rf3, n];
Rf1[n_]:= Round[Sqrt[2*n]];
Rf2[n_]:= Floor[(1/2) + Sqrt[2*n]];
Rf3[n_]:= Ceiling[(-1/2) + Sqrt[2*n]];

The following table is a quite convincing evidence of the above claim:

Table[
{k, Repeat[k], Rf1[k], Rf2[k], Rf3[k]},
{k, 1, 100}
]//TableForm

• I just want to mention that the determining a closed form expression for a recursive sequence is a hard problem. For example, the creator of Mathematica Stephen Wolfram in 2002 introduced the following slight modification of the Repeat[] sequence:

Clear[W, n];
W[1] = 1; W[2] = 1;
W[n_]:= W[n] = W[n - W[n - 1]] + 2

No closed form expression for this sequence is known. This is no longer true. On February 10, 2011 I found the following closed form expression for the above sequence

Clear[Wf1, n];
Wf1[n_]:= 2*Floor[Sqrt[n]]-(-1)^(n+Ceiling[Sqrt[n]])