Brief Introduction to Mathematica

Math 213 -- Math with Mathematica -- Fall 2009.
Christopher Hanusa -- chanusa@qc.cuny.edu -- Office: Kiely 409
File last modified: August 28, 2009
Thanks to John W. Kennedy for a previous incarnation of this file.

Getting Familiar

Mathematica is much more than a sophisticated calculator; however, a few simple calculator tasks will get you familiar with the interface.

Mathematica works with Notebooks—a new notebook appears when you start the program.

Exercise 1.  Try a simple addition. Type

3+4

and then use the ENTER key (not the return key).

Exercise 2.  Now try multiplication. Type

27*13

and again use the ENTER key.

Multiplication is indicated in Mathematica by * or by a space.  Thus, a*b and a b will give the same result; however, ab will be interpreted as a single quantity (named ab).

Exercise 3. Try entering

5  7

and also try

57

to compare the results.

Mathematica: Its Cells and Its Commands

Mathematica Cells

Mathematica works with cells.  The contents of a cell can be changed when the cell contains the blinking cursor.

To create a new cell, use the mouse to move the cursor between two cells and it changes to a horizontal "I-beam", click the mouse and a horizontal line appears.

When the notebook shows a horizontal line, what you type will appear in a new input cell.

Exercise 4.  Practice changing the simple commands in cells you already used. (After changing them, use the ENTER key to get the new output). Also, try creating a new cell between the cells you already made.

Mathematica Commands

Mathematica has a huge collection of commands. Each built-in Mathematica command begins with a capital letter and Mathematica insists on this. Thus, for the square root of a number we need the command Sqrt. The rest of the command indicates which number we need the square root of, and this is placed between square brackets, following the command name. Thus, Sqrt[n] computes the square root of the number n.

Exercise 5.  Compute the square root of 121, and that of 121801.

Mathematica Is More Than a Sophisticated Calculator

Numerical Approximations to Square Roots

Since Mathematica is not just a calculator, some of the results it returns may initially come as a bit of a surprise.

Exercise 6.  Try asking for the square root of 15.

What Mathematica does naturally is to process mathematical objects symbolically rather than numerically—it is not natural for it to approximate mathematical objects like the square root of 15.

We can force Mathematica to approximate the result by using its numerical result command, N[].

Exercise 7.  Enter the statement

N[Sqrt[15]]

to obtain a 6-digit numerical approximation to (Sqrt[15]).

Six digits is the default for Mathematica. Thus, the command N[Sqrt[15]] is equivalent to the command N[Sqrt[15],6]—six digits is assumed unless you request more.  If you want more digits, ask for them—and you can get as many as you like.

Exercise 8.  What do you expect the following statement to give?

N[Sqrt[15],100]

The number π

Mathematica has a built-in command for the irrational number π.

Exercise 9.  In a new cell, enter the Mathematica command Pi.

Again, if you want to force Mathematica to give a numerical approximation you need to ask.

Exercise 10.  Enter N[Pi,500] for the first 500 digits of π.

Getting Help with Commands

If at any point you need help with a command, use the ? operator.  This will give you a brief summary of the command and a link via >> to the Documentation Center listing for this command.  This is often the best way to learn about the options that the command needs.

Exercise 11.  Learn more about Sqrt:

? Sqrt

Symbolic Manipulations

Expand

Although Mathematica can be forced to act like a numeric calculator, its big advantage is in accurately manipulating mathematical objects symbolically. A simple algebra illustration is provided by the next exercise, using the Expand command.

Exercise 12.  Type in the following statements.  Before entering each, see if you can predict the output.

(x+y)^8

Expand [(x + y)^8]

You have obtained the (8^th) power binomial expansion, whose coefficients form the eighth row of Pascal's Triangle.

Defining Functions

If you have several calculations of similar type then it is worthwhile to make things even more convenient by creating your own functions. Thus, we might define our own function as the (n^th) power binomial (((x+y))^n).

Exercise 13.  Type and then enter the following statement

F[n_] := (x + y)^n

Notice also that Mathematica colors your input as you type.  Because you have written n_ on the left hand side in the function, Mathematica recognizes that occurrences of n on the right hand side is defined by user input into the function.  Also notice that F is blue (undefined) until it is evaluated.  
Entering this statement does not generate any output; this is always the case when you define a function using the := assignment rather than =. This statement defines our own binomial function allowing us to specify whatever exponent (value for n) we like.

Exercise 14.  To see this enter F[n] and examine the output.

Mathematica now "knows" what we mean by the function F, and as with any other function, we will need to provide a suitable argument in brackets.

Exercise 15.  Try, for example, F[x] to see that the result is sensible.  Then try entering F[12], or whatever exponent you would like.

These user-defined functions remain defined and usable in Mathematica until we clear them manually or terminate the Mathematica session.

Exercise 16.  Combine the binomial function with the Expand command to get any binomial expansion that interests you. For example, try Expand[F[8]] and Expand[F[24]].

Defining Functions of Functions

Of course, if you are investigating expansions of binomials, we can save even more typing by defining a new function that calculates the expanded form of the binomial.  Define a new function G in terms of our previous function F.

Exercise 17.  Define the function G by entering the following statement.

G[n_] := Expand [F[n]]

Exercise 18.  Enter suitable statements to see that G[8], G[24], and so forth give the output you expect. Also that G[x], G[2.5], and so on give the correct result but have no sensible (and finite) expanded form.

Plotting Pictures of Mathematical Objects

Mathematica is good at manipulating mathematical objects and presenting them to us in ways that make them easy to visualize. Among others, plotting commands are useful for visualization.

2D Plots

You may be aware of the cosine function; this is Cos[] in Mathematica. If you have forgotten what this looks like, get Mathematica to show you.

Exercise 19.  Enter the following statement

Plot[Cos[x], {x,-20,20}]

This plots the cosine function (with x measured in radians) over the interval [-20, 20]. 

Notice the square brackets around x because Cos is a function and the curly braces specifying the bounds---{x,-20,20} is a list.

Similarly, we can plot the cosine function over the interval [-3π,3π].

Exercise 20.  Enter the following statement

Plot[Cos[x], {x,-3Pi,3Pi}]

Did you ever wonder what cos ((x^2)) looks like?  Or other manipulations you've never encountered before?

Exercise 21.  Enter statements such as:

Plot [Cos [x^2], {x, -3 Pi, 3 Pi}]
Plot [Cos [x^3+3x], {x, -3 Pi, 3 Pi}]

Feel free to experiment further with other exponents, other functions, or other intervals for pictures you would like to see.

3D Plots

In cases where we have two independent variables, a three dimensional plot may be more appropriate. Mathematica can deal with this.

Previously we entered (defined) our own binomial function F (from Exercise 13) and its expanded form, G (from Exercise 17).  For any given power, we can visualize this function with the three dimensional plot command Plot3D.

Exercise 22.  Enter the statement

Plot3D[F[9], {x,-1,1}, {y,-2,2}]

This will give you a plot of (((x + y))^9) over the intervals of x and y specified.  If you would like to see plots like this for other binomials, or over other intervals, be creative and design your own exercises.

Here is one example---What does the cosine of the binomial function with exponent 4 look like?  Try the following exercise to see what we found:

Exercise 23.  Enter the statement

Plot3D [Cos[F[4]], {x,-2 Pi,2 Pi}, {y,-2 Pi,2Pi}]

Animation

We can take two-dimensional and three-dimensional plots even further.  Sometimes it is helpful to see relationships among a set of related images by displaying them in rapid succession. This is one way that animation is achieved in movies; we can use animation in Mathematica to produce interesting effects. The following exercise is a simple illustration.

First, we generate a list of three-dimensional plots of the binomial function with varying exponents. To do this we make use of the Table command in Mathematica:

Exercise 24.  Enter the statement

BinaryList =Table[Plot3D[F[n],{x,-1, 1}, {y,-2,23}],{n, 1, 8}]

This defines a list of eight three-dimensional plots for the binomial function with the exponent increasing from 1 to 8.

Exercise 25.  We can animate this list by using the command ListAnimate. Now enter

ListAnimate[BinaryList]

Mathematica draws the axes and the box surrounding your plots by default. If you find this distracting in the animation from Exercise 25, then get rid of the axes and box.

Exercise 28.  Modify the statement from Exercise 24 to say

BinaryList =Table[Plot3D[F[n],{x,-1, 1}, {y,-2,23},Axes->False,Boxed->False,Mesh->False],{n, 1, 8}]

Is that better?

Feel free to experiment with producing your own (improved) animations. Many other options are available including color.

Exercise 29.  Type ?Plot3D to see graphing options.  I also suggest ?Manipulate to see how Mathematica can modify continuous parameters.  Show me what you can do.

The Exploration Starts

This brief introduction was intended to help you start to get familiar with the Mathematica interface, how the software behaves, and a small sample of things it can do. Now that the process is underway, you can start on the real material for the course you are taking.