Adapted from 
Discovering Number Theory Lab 0

Jeffrey J. Holt
University of Virginia

John W. Jones
Arizona State University

Copyright © 2001 by W. H. Freeman and Company (this isn't really all their stuff anymore)

Introduction to Using Mathematica

Mathematica Basics
Click twice on the outer bracket at the right to open the cell.

If you're reading this cell now, then you've mastered the second hardest thing about Mathematica -- figuring out how to open a closed collection of cells.

The hardest thing about Mathematica is figuring out how to have Mathematica execute a computation.  For example, to have Mathematica evaluate 17 + 34, we type

17+34

To perform a computation, click the mouse on the command you want to execute, hold down the Shift key, and press the normal carriage return key, labeled Enter or Return.  (Hitting only the Enter key in the numeric keypad portion of your keyboard will also work, although we do not openly advocate hitting computers.)  Try it out on the above command.

If everything works the way it should, then you will get the answer 51. If you don't, ask for help.

Try having Mathematica evaluate the following:

3+4

13-47

12 23

12*23

284/16

2^143

Notice that 12 23 and 12*23 have the same effect.  Multiplication can be indicated either with the symbol * or by leaving a space between the two quantities that are to be multiplied together.  Mathematica version 6 will insert a multiplication sign between numbers when you leave a space; earlier versions will just leave the space.

Now that you have made some modifications to this notebook, you should save your work.  Doing so is easy:  Click on Save in the File menu.  There is also a keyboard "shortcut" that you can use.  On a Windows machine, hold down the Ctrl key and type s.  On a Macintosh, hold down the Command/"cloverleaf"/apple key adjacent to the Space bar and type s.  (In the future, we shall use the word control to refer to the Ctrl key on a Windows machine and the Command/"cloverleaf"/apple key on a Macintosh.  If you are using something other than a Windows machine or a Macintosh, check with your instructor to determine your "control" key.)  You should save frequently to avoid losing your work if the machine malfunctions.

Everything in a Mathematica notebook is contained in "cells".  The scope of a given cell is indicated by the square bracket at the right.  There are a variety of different kinds of cells.  This one is called a "text" cell, and (you guessed it) is used for text.  There are other kinds of cells, such as those shown below:

This is an input cell. (Do not execute.)

This is a text cell.

This is a subsection cell.

You can change the type of cell by clicking on the cell bracket (it will become shaded) and then by clicking on the Format menu and selecting Style.  The different types of cells allow great freedom in customizing notebooks.

To create a new cell, move the cursor until it is between cells (the cursor will change from vertical to horizontal) and then click.  A horizontal line should appear.  Then start typing; you will get an input cell, which can be changed to a different type of cell using the Style menu within the Format menu.

Create a cell below this one to compute 123!. (The "!" is not at the end of the sentence for emphasis, it's there to indicate the factorial operation.  Factorials are entered just as shown).

You can obtain decimal approximations using the command "N".  For instance, when you executed the command to compute 284/16, Mathematica reduced the fraction but returned a fraction as output.  To get a decimal approximation, run the command below:

N[284/16]

You can get more decimal places.  For instance, here are 100 digits of π:

N[Pi,100]

To close this collection of cells, click twice on the outer cell bracket.

Some Stuff Mathematica Can Do for You
Click twice on the outer bracket at the right to open the cell.

Here is (((x + 2 y))^(25 ))multiplied out:

Clear[x,y]
Expand[(x+2 y)^25]

If you don't hang the command Expand with the square brackets [ ] around the product, Mathematica will not multiply out the terms because it was not told to do so.  Try it.

(x+2y)^25

Get it?  Try changing the numbers above and rerunning.

Mathematica can also factor polynomials:

Factor[1+x^15]

As mentioned in the preceding section, you don't have to use an asterisk between two variables when you multiply them, but you can if you want to.

a=7;
b=4;
a b
a*b

However, remember that if you don't use an asterisk then you must put a space between the variable names.  To Mathematica, "ab" is not a product but a two-letter variable name.

a b
ab

Note that Mathematica remembers the values of a and b.

This is a calculus class, so let's get Mathematica to do some calculus for us.  What are these commands doing?

D[x^2 +1, x]

Integrate[x^2 +1, x]

Integrate[x^2 +1, {x,-1,1}]

Plot[x^2 +1, {x,-1,1}]

To close this collection of cells, click twice on the outer cell bracket.

Two Useful Commands
Click twice on the outer b....., well, you know the drill!

Two commands that you will use constantly are

Here is an example of how to use these commands.

N[3432902008176640000/121645100408832000]

Some other things about copy-and-paste:

Getting Help in Mathematica

Mathematica is a big program with lots of parts.  However, it's not necessary to learn everything about Mathematica in order to use it.  Feel free to experiment -- look at the menus, try out different things, play with the program.  Don't be afraid of breaking anything.  You can't break the machine just by pushing keys!

Selecting "Documentation Center" in the "Help" menu provides a means of searching for commands.  It's awesome.  It also gives you a ton of information about each command, and examples.  You can actually execute the stuff in the Documentation Center there if you want to, to see what commands do before muddying up your primary window with them.  If you're like me, you'll like the Documentation Center a lot better than this next help-source...

You can also get help from the Mathematica program itself.  If you want to find out what a certain function does, you can type ?"FunctionName".  For example, to find out what the functions Integrate and Plot do, you would type:

?Integrate

?Plot

You get more information by using two question marks:

??Integrate

??Plot

As you go along, you may find that something doesn't work the way you expect it to, or that there is something you would like Mathematica to do, but you don't know how to make it work. In those cases, you should seek help from the following sources:

Here's another experiment for you to try.

(a) What does the Mathematica function Floor do?
	(b) What does the Mathematica function Ceiling do?

S-i-R code

Defining functions in Mathematica is easy, but we're not going to do it yet.  (Why? Because the way Mathematica defines a function, it can't handle our S-i-R stuff.)  First, we'll notice something (seemingly) strange that Mathematica does.  Evaluate the following cell.

I

Well, that's bizarre, isn't it?  Not so much, as it turns out.  There are lots of constants built into Mathematica, and here's one of them:

Sqrt[-1]

Thus, we'll be using a lower-case "i" instead of "I" in our S-i-R work.  Here is a Mathematica command which corresponds to the SIR program which appears on page 44 of the text. (You don't need to understand what this does until we get to that part of the book; for now, you can just use the code and perhaps change a few constants.)

t = 0; 
S = 45400; 
i = 2100; 
R = 2500;
deltat=1;
For[k=1,k≤ 3,
Sprime = -.00001*S*i; 
iprime =.00001*S*i - i/14;
 Rprime = i/14;
 deltaS = Sprime*deltat;
 deltai = iprime*deltat;
 deltaR = Rprime*deltat;
 t=t+deltat; 
S = S+deltaS; 
i = i + deltai; 
R = R+deltaR; 
Print[t];Print[S];Print[i];Print[ R]; 
k++]

There are lots of ways to make this code better and more useful, and we'll get to them later.