Mathematica 4.0 in 30 Minutes

Basics

The first thing you need to know is how to enter expressions. Create a new notebook by selecting File | New or pressing Ctrl+N. Enter the following text:

1 + 1

Then hit Shift+Enter, or hit the numeric keypad enter key. This evaluates the expression you've entered. The kernel starts when you evaluate your first expression, so it may take a few seconds. Once the kernel starts and evaluates the computation, you should see the result appear in a cell just below the one where you entered an expression:

1 + 1

The blue brackets at the far right of the window show how cells are grouped together; this allows you to keep track of which results go with which computations, and ultimately to create heirarchies like this tutorial. You can double-click to expand or collapse everything in a given bracket. Highlight a bracket and hit Shift+Enter or the keypad enter to evaluate it. Hit delete to delete that entire bracket. Try expanding, collapsing, evaluating, and deleting in your new notebook.

Obviously you're going to want to do more complicated things than add, eventually. To take the sin of a number, you can type:

Sin[Pi]

There are three important things to notice about this expression. First, the names of all Mathematica functions are capitalized. So if you enter

sin[Pi]

You get a warning, and the result of your expression (the evaluation of your brand new "sin" function, rather than Mathematica's Sin function). Note also that the argument to the Sin function is enclosed in square brackets. This is how all arguments to all Mathematica functions are made. Thus, the expression

Sin (Pi)

returns π times the Sin function. Note that the output is actually a function in this case, not a number. More on outputting functions later. The final thing to note is the use of the constant π, or Pi, which is also capitalized. Thus, the expression

Sin[pi]

gives you no error, but "pi" is a new variable about which Mathematica knows absolutely nothing. It might as well be named "x".

Mathematica is much more than a glorified calculator, however. It can perform symbolic manipulation as well! This expression integrates (x^2) + 1 with respect to x:

Integrate[x^2 + 1, x]

Most functions are named exactly what you'd expect, with the notable exception of the D function for differentiation:

D[x^2 + y^3 + x y, x]

D[x^2 + y^3 + x y, y]

Note that the expression above uses implicit multiplication of x and y; this is accomplished by putting a space between them. Without the space, the expression includes a third variable named "xy":

D[x^2 + y^3 + xy, x]

D[x^2 + y^3 + xy, xy]

Finally, it's important to note that you can give names to expressions by storing them in variables. So say we wanted to store the expression (x^2) + (y^2)for later computation. We'll call it z:

z = x^2 + y^2

z

To clear the value of a variable, just set it equal to ".", as follows:

z =.

Note that the value held by z has, in fact, been erased:

z

One final thing you might want to do is get numerical values for your computations. This is accomplished with the N function:

N[Pi]

A more common way of applying single-argument functions is to use shorthand.

Pi // N

2 // Sqrt

2 // Sqrt // N

This may introduce ambiguity in the order of operations. (For you, not for the computer.) The // is generally evaluated last, as a rule of thumb.When in doubt, parenthesize.

1 + 4 // Sqrt

With a second argument, you can specifiy the precision of the numerical output:

N[Pi, 20]

Now you know almost everything you need to know in order to do basic computations! See the Help Browser (F1) for further details. This is extremely useful, and includes examples! Let's move on to lists.

Lists

It's often the case that you want to do computations on lists, and Mathematica has an excellent set of tools for working with lists. It's easy to define a list. Let's create one called "a":

a = {1, 2, 3, Pi, x, x^2 + 3}

Some useful list functions are demonstrated below; see the Help (F1) for further details. Note that if you select one of the function names below and then hit F1, you get help on that function. This is true in general, and comes in handy for deciphering commands you may encounter.

First[a]

Rest[a]

a[[4]]

Drop the first two elements of a:

Drop[a, 2]

Or the last two:

Drop[a, -2]

Here's a more complicated, but very useful function that takes a Boolean-valued function as its second input:

Select[a, NumberQ]

The NumberQ function returns True (capitalized, remember?) if and only if the item it receives as input is a number. The Select function returns a list of the elements for which the test function returned true. Pi isn't a number from Mathematica's point of view; it's a constant. Be careful of details like this when using Pi and E and I as constants.

Matrices are just lists of lists, but there are some special functions that apply to them:

b = {{1, 2}, {4, 3}}

b[[1]][[2]]

b[[1, 2]]

b[[2, 1]]

Transpose[b]

Det[b]

Inverse[b]

Matrix multiplication is performed using the "." operator:

b . Inverse[b]

To generate a list or table, you may want to use the Table function, which evaluates its first argument for the ranges specified as its later arguments. For example:

Table[i + j, {i, 1, 3}, {j, 0, 3}]

You can also generate a table of lists, of course:

Table[{x, Sin[x]}, {x, 0, 2  Pi, Pi/6}]

The last Pi/6 tells Table to go from 0 to 2Pi in steps of this size. F1 for details. Note that Mathematica leaves expressions such as ((Sqrt[3])/2)in exact form, rather than evaluating them numerically.

Advanced Lists and Functions

This section describes more advanced commands and shorthand for working with lists. Most of the commands have long and short forms, in order to save typing. We'll mention the long forms briefly and then settle in with the shorthand.

The Map function applies a function to each element of a list:

Map[Sin, {0, Pi, Pi/2}]

Its shorthand form is function/@list:

Sin /@ {0, Pi, Pi/2}

The Apply function essentially passes a list as arguments to a function with multiple arguments:

Apply[Plus, {1, 2}]

Plus @@ {1, 2, 3, 4}

This is all well and good for built-in functions, but you may need to define your own. Fortunately, Mathematica accounts for that. You can define a function as follows:

newPlus = Function[#1 + #2]

The Function command returns a function specified by its argument. The arguments to the actual function are given by # (for a single-argument function) or #1, #2, etc. Thus, the function newPlus is now defined to take two arguments (#1 and #2) and return their sum:

newPlus[1, 2]

The shorthand for this is as follows:

newPlus = (#1 + #2) &

Now we're ready to do heavy-duty list manipulation. Let's define a function without even giving it a name, then map it onto a list. Looks complicated? Break it down.

(#^2 + 1) & /@ {1, 2, 3, 4, 5}

See Help on Function for further details.

Plotting Commands

There are commands for plotting functions, lists, and 3D information.

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

Easy enough. You can also plot lists using the ListPlot command:

a = {5, 4, 3, 2, 1}

ListPlot[a]

The graphics can be resized using the mouse if they're too small. Here the list a has been plotted using the index of each element as an x-coordinate. Alternately, we could do

a = Table[{x, Sin[x]}, {x, 0, 2  Pi, Pi/12}]

ListPlot[a]

Note that Mathematica evaluates the exact expressions in the list numerically in order to plot them, but the list remains unchanged.

The commands displayed here all have plotting options (see help examples for details) and 3D versions. For example:

Plot3D[x^2 + y^2, {x, -5, 5}, {y, -5, 5}]

Previous Commands

Mathematica has a built-in history feature. Note that each expression you enter has In[#] and Out[#] next to the input and output. You can recall previous outputs using the % command. For example:

1 + 1

% + 2

This command displays the third item in the history:

%3

Making Things Pretty

There are some functions that only affect the display of a variable. Useful ones include MatrixForm and TableForm, for example:

a = {{1, 2}, {4, 3}}

a // MatrixForm

TableForm[a]

One other thing you may want to do is suppress output in certain cases. To do this, add a semicolon to the end of the line:

a = {1, 2, 3, 4};

Note that there is no output, but you can still access the result, both through the variable a, and through the history:

%

a

Mathematica also includes a way of inputting mathematical expressions in standard form. Ctrl+2 creates a graphical square root, for example:

(Sqrt[2])

Hit Ctrl+Space to escape from a graphical template. Try to enter the following expression on your own, in a new notebook:

(Sqrt[2]) + 1

Ctrl+6 creates an exponent, and Ctrl+/ creates a fraction. In the fraction, Tab moves to the next box.

(((x^2) + (y^2))/2)

Again, try to enter this expression on your own. I used the key sequence Ctrl+/ x Ctrl+6 2 Ctrl+Space + y Ctrl+6 2 Ctrl+Space Tab 2 Ctrl+Space Enter. Seem like more work than it's worth? Wait until you have to debug an expression like

((((rawdata[[i, 2]] rawdata[[i, 7]])/rawdata)[[i, 4]] - ((blankdata[[i, 2]] blankdata[[i, 5]])/blankdata)[[i, 4]])/(rawdata[[i, 5]]/rawdata[[i, 4]] - (PMTflare/PMTflarePickOff)))

and you'll gain new appreciation for the benefits of graphical input! The alternative is:

(rawdata[[i, 2]] rawdata[[i, 7]]/rawdata[[i, 4]] - 
          blankdata[[i, 2]] 
            blankdata[[i, 5]]/
              blankdata[[i, 4]])/(rawdata[[i, 5]]/
            rawdata[[i, 4]] - PMTflare/PMTflarePickOff)

Further Reading

The Help files can teach you how to do pretty much anything in relatively short order. Just hit F1, and search the Master Index (click that button) to get everything from discrete tools to graphical directives to plotting options.