Math 236  •  5 February 2001

Mathematica Introduction—Part 2

Your name(s):

Mathematical notation in Mathematica

Names of built-in objects

All of Mathematica's built-in names begin with upper-case letters, and unlike in most traditional elementary mathematical notation, names may consist of multiple letters.  In fact, most built-in function names are entire words or unspaced phrases.  For example:

Limit[(((z^2) -  9)/(z  -  3)),  z  ->  3]

FactorInteger[5625]

A handful of the most common elementary transcendental functions keep their usual abbreviations—but with initial upper-case letters of course:

ArcTan[Sin[(π/2)]]

Exponents, fractions, and square-roots

To enter an exponent, you may use the character ^ inline.  For example, type x^2 for the square of x.  To get raised exponents, use the Ctrl ^ key combination, type the exponent, and use the mouse or cursor keys to move to the right beyond the exponent.
Similarly, to enter a fraction, you may use the / character inline.  For example:  2/3  To get a multi-line built-up fraction, use the Ctrl / key combination.
To get a square root, you may use the Sqrt function inline.  For example:  Sqrt[2]  To get a traditional square-root symbol, use the  Ctrl 2 key combination.
To leave superscript, fraction , or square root mode and return to normal typing on the line, type Ctrl␣.  Or, use the right-cursor key.

Try it now:  In new input cells, enter the Mathematica equivalent of the traditional mathematical notation
\(TraditionalForm\`\@\(\(z\^\(\(2\)\(\ \)\) - \ 9\)\/\(z - \ 3\)\)\  + \ 6\) first inline and then as a multi-line display.

Common math constants

To enter common the math constants traditionally denoted e, π, i (that is, (Sqrt[-1])), and ∞, you may type E, Pi, I, and Infinity, respectively.  To get their more traditionally-appearing forms e, π, and i, and ∞, use paired Esc (Escape) key sequences.  For example, type π as EscpEsc.  (Pressing Esc will cause the glyph    to appear temporarily in the cell.)

Constants and functions

Clearing

Before introducing a new a name for a constant or a function, it's a good idea to clear any values it may previously have been given:

Clear[a2bc,  myNumber]

Assigning constants

To assign a constant value to a name, use the form name = expr with an equals sign, like this:

myNumber  =  a2bc + 5

To suppress the uninteresting output from evaluating such an assignment—or the output from any expression whatsoever—just terminate the expression with a semicolon:

myNumber  =  a2bc  + 5 ;

Once assigned a value, the name can of course be used in subsequent expressions:

myNumber/2

a2bc  =  7;

myNumber/2

Multiplication

Since Mathematica—like many other programming languages but unlike conventional school mathematics—allows multi-character names such as myNumber and a2bc, simply juxtaposing two letters does not indicate multiplication:

a  =  2;  b  =  3;
ab

a b
a*b
a×b

The multiplication sign × in the product expression a×b was typed as Esc*Esc

Working with functions

⚠  As you have already seen, the arguments of a Mathematica function are enclosed in square brackets and not in parentheses:

Exp[0]

Parentheses are used in Mathematica to group expressions.  Try this: Explain the output from the three expressions in the following cell:

Exp (0)
Exp (1)
Exp (2)

If a function has several arguments, they are separated with commas.  For example:

Log[2,  32]

Random[Integer,  100]

Random[Integer,  100]

To define a function of a single argument in Mathematica, use the form fname[var_] := expr, where the expression expr will typically involve the symbol var.  Be sure to append the underscore (⇧ - ) to the argument name var.  And be sure to use colon equal ( :=  ), not just equal.  For example:

picture[c_]  :=  Plot[Sin[c x],  {x,  0,  2  π}]

picture[1]

Table[picture[coef],  {coef,  1,  5}]

Do the obvious thing to define a function of several arguments.  Try this:  Define a function of two arguments—which in use will be real numbers—that calculates the distance between them.  For example, if the arguments are given the values 3 and 8, then the result of evaluating your function should be 5.  (Hint:  Use the absolute value function Abs.)

Getting built-in help (important!)

Perhaps this section is the most important in this tutorial: it tells how to get help from Mathematica itself.  There are two basic ways—evaluating an information expression and using the help browser.

Information

To get a brief summary of the syntax and meaning of a built-in object, evaluate a cell with an information expression like the following:

?Integrate

Try it now:  Use the information you obtained about Integrate to figure out how to calculate with Mathematica an indefinite integral (antiderivative) of x Exp(- ((x^2)/2)).
⚠ Recall that arguments to Mathematica functions are enclosed in square brackets, not parentheses!

Here's another information request:

?Plot

The derivative of a function may be denoted by a prime, for example:

Cos'[x]

Clear[f]
f[x_]  :=  0.75  Cos[2  x] Exp[-(x/10)]
f'[x]

Try this:  Guided by the information message you obtained about Plot, on the same axes plot the graphs of both f[x] and f'[x] over the interval from -2π to 2π.

Asking for information about a name that you have defined will display its definition:

?f

If you know just part of the name of an object, you can use wild cards in the information request.  For example:

?Int*

Try it now: Find out what objects have names ending in Solve.

Information long form

To get more information about an object than a single question mark information request provides, use a double question mark form.  This provides the basic information, the options (optional arguments of the form optname -> val), and the so-called "attributes".  For example:

?? Plot

?? Cos

The help browser (very important!)

The Help Browser provides (nearly) complete documentation about Mathematica. In fact, the Help Browser contains the entire text of Stephen Wolfram's 1,496-page definitive reference, "The Mathematica Book".

To open the Help Browser, from the Help menu select Help Browser.  Then navigate to find the topic you want.  It's fairly intuitive how to do this.  But if you need help on how to use the Help Browser, ask a human: it's much easier for someone to show you than to describe how in words.

Try it now:  Use the Help Browser to find out about the built-in function Solve.

Many Help Browser entries include at the end a section that looks like the following:

Further examples

(examples would go here)

Such a section is just a specially formatted close group of cells.  To see the further examples, click on the little triangle to open the group and see the examples.

The display in the large window at the bottom of the Help Browser is, in fact, just another Mathematica notebook, albeit one with a specialized style.  So you can copy examples from the Help Browser window and paste them into your own notebook.

Try it now:  Copy below into this notebook one of the "Further examples" input cells that you found in the help about Solve.  Then evaluate the input cell—perhap after modifying the example.

Another way to open the Help Browser for help about a built-in object name is to follow the More... hyperlink you see whenever you use the ?name form of information request.  Try it now:  Evaluate the following cell, then click on the More... hyperlink at its end.

?N

What's next?

If you have time at this lab session—you should!—continue this introduction by opening the notebook Intro3.nb.  Otherwise," clean up" as indicated  below.

Cleaning up