Getting Started with Mathematica

What is Mathematica?

Mathematica is a Computer Algebra System (CAS).  A CAS is a mathematical workspace that is capable of doing symbolic mathematics, numerical computations and producing graphs.  Some more sophisticated computer algebra systems allow the user to create programs and/or a word processing interface. Mathematica has all of this and more.  Mathematica is more than an educational tool.  It is used by graduate students, professors, engineers, chemists, physicists, and other scientists.  Mathematica is not the only CAS available; other computer algebra systems include Maple, SAGE, Maxima and several other lesser known packages.

The Layout of Mathematica

Mathematica combines the features of a word processor with the functionality of a mathematical workspace.  Mathematica documents are divided into cells, each of which has a specific purpose.  Along the right-hand side of this window there is a series of brackets; each of these brackets indicates a "cell."  The cell you are reading right now is a text cell; the cell above this one (with the section title) is also a text cell with a different font size, face and color.  Below you will find several input cells, each of which contains a mathematical expression which we will ask Mathematica to evaluate.  Once we enter an input cell, Mathematica will create an output cell directly below the input cell with the evaluated expression.  

NOTE: In Mathematica, the default cell type is an input cell; that is, when you create a new cell, Mathematica assumes that the new cell is an input cell (rather than a text cell). Since we will be using Mathematica primarily for computation purposes, rather than creating formatted documents (such as the one you're reading now), this default setting will be very convenient for our purposes.

Basic Calculations

Mathematica is probably the most powerful Computer Algebra System (CAS) on the market, and it may turn out to be the most powerful CAS you will ever use, but we can use it to carry out very simple calculations.  As noted in the preceding section, the cell below is an input cell, which is the Mathematica default.  

To evaluate a cell, place the cursor in the cell (the easiest way to do this is to just click on the expression in the cell), and then press Shift+Enter (that is, hold down the Shift key and press the Enter key).  Try this with the input cell below.

1+1

You should see several things happen here.  The answer of 2 (that was hard) appeared in a new cell, and in the margin you see the notations In[1] and Out[1].  Mathematica will keep track of, and index, all of the inputs and outputs during each session.  (NOTE: If you save the Notebook to a file and re-open it later, you will need to go back and reevaluate each input cell.)  

Here are a few more examples for you to try:

2/3

Pi

E

10/25

Sqrt[25]

Sqrt[12]

5^5

2^1000

103!

Sin[Pi/4]

(x+1)^5

Expand[(x+1)^5]

Expand[(x+1)^25]

Two things to notice regarding the above results:
* Mathematica  is returning exact results, rather than using decimal approximations or scientific notation (as you would see on your calculator).
* Mathematica  is case sensitive! All Mathematica commands, which include standard mathematical functions, start with a capital letter.  So if we want the sine of x we would type Sin[x] rather than sin[x].  
* The argument of each function is enclosed in square brackets rather than rounded parentheses.  So, where on paper we would write sin(x), in Mathematica we would instead type Sin[x].  Again, note that Mathematica is case sensitive, so x and X represent two different variables.

Exact Versus Approximation Mode

Computer algebra systems can carry out both symbolic mathematics and numerical calculations, hence most of them will make a distinction between exact mode and approximate mode.  Most will also have a way of converting from exact to approximate modes.  In Mathematica all input will be assumed to be exact unless there is a decimal in the expression.  So if you type 3 it will see 3 as an exact value; however, if you type 3.0, it will see this as a decimal approximation.  For example, enter each of the following inputs, and compare the results:

3*Pi

3.0*Pi

Note: in Mathematica you do not need the * to denote a product; a space will work as well:

3 Pi

3.0 Pi

You can also take an exact expression and approximate it with the N command.  The N command syntax is N[ expr, # ] where expr is the expression to be evaluated and # is the number of decimal places to use.  Try the following,

N[Pi, 10]

Notice that the number of total decimal places is 10, not the number of decimal places to the right of the decimal point. So, if you were asked to approximate pi to 10 decimal places (to the right of the decimal point) you would need to enter:

N[Pi, 11]

Try the following,

N[Sin[Pi/3]]

N[Sin[Pi/3], 100]

N[Sqrt[5], 20]

Solving Equations

Solving equations can be very difficult even with seemingly easy expressions.  Mathematica has several different commands that will solve equations.  The first two we will look at are the Solve and the NSolve commands.  Solve will try to solve the equation exactly and NSolve will give approximate solutions.  When you give Mathematica an equation you need to use two equal signs ==; a single = has a different meaning.  Try the following:

Solve[x^2+3x+2 == 0, x]

Solve[x^2+3x-2 == 0, x]

NSolve[x^2+3x-2 == 0, x]

Solve[x^2+3x-2 == 0, x]

N[%, 25]

NSolve[x^2+3x-2 == 0, x, 25]

Solve[x^4+3x+2 == 0, x]

Notice that in one of the above expressions we used a % symbol.  The % refers to the result of the most recent computation.

Simplifying and Factoring

There are many commands for simplifying, expanding, factoring and general algebraic manipulations.  We will look at only a few here.

Expand[(2x-1)^4 (3y+z)^5]

Factor[%]

Expand[(2x-1)^4 (3y+z)^5]

Collect[%,x]

Expand[(2x-1)^4 (3y+z)^5]

Collect[%,y]

Expand[(2x-1)^4 (3y+z)^5]

Collect[%,z]

Apart[2x/((x^2+1)(x-2))]

Together[%]

Together[1/x+3/(x-1)-5x/(x+1)]

Numerator[%]

Together[1/x+3/(x-1)-5x/(x+1)]

Denominator[%]

Graphing

As the saying goes, a picture is worth a thousand words.  We will be working with two-dimensional plots in this class but Mathematica is capable of creating three dimensional plots as well as many other special types of graphs.  The standard syntax for a plot is the Plot command followed by the function or list of functions to plot, followed by the domain of the plot, followed by options.  The domain is given as a list, for example {x, 2, 6} would mean that x should go from 2 to 6.  Try the following,

Plot[Sin[x], {x, -5, 5}]

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

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

Plot[Tan[x], {x, -Pi, Pi}]

Plot[Sin[x], {x, -Pi, Pi}, PlotRange->{-1.5, 1.5}]

Plot[Sin[x], {x, -Pi, Pi}, PlotRange->{{-2Pi, 2Pi}, {-1.5, 1.5}}]

Plot[Sin[x], {x, -Pi, Pi}, AspectRatio->1]

Plot[Sin[x], {x, -Pi, Pi}, AspectRatio->Automatic]

Plot[Sin[x], {x, -Pi, Pi}, PlotRange->{-1.5, 1.5}, AspectRatio->Automatic]

Help

The Mathematica Help system is very extensive and interactive.  The help system is written in Mathematica so all of the examples in the documentation can be changed and reevaluated.  Most of the time you will simply go up to the Help menu option but you can invoke the help system from a Mathematica notebook.  For example, to get a list of all the options of a command you can use the Options command,

Options[Plot]

If you know a command name but have forgotten the syntax of the command you can use the ?

?Plot

For the syntax, attributes and options you can use the ??

??Plot

As we saw above the Factor command is for factoring polynomials, what if we wanted to factor a number? Since Factor would most likely be in the name of the command we could put a wildcard * in front and behind Factor and we would get a list of all the commands that have Factor in them,

?*Factor*