A Sampling of Mathematica

This file is a Mathematica  notebook that gives some examples of what Mathematica  can do.  For information on how to read this notebook, see the notebook Read  First.

This file is loosely based on the "Tour of Mathematica" in Mathematica: A System for Doing Mathematics by Computer, Second Edition, by Stephen Wolfram.  This book was published by Addison-Wesley in 1991, and is available at most bookstores.  For information on how to obtain copies of Mathematica  itself, see the section "Buying Mathematica " in the notebook  Read  First.

Numerical Calculations

You can use Mathematica  as an enhanced scientific calculator.  Let's start with a simple example.

45 + 78

The first line here is what you type into Mathematica.  The second line is the result Mathematica  gives.  If you are reading this notebook on a color system, the input and output are blue, with the input in boldface.

Now let's try something more difficult.

3^100

Unlike a calculator, Mathematica  gives an exact answer for 3 raised to the power 100.

Now let's really test out Mathematica.  Here is 3 raised to the power 1000.

3^1000

Here's the result in the form you might get on a calculator.

N[%]

Here is the value of pi to two hundred decimal places.

N[Pi, 200]

Mathematica  knows about a large collection of mathematical functions, most of those you would find in any book of mathematical tables.

BesselJ[5, 34.6]

Log[4.5 + 2I]

Zeta[1/2 + 14.3 I]

Mathematica  can do many kinds of exact computations with integers.  FactorInteger gives the factors of an integer.

FactorInteger[70612139395722186]

You can also do all the standard numerical matrix computations with Mathematica.  This computes the inverse of a 2 by 2 matrix.

Inverse[{{3.5, 7.2}, {-2.4, 6.4}}]

This gives a  numerical approximation to the eigenvalues of the matrix computed in the last example.

Eigenvalues[%]

Mathematica  will also solve  linear programming problems. This returns a list
containing the maximum value of the "objective function" and the point at which it is attained.

ConstrainedMax[17 x -20 y + 18 z,
  {x - y + z < 10, x < 5, x + z > 20}, {x, y, z}]

Algebraic Calculations

One of the most important features of Mathematica  is its ability to deal with mathematical formulas in algebraic form.

(1 + x)^3

This is what Mathematica  does if you type in a simple algebraic expression.
You can expand out the expression like this:

Expand[%]

Mathematica  gives an explicit formula for the result.
You can factor this result to get back to what you started from.
% always stands for the last result that Mathematica  gave you.

Factor[%]

Now let's try a more complicated example.

(1 + 2x + 5y)^7

Expand[%]

Factor[%]

With the option setting Trig -> True Mathematica  will do algebraic operations using trigonometric identities.

Expand[ Cos[x]^3 Sin[x]^2, Trig -> True]

Mathematica  also does linear algebra on symbolic matrices.

Inverse[{{a, b}, {c, d}}]

Calculus

You can use Mathematica  to do calculus. Here's a simple integral.

Integrate[x^n, x]

Here's a more complicated example.

Integrate[x/(x^3-1), x]

Now let's try differentiating again.

D[%, x]

This gives the expression in a different algebraic form. We can get back our original form using Simplify.

Simplify[%]

Mathematica  can also give exact solutions to many definite integrals.

Integrate[ Sin[x]^10 Cos[x]^5 / x^10, {x, 0, Infinity}]

Here's another example.

Integrate[ Log[x] (1 + x^2)^(-2), {x, 0, 1}]

Most integrals found in any book of tables can be done by Mathematica.

Integrate[ Log[x]^6/(1 + x^2),  {x, 0, 1}]

Many integrals do not have a "closed form solution".  If you give Mathematica  such a definite integral it will be returned unevaluated. You can still use N to get a numerical answer.

Integrate[ Sin[ Sin[x]], {x, 0, Pi}]

N[ % ]

Mathematica  can also solve differential equations. Here is a pair of simultaneous differential equations. The solution you get involves two undetermined coefficients.

DSolve[{x'[t] == -y[t], y'[t] == x[t]}, 
                                {x[t], y[t]}, t]

It is a mathematical fact that most differential equations do not have an explicit symbolic solution. In these cases you can get a numerical approximation to the solution using NDSolve.  After the solution is computed it is plotted.

NDSolve[{y''[x] + Sin[x]^2 y'[x] + y[x] == Cos[x]^2,
     y[0] == 1, y'[0] == 0}, y, {x, 0, 20}]

Plot[ Evaluate[ y[x] /. %], {x, 0, 20} ]

Solving Equations

This is how you solve a quadratic equation in Mathematica.

Solve[x^2 + 2 a x + 1 == 0, x]

Here's a more complicated example.

Solve[x^5 + 3x + 1 == 0, x]

It is a fact of mathematics that there is no way to get an exact formula for the solutions of a quintic equation like this.  You can nevertheless ask Mathematica  to give you numerical results.  You get the five complex number roots to the equation.

N[%]

When equations contain complicated functions there is in general no systematic procedure for finding all solutions, even numerically.  The Mathematica  function FindRoot searches for a numerical solution to an equation, starting at a specified point.

FindRoot[{Sin[x] == x - y, Cos[y] == x + y},
                         {x, 1}, {y, 0}]

Mathematica  also has an efficient routine for finding the solution to linear equations. Here's a simple example.

LinearSolve[{{1.02, 5.9}, {2.87, 1.9}}, {1.9, 1.06}]

Graphics

Here is a simple Mathematica  plot.

Plot[Sin[x], {x, 0, 2Pi}]

There are many options you can specify for a plot.  Using Show you can redraw the previous plot with specified options.

Show[% , Frame -> True, 
            PlotLabel  -> "The Sine Function"]

Now for some three-dimensional graphics.

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

Here is a contour plot of the same function.

Show[ ContourGraphics[ % ], ContourShading -> False,
                            ContourSmoothing -> True]

This generates a three-dimensional parametric surface.

ParametricPlot3D[ {u Sin[t], u Cos[t], t/3},
              {t, 0, 12}, {u, -1, 1}, Ticks -> None]

Mathematica  generates all graphics in PostScript, so that you can resize pictures, and make use of the resolution available on different types of printers.

Animated Graphics

You can use sequences of graphics cells in a notebook as frames in a "movie".

For information on other controls for viewing Mathematica  movies, see the notebook Read This First!

This notebook contains a sample of animation in Mathematica.  Due to disk space limitations, this is a simple black and white example.  Look at the Color Movie notebook on your disk for an example of animated color graphics.

To view the movie:

Scroll until the first graphics cell is completely visible in the window.  (The movie is shown in this cell.)

Double-click the first picture in the sequence to start the movie.  Click again anywhere to stop.

For information on other controls for viewing Mathematica  movies, see the notebook Read This First!

Do[Plot3D[Cos[Sqrt[x^2 + y^2]+Abs[n-2Pi]]/
                              Sqrt[x^2 + y^2 + 1/4],
	{x,-4Pi,4Pi},{y,-4Pi,4Pi},
PlotPoints->26,Lighting->True,PlotRange->{-2,2},
BoxRatios->{1,1,1},Boxed->False,Axes->None],
{n, 0, 2Pi - (2Pi/16), 2Pi/16}]

Programming in Mathematica

You can use Mathematica  not only as a "calculator",  but also as a full symbolic programming language.

Many application packages covering specific areas have been or are being written in the Mathematica  language.

A Graphics Application Package

Needs["Graphics`Polyhedra`"]

This loads in a package that defines properties of polyhedra.
The package defines, among other things, the geometry of a dodecahedron. The Vertices function gives the coordinates for the faces of the given polyhedron.
Here are the vertices for a dodecahedron shown in shortened form.

Vertices[ Dodecahedron ] // Short

This shows the dodecahedron as a three-dimensional graphical object.

Show[ Polyhedron[ Dodecahedron]];

As another example, we can use the definitions from the polyhedra package to create an image of a stellated icosahedron, which is often used as an icon for the Mathematica  system.

Show[ Stellate[ Polyhedron[ Icosahedron ] ] ];

Random Walks

These examples illustrate random walks in various dimensions.
Mathematica's  list operations and plotting routines are combined to give concise 
programs that generate and then plot the random walks.

One-Dimensional  Random Walk

FoldList[Plus, 0, Table[Random[Real, {-1, 1}], {100}]];

ListPlot[%, PlotJoined->True]

Two-Dimensional  Random Walk

FoldList[Plus, {0, 0}, Table[Random[Real, {-1, 1}], {100}, 
         {2}]];

ListPlot[%, PlotJoined->True]

Three-Dimensional  Random Walk

FoldList[Plus, {0, 0, 0}, Table[Random[Real, {-1, 1}], {100},
         {3}]];

Show[Graphics3D[Line[%]]]

More on Programming

There are several styles of programming in Mathematica.  One of them is procedural programming, as you would find in a standard structured programming language such as C or Pascal.

Another is "rule-based programming". The idea is to give transformation rules which specify how Mathematica  should transform expressions it receives as input. 

You can give rules that mimic the formulas you might find in a mathematics textbook.

Here is an example of how you might teach Mathematica  about a new form of logarithm function, called nlog.

nlog[a b c d^2]

Mathematica  initially knows nothing about our new function, so it does nothing to expressions involving nlog.

nlog[x_ y_] := nlog[x] + nlog[y]

This tells Mathematica  how to expand  logarithms of products.

nlog[a b c d^2]

Now Mathematica  can expand the nlog out.

nlog[x_^n_] := n nlog[x]

This gives a rule for nlog of a power.

nlog[a b c d^2]

Now Mathematica  can expand the expression completely.

Another style of programming you can use in Mathematica  is functional programming.  In functional programming, you specify a collection of functions to apply.  This style of programming often yields compact, elegant programs that make good use of Mathematica's many integrated capabilities.

This is a program that plots the solutions to a polynomial equation as points in the complex plane.

RootPlot[poly_, z] :=
  ListPlot[{Re[z], Im[z]} /. NSolve[ poly == 0, z],
                        AspectRatio -> Automatic ]

You can immediately use the program to plot solutions.

RootPlot[ z^7 - 1 , z];

Here is a program written in functional programming style, which finds the first n terms in the continued fraction decomposition of the number x.

ContinuedFraction[x_Real, n_Integer] :=
 Floor[ NestList[ 
     Function[{u}, 1/(u - Floor[u])], x, n - 1] ]

ContinuedFraction[.34252515, 7]

The Mathematica User Interface

Mathematica  consists of two parts - the "kernel", which actually does computations, and the "front end", which deals with interaction with the user.  The kernel of Mathematica  is essentially the same on all computers that support Mathematica.  The front end, on the other hand, works differently on different kinds of computers.

Notebooks

One of the most important aspects of the X front end is its ability to support Mathematica  "notebooks".  

This file is an example of a notebook.  Notebooks consist of a mixture of text, graphics, sounds and Mathematica  input.

Notebooks can be used like "interactive textbooks" -you read the text in the notebook, then use the Mathematica  input in the notebook to perform calculations.