Math 126A-01Calculus II
Introduction to Using Mathematica

Using the Help Browser

The Help Browser has all the information you need to run Mathematica, including the complete contents of the  Mathematica Book. After opening the Help Browser by selecting it from the Help menu, click on the button Getting Started/Demos and look for the item Tour of Mathematica. This will give you a quick overview of how Mathematica works.

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 Kernal 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 mathmematical 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 * 456

is the same as

123 456

You must press the Enter key or the key combination Shift-Return to enter a command to be evaluated by the Kernel.

a  =  123^456

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^456;

You can refer to the previous output using a single %.

12/34  +  56/78

b  = % / 4

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

%5  +  %6

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

Basic 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, after selecting the Basic Calculations palette, the entry for Calculus/Common Operations brings up symbols for integration and differentiation. 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 exmaple:

∫(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]

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]

Mathematica 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

Clear[x, y, a]

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 axpression. 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]

Lists, Equations and Substitutions

The standard way to group informatiuon 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: { }. The following are simple examples:

{a, b, c}{{1, 2}, {3, 4}}{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 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_]  :=  9  x^4  - 38  x^3  + 6  x^2 + 18  x

Solve[f'[x]  ==  0]

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

f[x]  /.  %

Many functions in Mathematica have a numerical form that replaces symbolic computations with numerical algorithms. For example,

NSolve[f'[x]  ==  0]

f[x]  /.  %

Plot[f[x],  {x,  -1, 4}]

Another common example is numerical integration:

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

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

Plotting Graphs

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]}]

By default, Mathematica keeps the horizontal size of a plot in fixed proportion to its vertical size so that it better fits the page.

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

To get equal scales on both axes set the option AspectRatio to Automatic:

Plot[Sqrt[1  -  x^2], {x, -1, 1},  AspectRatio -> Automatic]

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

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

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

To plot a curve defined parametrically by two functions x = f[t], y = g[t], use:

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

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