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 CASs have an interface for you to write your own 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, ...  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 a word processor and a mathematical workspace.  Mathematica documents are divided into cells which each have a specific purpose.  If you look to the right you will see a series of brackets, each of these brackets is 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 that just has a different font size, face and color.  The cell below with the 1+1 in it is an input cell that has a mathematical expression that we will give to Mathematica to evaluate.  Once we send an input to Mathematica it will create an output cell directly below the input cell with the evaluated expression.  Now we will not be interested in creating mathematical documents in this class, we will simply be using Mathematica to help solve more difficult Calculus problems.  So most of our cells will be input and output cells.  When you begin a new cell Mathematica assumes that this is an input cell so we should not need to worry about the different styles that a cell can take on.

Basic Calculations

Mathematica is probably the most powerful Computer Algebra System (CAS) on the market and it may be the most powerful CAS you ever use but we can still do very simple calculations with it.  As we pointed out above the cell below is an input cell, the Mathematica default.  When you evaluate a cell you simply need to have the cursor in the cell and then type Shift+Enter, that is hold down the Shift key and press the Enter key.  Do it in the cell below.

1+1

You should see several things happen here.  First the answer of 2 (that was hard), and second, in the margin you will see In[1] and Out[1].  Mathematica will keep track of all of the inputs and outputs you do during each session.  Now if you save the Notebook to a file and open it later you will need to reevaluate the inputs.  Here are a few more basic calculations, try them,

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]

You should note a couple things about the above outputs.  First Mathematica is keeping the outputs exact, it is not approximating square roots or converting long expressions to scientific notation, like your calculator would.  Second, the syntax for Mathematica functions is a little strange.  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] and not sin[x].  Another thing that is different from the way we write mathematics is that the expression we are applying the function to is in square brackets.  So in mathematics we would write sin(x) but in Mathematica we would type Sin[x].  Mathematica is case sensitive, so x and X represent two different things. Throughout the class we will see many of Mathematica's functions but we will not even scratch the surface of what Mathematica can do.

Exact Verses Approximation Mode

Computer algebra systems can do 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 exact and if you type 3.0 it will see this as an approximation.  Okay, 3 is 3.0 right, not to a CAS.  Try the following,

3*Pi

3.0*Pi

Note the difference in the outputs.  In the first it keeps pi as pi but in the second, since 3.0 was approximate it approximated pi and did the numeric product.  Also, 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 total 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 so if you were asked to approximate pi to 10 decimal places you would need to use the command,

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 % means to take the last output that Mathematica has calculated.

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*