An Introduction to Mathematica (MM)

Gerald Farin & Dianne Hansford
January 2015

This is a sample Mathematica notebook to help you get started.
Much of the functionality you will need in your classwork is included here.

Cells:
Cells are the building blocks of a Mathematica notebook. 
A cell is indicated by the brackets on the right hand side of the notebook. 
The default cell type is for executable code. 
To format this cell for text, we click on the cell’s bracket and select Format ->Style->Subsubtitle

Kernel
The Kernel evaluates code in a notebook on a cell by cell basis.
To evaluate the cell below, Place the cursor in the cell below and press Shift-Enter.
You will find the command under Evaluation->Evaluate Cells. 
Also try Evaluate Notebook -- this evaluates all cells with executable code. 
(If MM won’t stop evaluating because of an error, stop it with Evaluation ->Quit Kernel.)

(* Some simple code *)
a = 3;
b = 2
Print["Hello World!  a=",a];

In the code above, observe the following.
-- A semicolon after input supresses output.
-- Note the structure of Print. Use often to avoid a silent program!
-- An in-code comment is formed with (* ... *)

Let’s play a little to get a feeling for the input format.

Plot[Sin[x],{x,0,20}]

If you don' t understand a command, use Help -> Function Navigator or Documentation Center to learn more about the input parameters. Go look up Plot.

Notice that MM routines begin with capital letters. Therefore, it is best if your variables and routines do not start with a capital letter.

Manipulate[Plot[Sin[c*x], {x,0,10}],{c,1,10}]

Keep in mind that MM is very picky about notation; case matters, the type of bracket (square or round), etc. Variables are colored differently than pre-defined functions.
 
The Help pages are notebooks, so you can copy the commands from it into your own notebook.

Sqrt[81]
sqrt[81] (* this does not calculate the square root of 81 *)
v1 = 2;
v2 = 3;
v3 = v1 + v2
(* See how confusing the output gets when there isn't any text with the output. Try the following *)
Print["v3 = v1 + v2 = ", v3]

Create a new cell by moving the mouse until the cursor becomes a horizontal bar and then click.

Merge cells by selecting their brackets and using Cell -> Merge Cells. Try merging this cell with the previous one.
   
You can learn more about the basics of notebooks in Document Center' s "Notebooks and Documents" --> Notebook Basics. Create your own notebook and stylize it the way you like.

3D Plotting  
Notice that the surface below looks very faceted as you move the slider, but when you leave the slider in one place for several seconds, the surface becomes smoother. 
Rotate the object by putting the cursor in the window with the surface, left-mouse click, and and move the mouse.
Zoom: cntrl and left-mouse click
Translate: shift and left mouse click

Manipulate[Plot3D[Sin[c*(x+y)],{x,-3,3},{y,-2,2},Mesh->None], {c,1,5}]

Functions

(* define a functions f and g. Notice the := notation *)
(* //N : forces output of a numerical expression  -- try removing it *)
f[x_]:= Sin[x^2-Cos[x]]+x^2//N;
g[x_]:=3x;
f[2]

Plot[{f[x],g[x]},{x,0,10}]

Subroutines
All the variables are global.
If you want to specify a local variable, you will need the command Module.

myroutine[s_, t_] := (
Print["myroutine: input s=",s,"  t=",t];
	v = s + t;
	q = s * t;
(Return[v];
);

v = 0;
q = 0;
s = 1;
t = 2;
returnval = myroutine[s,t];
Print["returnval = ",returnval,"   v=",v,"  q=",q];

Experiment with myroutine by 
-- removing the Return statement
-- removing the semicolon on the q = s*t statement

Lists andTables 
We will use lists and tables a lot in this class. Sometimes we will refer to them as arrays.

tt={2,3,2,6};
Print["tt = ",tt,"    and tt in matrix form = ",MatrixForm[tt]];

ss=Table[i^2,{i,1,10}];
Print["ss = ",ss,"    and ss in matrix form = ",MatrixForm[ss]];
ListPlot[ss]

By default, listplot plots over the integers 1,2,3,...

Look up ListPlot in Help and modify the style of the plot.
-- add list tt to the plot
-- join the points with a line
-- change the color and point size (using PlotStyle)

We will work with points and vectors

points3d = Table[{i,10+i,-i},{i,1,5}];
Print["points3d = ",points3d,"  in matrix form: ",MatrixForm[points3d]];
(* Access individual elements *)
Print["first point = ",points3d[[1]]," and its first coordinate = ",points3d[[1,1]]];

cc=Table[{i,j,4},{i,1,3},{j,3,4}];
Print["cc = ",cc," and cc in matrix form = ",MatrixForm[cc]];

(* Access individual elements *)
Print["cc[[1,1]] = ",cc[[1,1]],"     cc[[1,1,1]=",cc[[1,1,1]]];

Curves

(*parametric curve: circle *)
curve[t_]:={Cos[t],Sin[t]}; 
ParametricPlot[curve[t],{t,0,Pi}]

curve2[t_]:={t*Cos[t],t*Sin[t]};
ParametricPlot[curve2[t],{t,0,4Pi}]