Mathematica tips

Dan Dill

Mathematica is a comprehensive tool for mathematical analysis and exposition. Here are some tips on using Mathematica to document explorations, and an example document fragment.

Tips

Sectioning a Mathematica document

Mathematica provides the following hierarchy of cell types that are useful to organize a document: Title, Subtitle, Subsubtitle, Section, Subsection, and Subsubsection. These are selected from the Toolbar, from the Style… list (see below), or with Alt1, Alt2, Alt3, Alt4, Alt5, and Alt6, respectively. This document is organized using these.

As I have done here, be sure to give each of your documents a meaningful title, indicate that you wrote it, and include a brief description of the point of the document.

You can specify headers and footers for your documents using the File → Palettes → OpenAuthorTools, selecting SetPrintingOptions from the AuthorTools palette, and then selecting Edit Headers and Footers…. That is how I have set the headers and footers for this document.

Key parts of a Mathematica document

A useful way to think of Mathematica documents is that they are composed of text, calculations, and mathematical expressions.

Text

Text is entered in a Text cell. The way to start a Text cell is to move the cursor to an empty area, indicated by the cursor turning horizontal, specifying a Text cell by holding down the keys Altalt and 7 together, and then typing in the text. Alternatively, you can use the Toolbar to specify that the cell type is Text.

Calculations

Expressions to be evaluated by Mathematica are entered in Input cells, This is the default cell type, and so to enter an expression, just move the cursor to an empty area, indicated by the cursor turning horizontal, and start entering the expression to be evaluated. You tell Mathematica to evaluate the expression by holding down together the keys ⇧ ⏎. The result will be display immediately following, as an Output cell. For example,

Sin[4 π x]/x  /. x -> 1.34

By the way, in this expression, /.x->1.34 means "everywhere there is an x, replace it by the value 1.34, and then evaluate the resulting expression. I find this useful when I want to explore the value of an expression for different values of variables.

Mathematical expressions

Mathematical expressions can be entered in in a DisplayEquation cell; the advantage of the DisplayEquation cell is it is formatted more nicely when printed. As example, say we want to enter the expression

f(
        y) = (∫_(-∞)^y) g(
            x) dx.

First, write the expression using Mathematica's rules for calculation input, but don't use ⇧ ⌤, since we do not want to evaluate the expression.

f[y] = Integrate[g[x], {x, -∞, y}]

Next, select the cell bracket and then use Ctrl ⇧ t to convert the expression to traditional mathematical notation.

f(
        y) = (∫_(-∞)^y) g(
            x) \[DifferentialD]x

Finally, select the cell bracket and then convert the cell style to DisplayFormula. Do this either by right clicking at the cell bracket, selecting Style…, and then selecting DisplayFormula, or by selecting DisplayFormula from the drop down list on the Toolbar. The final result is

f(
        y) = (∫_(-∞)^y) g(
            x) dx

Tools

Special characters can be entered in several ways. For example, the Greek letter β can be entered with the sequence Esc b Esc, the Greek letter Ψ can be entered with the sequence Esc Psi Esc, and so on. The palette of these characters and all of the others is available with File → Palettes → CompleteCharacters.

The palette BasicTypesetting is helpful for entering in mathematical expression.

Mathematica contains a spell checker. This is accessed with the keys Alt ;. Please use this to help ensure that documents are free of spelling errors.

Everything these is to know about Mathematica is contained in its online Help file. However, I find the text by Heikki Ruskeepää, Mathematica Navigator, 2e, 2004, ISBN 012603642X,

http://www.amazon.com/exec/obidos/ASIN/012603642X/dandillcom/

to be a much more accessible guide to working with Mathematica, and so I strongly recommend it.

Example

Here is an example document fragment, which describes an investigation of a expression for the Dirac delta function.

We have learned that the completeness of the set of all eigenfunction (Subscript[g, k])(x) of an hermitian operator means that

δ(x-x')=(∑_(k = 1)^∞)(Subscript[g, k])(x) (SuperStar[(Subscript[g, k])(x')]).

A way to explore this expression is to specify the set of eigenfunctions to use, and then plot the sum for increasing numbers of eigenfunctions. Let's choose the eigenfunctions to be those of a particle confined to a one dimensional region of length L = 1, namely (Subscript[g, k])(x) = (Sqrt[2]) sin(k π x). The function that defines these is

g[k_, x_] := (Sqrt[2]) Sin[k π x]

We can then write the function that approximates δ(x - x') as

d[n_, x_, xp_] := Sum[g[k, x] g[k, xp], {k, 1, n}]

Here is what the 4 term approximation looks like, for x' = 0.6.

Plot[d[4, x, 0.6], {x, 0, 1}, PlotRange -> All];

Here is what the 12 term and 4 term approximations looks like together.

Plot[{d[4, x, 0.6], d[12, x, 0.6]}, 
{x, 0, 1}, 
        PlotRange -> All];

Here is the 36 term approximation added to the previous two.

Plot[{d[4, x, 0.6], d[12, x, 0.6], 
          d[36, x, 0.6]}, 
{x, 0, 1}, 
        PlotRange -> All];

Here are the same three approximations together with those evaluated at x' = 0.35,

Plot[{
d[4, x, 0.6], d[12, x, 0.6], 
          d[36, x, 0.6], 
d[4, x, 0.35], d[12, x, 0.35], 
          d[36, x, 0.35]}, 
{x, 0, 1}, 
        PlotRange -> All];

From these comparisons we see that the more eigenfunctions we include, the more sharply peaked the approximation, and that the point of peaking is x'.

As one final exploration, let's evaluate the integral of the three approximations centered at x' = 0.35.

Integrate[{d[4, x, 0.35], d[12, x, 0.35], d[36, x, 0.35]}, {x, 0, 
        1}]

The results show that the area approaches 1 as more terms are included in the approximation.

Hopefully this helps us makes sense of the so-called completeness relation

δ(x-x')=(∑_(k = 1)^∞)(Subscript[g, k])(x) (SuperStar[(Subscript[g, k])(x')]).