Introduction to Mathematica

Math 344, Carleton College, Joshua R. Davis, revised 2020 September 13

How to use this notebook

This notebook surveys the essentials of using Mathematica. I try to keep the notebook as concise as possible. Everything here is necessary, or at least extremely helpful, for completing your coursework.

Learning Mathematica itself is not a goal of our course. I will never test you on Mathematica without letting you refer to basic tutorials like this. But you should be aware of what’s in the tutorials, so that you can refer to them quickly when needed.

A Mathematica notebook is a mixture of program code, explanatory text, figures, and more, organized into “cells”. See the brackets along the right side of this notebook? They delineate the cells, cells within cells, etc.

To activate a cell of program code, click anywhere in the cell and press Enter or Shift-Return (not just Return). Activating a code cell causes Mathematica to carry out the computation described by the code.

The first time that you read this notebook, you should activate every code cell, observe the results, and use the surrounding text to understand what is happening.

Fancy calculator

Arithmetic

Mathematica is basically an extremely fancy calculator. To see what I mean, activate the following code cell (following the instructions above). Mathematica runs the code and shows you the result.

2 + 5

Here’s another simple example, using the natural logarithm function Log. In Mathematica, you apply functions to inputs using square brackets, not parentheses.

Log[100]

In the preceding example, Mathematica appeared not to do anything. That’s because Mathematica keeps all of its calculations exact by default, and there is no simple, exact expression for the natural logarithm of 100. If you want a numerical approximation to the answer, then use the N function on the number Log[100].

N[Log[100]]

Algebra

The reason why Mathematica keeps everything exact is that it is not just a numerical calculator. It is a “computer algebra system”, meaning that it can perform complicated algebraic manipulations and arrive at answers that are exactly correct. Here’s a small example of expanding a polynomial.

Expand[(t + 2)^7]

Here’s a larger example of solving a system of two non-linear equations. It turns out that there are four possible solutions, each containing a value for x and a value for y. Notice that Mathematica gives exact answers, in terms of the square root of 3. Notice also that half of the answers are non-real; Mathematica knows about complex numbers.

Solve[{x^2 == y^2, x y == -3}, {x, y}]

You can enter more than one command into a single code cell. Mathematica executes them in order and prints the results. Here are some examples with vectors.

{1, 2, -1} + {x, 1, 3}
{1, 2, -1}.{x, 1, 3}
Cross[{1, 2, -1}, {x, 1, 3}]

Calculus

Differentiation is a mechanical process, so it’s not surprising that Mathematica’s algebra facilities can differentiate functions. But Mathematica can also anti-differentiate (compute indefinite integrals) quite well. Just remember to append “+ C”.

D[Log[(Sin[x] - x^3)] / Cos[x], x]
Integrate[Cos[E^beta] E^beta / Sin[E^beta], beta]

What if you wanted to integrate that last function from 0 to 1? Here are a few ways to do it. The first way gives you an exact answer. The second way computes an exact answer and then numerically approximates it. The third way uses numerical integration methods to compute the integral, without even attempting to compute an exact answer first. This third way is really useful for more complicated functions, whose antiderivatives can’t be computed in any nice way.

Integrate[Cos[E^beta] E^beta / Sin[E^beta], {beta, 0, 1}]
N[Integrate[Cos[E^beta] E^beta / Sin[E^beta], {beta, 0, 1}]]
NIntegrate[Cos[E^beta] E^beta / Sin[E^beta], {beta, 0, 1}]

Canceling a computation

As you may know, only the simplest functions have antiderivatives that can be written down in terms of elementary functions. Here is an example where Mathematica is forced to use a rather fancy “hypergeometric” function. Notice that the answer incorporates the unknown constant a.

Integrate[Sqrt[1 - x^3 / a^3], x]

What if you wanted to know the definite integral, from x = 0 to x = 1? Execute the following code cell. When you do, Mathematica may take a long time to compute. I think it’s trying desperately to simplify the hypergeometric function. After a few seconds, cancel the computation by pressing Command-. (on a Mac) or Alt-. (on Windows).

Integrate[Sqrt[1 - x^3 / a^3], {x, 0, 1}]

If we just want to know the definite integral for a particular value of the constant a, then we can plug that value in before integrating, and save Mathematica a lot of work. Here’s an example with a = 2.

Integrate[Sqrt[1 - x^3 / 2^3], {x, 0, 1}]
N[Integrate[Sqrt[1 - x^3 / 2^3], {x, 0, 1}]]
NIntegrate[Sqrt[1 - x^3 / 2^3], {x, 0, 1}]

Graphing

Mathematica also offers various graphing capabilities. Here are two examples. You can rotate the 3D example by dragging it with your mouse.

Plot[x - Sin[x], {x, 0, 4 Pi}]
ParametricPlot3D[
	{(2 + Cos[9 t]) Cos[t], (2 + Cos[9 t]) Sin[t], Sin[9 t]},
	{t, 0, 2 Pi}]

Here are two more 3D examples. In the second one, look closely at the origin. Mathematica might not handle it well, because it’s not a nice function.

Plot3D[x^3 - 3 x y^2, {x, -3, 3}, {y, -3, 3}]
Plot3D[x^y, {x, 0, 1}, {y, 0, 1}]

More

Mathematica is also a sophisticated programming language. Thousands of packages are available all over the web, for doing just about any kind of computation you can imagine. Mathematica is also the basis for Wolfram Alpha, which is an “artificial intelligence” product for answering many kinds of factual questions.

Various competing software products are available, including Maple, Sage, Mathcad, Matlab, Octave, and R. They have varying strengths, weaknesses, and costs. For example, you may use R if you take a statistics course. I use several products and programming languages in my own mathematical research.

Storing values in memory

This section presents a simple and powerful tool, that can greatly expedite your use of Mathematica. But this tool is also the cause of many, many user errors. So it is worth your time to read this section carefully.

Basics

The tool is simply this: Whenever Mathematica computes something for you, you can store the answer for future use, under a name of your choice. To illustrate, let’s return to the vector examples from earlier in this tutorial.

{1, 2, -1} + {x, 1, 3}
{1, 2, -1}.{x, 1, 3}
Cross[{1, 2, -1}, {x, 1, 3}]

These three calculations all use the same two vectors. It’s tedious and error-prone to retype them. We’d rather just give them names, such as v and w, and then use those names like this.

v + w
v.w
Cross[v, w]

When you execute the code cell above, not much happens, because Mathematica doesn’t know what v and w are. It signals this by coloring those names blue. Now execute the following cell.

v = {1, 2, -1}
w = {x, 1, 3}

What happens? Mathematica loads the two vectors into its memory with the names v and w. Now it knows what v and w are, so it colors those names black, in all code cells throughout the notebook. Let’s try evaluating the sum, dot product, and cross product of v and w again.

v + w
v.w
Cross[v, w]

Notice that x is still in blue, because Mathematica doesn’t know what x is. Mathematica manipulates x as a variable, doing as much algebra as it can without assuming any particular value for x. The qualitative difference between x and v is very apparent in this next example. (By the way, the v version is not a well-formed mathematical expression, but Mathematica gives an answer anyway. What exactly is Mathematica doing?)

Expand[(v + 3)^2]
Expand[(x + 3)^2]

So what if you want to do algebra with a variable v, but you’ve assigned v a value, so Mathematica refuses to do algebra with it? Then you need to clear the value from v, like this.

Clear[v]
Expand[(v + 3)^2]
Expand[(x + 3)^2]

Choosing names

You have great leeway in how you name your quantities stored in memory. Names can be just about any combination of upper- and lower-case letters and digits. However, there are two rules that you must observe.

First, you can’t start a name with a digit, as this example shows.

secondGraders = 126
2ndGraders = 126

Second, you should never start a name with an upper-case letter. The reason is that all built-in Mathematica names (Pi, Sin, E, Log, Plot, etc.) start with upper-case letters, and it’s a good idea to distinguish your names from the names of built-in quantities. If you try to reuse a name that Mathematica has built-in, then Mathematica actually refuses.

N = 34
N
n = 34
n

In the following example, Mathematica lets you use the name M, but I’m telling you that it really isn’t a good idea to begin your names with upper-case letters, so don’t.

M = 36
M
m = 36
m

Notebook vs. memory

Evaluate this code cell.

x = 13

Then evaluate this code cell.

x = 20

Then go back and evaluate the first code cell but not the second. Then evaluate this next code cell. What value of x is reported?

x

The lesson here is that what you see in a notebook does not necessarily reflect what’s in Mathematica’s memory. The value of x is not the last value listed (spatially) in the notebook, but rather the last value that was assigned (temporally) from the notebook.

When Mathematica starts up, its memory is clear. No names are storing any values, except for the built-in names with their built-in values. To see what I mean, quit Mathematica and reopen this notebook, right now. When Mathematica starts up again, you will find that all of the names we’ve been using (m, v, etc.) are in blue. If you want to use the values that you set up before quitting, you have to set all of those values again.

For now, just activate this next cell to set a value for t. Then close this notebook without quitting Mathematica, and reopen the notebook. You will find that the name t still has a value, but the other names (m, v, etc.) still don’t.

t = (13 + r)^2

Now let’s hit the same idea from another direction. Keeping this notebook open, go to the File menu and open a new notebook. In that notebook, type “r = -2” (without the quotation marks) and hit Shift-Return or Enter to execute that code. Then come back to this notebook. You will see that r is black in both notebooks. And evaluating this next cell produces a different answer than it did earlier, because r is no longer unknown.

t = (13 + r)^2

The moral of the story is: Mathematica has a memory, consisting of names paired with values. You can set values in this memory by issuing commands from a notebook. If you have multiple notebooks open, then they all interact with the same memory. If you close a notebook, but don’t quit Mathematica, then the memory is still there. But quitting Mathematica clears all of the memory. So:

* If Mathematica isn’t behaving as you expect, make sure that the names are set up as you think they are. Make sure that unknowns don’t accidentally have values stored in them, and Clear them if they do. Make sure that names that should be storing values really are storing them, rather than being unknown. And make sure that the values are what they’re supposed to be.

* Make sure to save/print/write any important results from your Mathematica work, before you quit the program or log out of the computer.