MATH 225: Calculus III
Introduction to Mathematica

The purpose of this notebook is to give you a quick introduction to using Mathematica and to show you the functions and procedures you need to know to complete some of the assignments for the course.

Using the Help Browser

The Help Browser has extensive documentation for using Mathematica. Open the Help Browser by selecting it from the Help menu, and click on the button Getting Started/Demos. The items listed there will give you an overview of how to get started using  Mathematica. The Help Browser is also a handy reference for looking up the names of commands and how to use them. If you hear a beep when you enter a command it is because Mathematica thinks something is wrong. Click on the item Why the Beep?… in the Help menu to find out more.

The Front End vs the Kernel

The mathematical brain of Mathematica is located in a separate (invisible) part of the program called the Kernel. You interact with the Kernel by typing commands into the Front End,  the part of the program you are using now that displays Mathematica notebooks. The Kernel remembers everything that you enter into the Notebook as a command. After using Mathematica for a while you may wish to have the Kernel "forget" and start over again. You can always quit Mathematica and start over again, but you can also achieve the same result by selecting Quit Kernel from the Kernel menu without having to restart the Front End.

Basic Calculations

Arithmetic calculations use the usual symbols, +, -, *, / for addition, subtraction, multiplication and division, respectively, and ^ denotes exponentiation. Mathematica also allows you to use a space to denote multiplication. This convention follows normal mathematical notation and is very convenient, but you must be careful to remember to include a space (or a *) when you mean multiplication. For example:

123* 45

is the same as

123 45

You must press the Enter key on the numeric keypad or the key combination Shift-Return to enter a command to be evaluated by the Kernel. On many PC keyboards, the Return key is labelled Enter, so be careful not to get confused on this point (pressing the Return key by itself just puts the cursor on the next line of the input cell).

123^45

As you can see, Mathematica can handle very large numbers. The output  of a command can be suppressed by putting a semicolon at the end of it.

a  =  123^45;

You can refer to the previous output with the symbol %.

b  = % / 1234512345

You can also refer back to a previous output using its corresponding number. For example, the fourth output line is stored in Out[4], which can also be denoted by %4.

%4 / 1234512345

Mathematica tries keep all its calculations in symbolic form. To convert a number into decimal notation, use the function N[]:

N[%]

A function can also be attached at the end of an expression with //. For example,

N[(2/3)^4]

is the same as

(2/3)^4  // N

Built-in Functions

Mathematica has many built-in functions, including all the familiar scientific functions like Sin[], or Exp[], as well as functions for performing operations like taking a derivative or integrating. The built-in function names are always capitalized and are usually spelled out completely.

Plot[E^(-x/3 ) Sin[2  x],  {x, 0, 2  Pi}]

Note that the exponential function can be written either as Exp[x] or as E^x. Special constants like e or π are also written out and capitalized.
Lists (items enclosed in braces { }) are used frequently in Mathematica  for both input and output.  The above Plot[] command used a list {x,0,Pi} to signify that the variable is x and runs from 0 to 2Pi. 
Under the File menu is a submenu of Palettes. Selecting one of these will bring up a floating panel which you can use to simplify entering a command. Some commands are given symbolic forms in these palettes. For example, the BasicInput palette (which may already be displayed by default), has symbols commonly used in calculus. Click on a button to enter a template into the Notebook. The ones for integration and differentiation look like:

∫\[Placeholder] \[DifferentialD]\[Placeholder]                     (∂_\[Placeholder]) \[Placeholder]

You can then fill in the blanks. For example:

∫(E^(-x/3 ) Sin[2  x]) \[DifferentialD]x

(∂_x)( E^(-x/3 ) Sin[2  x])

These command can also be entered by writing out their full names:

Integrate[E^(-x/3 ) Sin[2  x],  x]

D[E^(-x/3 ) Sin[2  x],  x]

Try selecting the word Integrate above and then slecting Find Selected Function… in the Help menu. The Help Browser will appear with the full explanation of the Integrate command and its various options. Notice that functions use square brackets: [ ]. Parentheses are used only for grouping terms in a mathematical expression.

Variables and Assignments

Any sequence of characters you enter will be interpreted by the Kernel as the name of a variable. For example,

x  =  123;  y  =  118;

delta  =  N[x  -  y]/x

A single equal sign = denotes assignment: the value on the right-hand side of the = is stored in the variable named on the left-hand side. The function Clear[] removes an assignment.

Clear[delta];

2*delta  -  x

Be careful to put spaces where you intend to have multiplication. If you enter

xSin[y]

The Kernel thinks xSin is the name of a variable. Putting a space between x and Sin[] gives

x Sin[y]

Another form of assignment is the delayed assignment which is denoted by :=. In this kind of assignment, the right hand side is not evaluated until the left-hand side is used. This distinction often does not make a practical difference, but it can be useful. In the following delayed assignment, the plot command itself is stored in the variable graph and there is no output.

graph  :=  Plot[ x Sqrt[1 + x^2], {x, -1, 1}]

With a simple assignment the plot is computed immediately and the result stored in the variable:

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

To see what has been assigned to a variable, enter the variable name preceded by a ?:

x  :=  a;  y  =  3;

?x

?y

You should get in the habit of clearing any variables you use in one problem before you move on to another problem. The Kernel will not forget what you assigned to variabels even if you do.

Clear[x, y]

Defining Functions

Some care must be taken to enter the definition of a function in Mathematica. The argument (or arguments) to the function must be followed by an underscore (_) to signify that the argument may be replaced by any expression. Delayed assignment is not required, but is usually a good idea in defining a function. Here's an example:

f[x_]  :=  E^(-x/3) Sin[2  x]

f[2]

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

Integrate[f[x], {x, 0, 2  Pi}]

D[f[x], x]

When a function has been defined in this way, a derivative can be calculated using the prime notation:

f'[x]

Using an underscore x_ in the definition of f allows x to be any other expression. For example,

f[y]

f[(2  x  -  y^3)]

If we do not use an underscore, the function does not work as expected.

g[x]  :=  x^3  -  x;

g[2]

g[y]

g[x]

Functions of several variables are defined is a similar way.

f[x_, y_]  :=  -3 
        y/(1 + x^2  +  y^2)

f[x_, y_, z_]  :=  
      8 (x^2 + y^2 + z^2) - 8 (x^4 + y^4 + z^4)

The function D[] automatically performs partial derivatives.

D[f[x, y], x]

D[f[x, y], y]

D[f[x, y, z], z]

Lists, Equations, and Substitutions

The standard way to group information in Mathematica, both for input and output, is by using lists. A list is any sequence of objects separated by commas and enclosed in a set of braces: { }. For example, a 3D vector in Mathematica is denoted by a list like {a,b,c}, a 2×2 matrix by a list of lists like {{1,2},{3,4}} and a list of equations by something like {x^2 - y == 1, x + 2 y == 4}. An equation in Mathematica is denoted by a double equal sign == to distinguish it from an assignment =. Equations are most commonly used with the Solve[] command. For example,

Solve[{x^2  -  y  ==  1,  x  + 2 y  ==  4}]

Notice that both the input and output is formulated as a list. The arrow notation is used for substitutions. The syntax for substituting a value for a variable is: expression /. {variable -> value}. For example,

x^2  -  y  /.  {y -> (5/4), x -> (3/2)}

x  + 2 y  /.  {y -> (5/4), x -> (3/2)}

As another example, say we want to calculate the critical points of the function

f[x_, y_]  :=  3  x  - (x^3) - 2  (y^2) +  (y^4)

solns  =  
      Solve[{D[f[x, y], x] ==  0,  D[f[x, y], y] == 0}]

The value of f[x,y] at the critical points can be calculated easily by substituting the previous result into f[x,y]:

f[x, y]  /.  solns

To pick out a particular element from a list you can you double square brackets: [[ ]]. For example, the variable solns defined above contains a list of the three substitutions. To pick out the second one use solns[[2]].

f[x, y]  /.  solns[[2]]

Many functions in Mathematica have a form that uses a numerical algorithm (computing with numbers in decimal form) instead of symbolic computation. For example,

Solve[x^5 + 2  x^3 + x^2 == 1]

NSolve[x^5 + 2  x^3 + x^2 == 1]

If Solve[] is unable to solve an equation, try FindRoot[]. The variable and an initial guess must be given.

FindRoot[ Cos[x]  == x,  {x, 0.5}]

Another common example is numerical integration:

Integrate[E^(Sqrt[1 + x^4]), {x, 0, 1}]

NIntegrate[E^(Sqrt[1 + x^4]), {x, 0, 1}]

2D Plots

To graph a function use Plot[]:

Plot[x^3  -  x,  {x, -2, 2}]

Notice that the first argument to Plot[] is any expression and the second is a list specifying the variable and the interval over which to plot the graph. There are many optional arguments that control the appearance of the plot. For example, to give the plot a color use:

Plot[x^3  -  x,  {x, -2, 2},  
      PlotStyle -> {Hue[ .1]}]

Several functions can be plotted at once by combining them into a list:

Plot[{x^3  -  x,  3  x^2  -  1} , {x, -2, 2},  
      PlotStyle -> {Hue[ .1],  Hue[ .7]}]

To control the vertical range of the plot set the option PlotRange:

Plot[9  x^4 - 5  x^3,  {x, -2, 2}]

Plot[9  x^4 - 5  x^3,  {x, -2, 2},  
      PlotRange -> {-0.5, 1}]

To plot a curve defined parametrically by two functions x = f[t], y = g[t], for values of t ranging from a to b, use ParametricPlot[{f[t],g[t]},{t,a,b}]. For example,

ParametricPlot[{Cos[t], Sin[t]},  {t, 0, 2  Pi}]

By default, Mathematica keeps the horizontal size of a plot in fixed proportion to its vertical size so that it fits better on the page. To get equal scales on both axes set the option AspectRatio to Automatic:

ParametricPlot[{Cos[t], Sin[t]},  {t, 0, 2  Pi},  
      AspectRatio -> Automatic]

Different plots can be combined with the command Show[]:

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

tang  =  
      Plot[Sin'[2] (x - 2)  +  Sin[2],  {x, 1, 3},  
        PlotStyle -> Hue[ .1]]

Show[func, tang]

3D Plots

To graph a function of 2 variables use Plot3D[]:

Plot3D[(x y^3)/(1 + x^2 + y^6), {x, -4, 4}, {y, -4, 
        4}]

To get a smoother graph, increase the number of points plotted using the option PlotPoints .

Plot3D[(x y^3)/(1 + x^2 + y^6), {x, -4, 4}, {y, -4, 
        4}, PlotPoints -> 50]

Different 3D plots can also be combined with the command Show[]:

func1  =  
      Plot3D[4 - x^2 - y^2, {x, -2, 2}, {y, -2, 2}, 
        BoxRatios -> Automatic]

func2  =  
      Plot3D[2 - x y, {x, -2, 2}, {y, -2, 2}, 
        BoxRatios -> Automatic]

Show[func1, func2]

The point of view can be changed with the option ViewPoint. The easiest way to do this is to use the menu item Input/3D ViewPoint Seletor...

Show[func1, func2, 
      ViewPoint -> {-1.415,  -1.968,  2.361}]

3D plots of curves and surfaces can be created with ParametricPlot3D[]

ParametricPlot3D[{Sin[t], Cos[t], t/π}, {t, 0, 
        6  π}]

ParametricPlot3D[{u Sin[v],  u Cos[v],  v/π}, {u, -1, 
        1}, {v, 0, 3  π}]

Level Curves and Surfaces

Level curves of a function in the plane, f(x, y)  =  c,  can be plotted using ContourPlot[].

f[x_, y_]  :=  -3 y/(1 + x^2  +  y^2)

ContourPlot[f[x, y], {x, -4, 4}, {y, -4, 4}]

The shading can be turned off with the option ContourShading.

ContourPlot[f[x, y], {x, -4, 4}, {y, -4, 4}, 
      ContourShading -> False]

To limit the number of contours plotted, use the option Contours. For example to plot f(x, y)  =  1:

ContourPlot[f[x, y], {x, -4, 4}, {y, -4, 4}, 
      ContourShading -> False,  Contours -> {1}]

To plot f(x, y)  =  1, f(x, y)  =  0, and f(x, y)  =  -1/2:

ContourPlot[f[x, y], {x, -4, 4}, {y, -4, 4}, 
      ContourShading -> False,  
      Contours -> {-1/2, 0, 1}]

For level surfaces in space, f(x, y, z)  =  c, we need to invoke a special package:

<< Graphics`ContourPlot3D`

The following function has some interesting level surfaces.

f[x_, y_, z_]  :=  
      8 (x^2 + y^2 + z^2) - 8 (x^4 + y^4 + z^4)

ContourPlot3D[
      f[x, y, z], {x, -5, 5}, {y, -5, 5}, {z, -5, 5}, 
      Contours -> {2}, PlotPoints -> 10]

ContourPlot3D[
      f[x, y, z], {x, -5, 5}, {y, -5, 5}, {z, -5, 5}, 
      Contours -> {3}, PlotPoints -> 10]

ContourPlot3D[
      f[x, y, z], {x, -5, 5}, {y, -5, 5}, {z, -5, 5}, 
      Contours -> {4}, PlotPoints -> 10]