Beginning Mathematica

Mark Senn

December 5, 2006

Wolfram Research's Mathematica is the world's only fully integrated environment for technical computing.  It does symbolic mathematics, numeric mathematics, graphics, and interactive notebooks (you're reading one now).  There is a separate gridMathematica product for using Mathematica on clustered computers, webMathematica for using Mathematica over the web, and a slew of application packages and programs.

Mathematica is built on the powerful unifying idea that everything can be represented as a symbolic expression.  For example, x  +  y is represented internally as Plus[x,y], {x, y, z} is represented internally as List[x,y,z], etc.  In Mathematica there are few concepts but they are used many different ways.

Mathematica is a very comprehensive software package.  This document just demonstrates a tiny fraction of Mathematica and is meant to help people get started using it.

Table of Contents

Interfaces
Help
Notebooks
Numeric Mathematics
Symbolic Mathematics
Graphics

Interfaces

Mathematica comes with two interfaces: math and mathematica.  I recommend people using Mathematica interactively use the mathematica interface and any other programs that use Mathematica non-interactively use the math interface.
The rest of this document is only applicable for the mathematica interface.

Help

For Mathematica help see Mathematica's on-line help system (click on the word ``Help'' on the top part of Mathematica window), or see Wolfram Research's Documenation Center.

Notebooks

Normally, when Mathematica starts up everything you type is in an "input" cell and when you type "Shift Enter" Mathematica evaluates what you type.  If you type input followed by ";" Mathematica evaluates it but doesn't print the result.

You may be wondering how stuff like this paragraph was input.  This is a "text" cell.  Don't worry if this doesn't make much sense now, the important ideas are the appearance of a notebook is controlled by a style sheet and notebooks consist of cells.  Notebooks are all represented internally in Mathematica with Notebook[…].  Sometimes notebooks are described after everything else in Mathematica training...I think it is better to at least have the information here so you know where to find it later.

Style Sheets

Notebooks are stored internally in Mathematica using the generic Notebook[…] function.  The style used to display a notebook is controlled by the style sheet.  This notebook uses the "Textbook" style sheet (Format => Style Sheet => Textbook).

Cells

Notebooks consist of cells.  There are

cell types.  This subsection starts with a "Cells" Subsection cell, followed by a Text cell, and then (immediately above) an IndentedText cell.  Input cells are evaluated by Mathematica.
Cell structure is shown using the blue brackets on the right hand side of the window.  Cells can be nested.  Cell types and cell structure can be changed.
To delete a cell left click on the cell bracket and type Back Space.
To change cell type left click on cell bracket and do Format => Style => ….
To combine multiple cells to make one, left click on first cell bracket, shift-left-click on other cells to combine, and then do Cell => Merge Cells.
To divide an existing cell into two, left click in the text where you'd like to divide a cell and then do Cell => Divide Cell.

Making External Links

To make a link like this one to abbymath.com I did the following:

Making Internal Links

Ta make a link like this one to Numeric Mathematics (the next section) I did the following.  First set up the link:

after putting in the link target cell, in this case the "Numeric Mathematics" cell, do

Numeric Mathematics

Let's have Mathematica add 2 + 2.  I typed "2 + 2" followed by Shift-Enter in an input cell to do that.  If you just type Enter, Mathematica won't do anything so be sure to type Shift-Enter when you want Mathematica to evaluate something.

2 + 2

Sqrt[50 + 50]

Mathematica can do numeric mathematics with an arbitrary number of digits:

N[2 1/3, 20]

N[Pi, 1000]

Random Numbers

Pseudorandom (from now on just called random) number between 0 and 1:

Random[]

Return 0 or 1 with equal probabilty:

Random[Integer]

Random integer between 1 and 12 inclusive:

Random[Integer, {1,12}]

Throw two six sided dice:

Random[Integer, {1,6}] + Random[Integer, {1,6}]

Range

Return all numbers in a range:

Range[1, 5]

Range[3, 5]

Range[3, 5, 0.2]

Sets

a = {w, y, z};
b = {x, y, z};
Union[a, b]

Intersection[a, b]

Complement[a, b]

Statistics

data = {2, 3, 5, 17, 19, 31}

Mean[data]

"%" gets replaced by the last evaluated result.

N[%]

Of course, one can use this:

N[Mean[data]]

Variance[data]

StandardDeviation[data]

Median[data]

Quantile[data, 0]

Quantile[data, 0.5]

Quantile[data, 1]

Sum

Add 1 + 2 + 3.

Sum[i, {i,1,3}]

Using a Package

Tim Montgomery set a world's record by running 100 meters in 9.78 seconds.  What was his average speed in miles per hour?
The <<Miscellaneous`Units` below reads in a file with some definitions that aren't part of core Mathematica.  Do Help => Help Browser => Add-ons & Links and type "Units" in the dialog box for more information about the Miscellaneous`Units` package.

<<Miscellaneous`Units`
Convert[(100 Meter)/(9.78 Second), Mile/Hour]

Symbolic Mathematics

x = 3; y = 5;
x + y

Differentiation

Clear[] variables in case they have values left over from earlier.  Solving for knowns (instead of unknowns) causes weird problems and confusion.  If you get really weird results when solving for unknowns double check that they don't already have a value.

Clear[a, b, x];
expr = 2 a x + b;
D[expr, x]

Integration

Integrate[expr, x]

Matrix and Vector Operations

Clear[a, b, c, d, x, y];
(* matrix *)
m = {{a, b}, {c, d}};
(* vector *)
v = {x, y};

(* constant times matrix *)
k m

(* square each element of m *)
m^2

(* matrix multiplication *)
m . m

(* matrix times vector, v acts as column vector *)
m . v

(* vector times matrix, v acts as row vector *)
v . m

(* transpose matrix *)
Transpose[m]

Solving Equations

Clear[x, y];
Solve[{x + y == 10, 2x - y == 5}, {x, y}]

Solve[{a x^2 + b x + c == 0}, x]

Do the a, b, and c replacements on the fly just for this one statement.

% /. {a->1, b->5, c->4}

Set a, b, and c until they are reset.

a = 1; b = 5; c = 4;

Solve[{a x^2 + b x + c == 0}, x]

Reset b and c.

b = 10; c = 9;
Solve[{a x^2 + b x + c == 0}, x]

Reset just c.

c = 8;
Solve[{a x^2 + b x + c == 0}, x]

N[%,20]

After the following input is evaluated you may want to left click on the output cell bracket and do Cell => Convert To => and then InputForm, OutputForm, StandardForm, or TraditionalForm to see the different ways the output can be displayed.

Clear[a, b, c, d, e];
Solve[{a x^4 + b x^3 + c x^2 + d x + e == 0}, x]

Graphics

Plot y = sin x  for 0  ≤  x  ≤  
                2  π.

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

Plot y  =  cos x  for 0  ≤  x  ≤  
              2  π.

p2 = Plot[Cos[x], {x,0,2Pi}]

Show both the plots on the same set of axes.

Show[p1, p2]

Make a set of plots.  After the plots are displayed left click on the cell bracket enclosing the set and do Cell => Animate Selected Graphics to make a movie of the plots.  Handy shortcut: double clicking on any graphic in a grop of cells starts the animation.  To stop the animation hit any key.

Table[
    Plot[Sin[x-t], {x,0,10}],
    {t, 0, Pi, Pi/10}
]

Do a 3D plot.

Plot3D[Sin[x] Cos[y], {x,-Pi,Pi}, {y,-Pi,Pi}]

Make a set of 3D plots to do an animation.

Table[
    Plot3D[
        Sin[t+x] + Cos[t+y],
        {x,-Pi,Pi},
        {y,-Pi,Pi},
        Axes->False,
        Boxed->False
    ],
    {t,-Pi,Pi,Pi/10}
]

Compare throwing one twelve-sided die with two six-sided dice.

(* do this many throws *)
n = 1000;

(* make lists with the dice throw values *)
die = Table[Random[Integer, {1,12}], {n}];
dice = Table[
    Random[Integer, {1,6}]
        + Random[Integer,{1,6}],
    {n}
];

Make some plots showing the individual dice throws.

pdie  =  ListPlot[die,  PlotStyle -> Red]

pdice  =  ListPlot[dice,  PlotStyle -> Blue]

Put both plots on the same axes.

Show[pdie,  pdice]

Add individual dice throws to get total distribution.

tdie  =  Table[0,  {12}];
tdice  =  Table[0, {12}];

Do[ tdie[[die[[i]]]]++,  {i, 
          n}];
Do[ tdice[[dice[[i]]]]++,  {i, n}];

tdie  =  tdie / n;
tdice  =  tdice / n;

pdie  =  
      ListPlot[tdie,  PlotJoined -> True,  
        PlotStyle -> Red]

pdice  =  
      ListPlot[tdice,  PlotJoined -> True,  
        PlotStyle -> Blue]

Show[pdie, pdice]