Why should you learn Mathematica?

Comp110 Course Module 0

Mathematica is an environment for solving and presenting many of the computational problems that you will encounter while you are here at Rice.  Our goal is to teach you how to use Mathematica to do your homework and projects.  Initially, we will focus on using Mathematica as a calculator and then quickly move to using Mathematica as a document preparation environment.  Finally, the majority of the class will focus on using Mathematica to write short programs that use Mathematica's own built-in functions to solve interesting computational problems relevant to Science and Engineering.  This short module will give you an overview of some of the interesting features of Mathematica.

Numerical computation

At its core, Mathematica is essentially the world's most sophisticated calculator.  To use this calculator, you simply click inside an existing Input cell (the orange box below) and hit Shift+Enter to compute the value of the expression appearing in the cell.  (Shift+Enter tells Mathematica to evaluate the cell in which the cursor resides.)  For example, try evaluating the cell below.

1+1

This first evaluation may have taken a couple of seconds.  The reason for this delay is that Mathematica is loading into the computer's memory a very large piece of code called the kernel that performs basic mathematical computations.  Subsequence calculations will be much faster.

Expressions in Mathematica can be entered in the standard textual form used in most computing languages.

2*3 -4/2+3^2

One powerful feature of Mathematica is that it has literally hundreds of pre-defined mathematical functions already defined.  For example, let's consider the problem of computing the product of the integers from 1 to 100.  In standard mathematical terminology, the function Factorial computes this product and is usually written as 100!.

100!

Note that the resulting whole number has hundreds of digits.  Mathematica, unlike most languages for computing (like Java or Matlab), has no restrictions on the size of numbers.

Mathematica also handles real (decimal) numbers exceptionally well.  For example, Mathematica understands about constants such as π.  Mathematica can give you the first 100 digits of π by using the function N that computes symbolic constants to their numerical form.  Evaluate the cell below clicking in the cell and hitting Shift+Enter.

N[π,100]

Next, let's compute the first 1000 digits of π by clicking inside the cell and adding a zero to 100.  Again, hit Shift+Enter and Mathematica will compute the first 1000 digits of π.

Usually, you are interested in entering and evaluating your own expressions.  To create a new Input cell, you simply place the mouse between two existing cells (a cell is roughly a chuck of text) and click.  The result should be a horizontal line.  Now, typing in text will expand the line into an Input cell.  As a test, let's compute the square of 5!.   Create a new cell, enter this expression and hit Shift+Enter. If you have done everything correctly, Mathematica should return 14400.

To find out the more about the functions that Mathematica supports, go to the Help menu and select Documentation Center.  This action will bring up the main window for Mathematica's help system.  Explore the entries in this window and locate a link to a listing of Mathematica's mathematical functions.  During the course of this class, you will become extremely familiar with the Docuemtation Center.

Symbolic computation

Another feature that makes Mathematica much more powerful than most languages for computation is Mathematica's ability to compute with symbolic expressions.  For example, in Java or Matlab, entering the expression x+x will yield an error if the variable x is undefined.  In Mathematica, having undefined variables in expressions is commonplace.  For example,

x+x

The basic mode of evaluation in Mathematica is that of simplification instead of computation.  Mathematica attempts to apply a set of built-in rules to simplify any expression that it encounters.  If the expression consists only of numerical values, Mathematica computes the expression and returns the appropriate numerical value.  However, if the expression contains undefined variables, Mathematica uses the rules of algebra (and beyond) to simply the input expression.  For example, Mathematica can be used to illustrate the binomial expansion of a polynomial

Expand[(((a+b))^10)]

Essentially, Mathematica encodes most of basic mathematical knowledge and much of advanced mathematical knowledge.  For example, Mathematica understands how to handle real numbers raised to an imaginary power.  Moreover, it has rules for handling this process exactly as if the values are known constants.  Here is a famous theorem of Euler.

(E^(I π))

Here, i is the imaginary number that is the square root of -1 and e is the constant 2.718281828459045` -- that is, the value of the limit

Limit[(((1+(1/n)))^n),n->∞]

One feature that is very useful for students in basic calculus is Mathematica's knowledge of sums and integrals.  For example,

∫Sin[x]\[DifferentialD]x
(∑_(i=0)^∞)(1/(2^i))

Mathematica is an essential tool for people who do lots of calculus.

2D Expression input and typesetting

As the semester progresses, you will become more and more familiar with the various numerical and symbolic capabilities of Mathematica.  Another interesting feature of Mathematica is its ability to represent mathematical expressions in a visually elegant manner beyond the standard 1D textual representation of languages like Java or Matlab.  For example, to compute 3 squared, in most languages one would normally enter the expression 3^2.  However, Mathematica has a WYSWYG (what you see is what you get) interface that allows you to enter the expression

(3^2)

There are several ways to enter such expressions in Mathematica.  The simplest way for new users is to use a palette to create this expression.  Open the Palettes menu and select BasicMathInput.  This action opens a menu on the right hand side of your screen that contains a number of expressions.  Clicking on one of these expression, adds the expression to your currently selected cell.  As an example, create the number π by clicking on π in the palette.

Some of the expressions have pieces that are white or black rectangles.  These rectangles are called placeholders.  Placeholders are dummy expressions waiting to be replaced by the user with their desired contents.  As an example, create the expression (e^(-i π)) using the placeholder for exponents.  You can move the cursor around inside the expression using your mouse or arrow keys.

These expressions can also be created very quickly using keyboard shortcuts. For example, you can create an exponent by typing Crtl 6 inside an existing expression.  Mathematica will automatically create a place holder at the appropriate location.  (See the Insert menu and Typesetting submenu for more details.)  Constants such as π can be created using the Escape key.  To enter π, simply hit Escape pi Escape.  (For more shortcuts, check out the SpecialCharacters palette.  As an exercise, create (e^(-i π)) again, but this time use only the keyboard.

While these examples may seem trivial, the payoff from good typesetting is that more complicated expressions are visually easier to parse.  For example, the roots of the quadratic equation a (x^2)+b x+c==0 have the form

Solve[a (x^2)+b x+c==0,x]

This 2D expression for the roots appears exactly the way it does in most math textbooks.  Compare that with the 1D textual form

(-b+Sqrt[b^2+4 a c])/(2 a)

2D and 3D graphics

Solving a problem using a computational tool such as Mathematica involves more than writing a mathematical function and typing in an equation; you need to present your solution in a manner that conveys the nature of the solution in a natural way.  The key tool in this process is to visualize your solution using graphics.  Mathematica also supports a rich set of 2D and 3D graphics functions and is well-suited to this task.  For example, Mathematica supports a rich set of 2D plotting functions

Plot[((Sin[6x])/x),{x,0,2π}]

Mathematica also supports 3D plotting.  Here is a nice example from the Documentation Center.

Plot3D[Sin[x y],{x,0,3},{y,0,3},ColorFunction->"Rainbow",Mesh->None]

Since this graphic is a surface in 3D, you can visualize the shape of surface better by clicking and dragging  the shape.  Mathematica will automatically rotate the surface in 3D in a natural manner.

Mathematica also allows the creation of custom 2D graphics in two ways.  First, Mathematica includes a simple, but elegant tool for creating 2D figures. To create a figure, you simply select New Graphic from the Graphics menu.  Mathematica will insert a new figure at the cursor.  You can add elements to the graphic by opening the Drawing Tools palette from the Graphics menu.  Below is a figure that I created using the tool.

You can also edit an existing 2D graphic using the drawing tool.  For example, we could copy the output of our previous 2D plot and use the drawing tools to add text or formulas.

Finally, Mathematica allows the user to create custom 3D shapes.   Here is another nice example from the Documentation Center. Note that green cuboid is semi-transparent.

Graphics3D[{Blue,Cylinder[],Red,Sphere[{0,0,2}],Black,Thick,Dashed,Line[{{-2,0,2},{2,0,2},{0,0,4},{-2,0,2}}],Yellow,Polygon[{{-3,-3,-2},{-3,3,-2},{3,3,-2},{3,-3,-2}}],Green,Opacity[.3],Cuboid[{-2,-2,-2},{2,2,-1}]}]

Simple interactive user interfaces

Mathematica has one more feature that is worth mentioning.  Often, the solutions to problems that you will work on are not simply static entities, but things that change dynamically.  Mathematica incorporates the capabilities to produce dynamic output.  In particular, Mathematica contains functions that allow the user to create a simple  interface where a variable associated with an expression can be manipulated dynamically by the user.  For example,

Manipulate[x!,{x,0,50,1}]

The function Manipulate is especially useful for visualizing the behavior of objects whose behavior varies with time.   For example,  in module 6, you will set up and solve for the motion of the Sun, Earth and Moon using Newton's law of gravity.  The key observation in this problem is that the strength of gravity decreases at a rate of (1/(r^2)), where r is distance between two bodies.

To give you an idea of what you will be doing by the middle of course, I've include Mathematica code that sets up and solves the equations of motion for the Earth and the Sun.  In my equations, I have forced gravity to decrease at a rate of (1/(r^α)) with α=2 initially.  For now, don't worry about understanding the Mathematica code, simply evaluate each of the four cells below.  Then manipulate the time slider in the last cell and observe that the Earth moves in a circular orbit around the Sun.

α=2

G= 4.981×(10^(-10));
ms=1.989×(10^30);
me=5.977×(10^24);

sunEarthInitConds = 
{xe[0]==1.495979×(10^8),
ye[0]==0,
xe'[0]==0,
ye'[0]==2.56721625×(10^6)};

sunEarthDiffEqs=
{xe''[t]==-G(ms/((((Sqrt[((xe[t])^2)+((ye[t])^2)])))^(1+α)))xe[t],
ye''[t]==-G(ms/((((Sqrt[((xe[t])^2)+((ye[t])^2)])))^(1+α)))ye[t]};

sol[t_]={xe[t],ye[t]}/.NDSolve[{sunEarthDiffEqs,sunEarthInitConds},{xe[t],ye[t]},{t,0,365}][[1]];

earth=ParametricPlot[sol[t],{t,0,365},AspectRatio->Automatic];
radE=6400 500;
earthDisk[t_]:=Graphics[{RGBColor[0,0,1],Disk[sol[t],radE]}]
radS=10 700000;
sun=Graphics[{RGBColor[1,1,0],Disk[{0,0},radS]}];

xmax=1.5*(10^8);

Manipulate[Show[{sun,earthDisk[t],earth},AspectRatio->Automatic,PlotRange->{{-xmax,xmax},{-xmax,xmax}},Axes->True],{t,0,365}]

As a final example, change the value of α in the first cell to be slightly smaller (say 1.9) and reevaluate the cells.  Now manipulate the slider.  How did the orbit of the Earth change?  When gravitational attraction is slightly stronger, the orbit of the Earth changes in an interesting manner.  Experiment with even smaller values for α.  What happens if you increase α?

I hope that this example gives you some idea of the power of Mathematica and how such a tool can enhance your understanding of Science and Engineering.