Introduction to Mathematica

Multivariable Calculus, Math 211, Fall 2021, Carleton College, Joshua R. Davis, jdavis@carleton.edu

How to use this notebook

This notebook surveys the essentials of using Mathematica in a course such as multivariable calculus. I try to keep the notebook as concise as possible. Everything here is 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 tutorial materials 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, cells within cells within cells, etc.

To activate a cell of code, click anywhere in the cell and press Enter or Shift-Return (not just Return). Once you do, Mathematica carries out the computation described by the code and shows you the answer, if any.

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.

Arithmetic

Mathematica is basically an extremely fancy calculator. To see what I mean, activate the following code cell (by clicking on the code cell and pressing Enter or Shift-Return). 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.

N[Log[100]]

Mathematica has all of the functions that you expect --- and many more. They all begin with upper-case letters. If you misspell a function, then Mathematica tells you that it doesn’t know what you’re talking about, by showing the function name in blue. For example, execute the following cell. Then change “sin” to “Sin” and try again.

sin[90 Degree]

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. (Notice that t is blue, because its value is unknown to Mathematica.)

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. Notice that you enter vectors with curly brackets {} rather than angle brackets <> or parentheses ().

{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.

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 useful for really 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 anti-derivatives 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). To clarify, you need to hold down Command or Alt and then press the period key.

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 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’s an example from Calculus I.

Plot[x - Sin[x], {x, 0, 4 Pi}]

Here’s a helix in a torus. Grab the 3D image with your mouse, to rotate it.

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

Here’s the graph of the function (y^2) sin(x).

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

Here’s a strange example. Look closely. What is happening at the origin?

Plot3D[x^y, {x, 0, 1}, {y, 0, 1}]

Here’s a famous surface, which we discuss later in the course.

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

More

Maybe now you believe that Mathematica can help you learn calculus. If you want to get better at it, then go on to the next tutorial, which is memory.nb. Other notebooks are made available throughout our course.

You can also find Mathematica tutorials and examples on the Web. Thousands of Mathematica resources are available, 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 various kinds of factual questions.

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