Math 421  •  September 2006

Mathematica Introduction—Part 2

Your name(s):

From now on, section and subsection groups of cells in my Mathematica notebooks are ordinarily closed, so you can discern the overall structure of the notebook.  Remember to open closed groups to see what's inside them!

Mathematical notation in Mathematica

Names of objects

All of Mathematica's built-in names begin with upper-case letters (or for some special names, with $).  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)]]

Names that you invent should ordinarily begin with a lower-case letter.

Try the following and be sure you understand the output:

arcTan  =  Pi/4
arcTan[1]
ArcTan[1]

Since names may consist of more than a single letter, be especially careful when entering something like the Mathematica version of a symbolic expression a  +  b i: be sure to put a space between the symbol that's multiplying i.  Try the following and be sure you understand what happened.

a  =  2;  b  =  v; 
a  +  bI
a  +  b I

When the first factor in a product is an actual number, you do not need any intervening space.  Examine the following input cell carefully—run your cursor along from character to character to observe there is no space between the 3 and the I.  Then evaluate the cell.

2  +  3  I

Exponents, fractions, and square-roots

To enter a superscript , 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 in Mathematica their more traditionally-appearing forms 
	e, π, and i, and ∞, 
use paired Esc (Escape) key sequences.  For example, you would type π as EscpEsc.  (Pressing Esc will cause the glyph    to appear temporarily in the cell.)  
Try these:

(1) In a new input cell type the Mathematica expression traditionally appearing as the following; then evaluate it.
	(i^( 2))
[Hint: The shortcut key sequence for i is EsciiEsc (with a double i).

(2) In a new input cell type the Mathematica expression traditionally appearing as the following; then evaluate it.
	(e^(2  π i))
[Hints: What do you think the shortcut key is for the complex number e?  Also, the two-character name πi is not the same as the two one-character names π and i in the product π i.]

Equality and ordering

Equality is denoted by two equal signs.  Evaluate the following two cells now.

2  ==  1  +  1

3  ==  5  -  1

Try typing a similar equality between numbers yourself now.  Create a new cell below this one; in it type the expression saying that 3 equal 6 divided by 2.  Do it now.

Did you notice what happened as you typed the two equal signs in a row?  As soon as you typed the next character (either 3 or a space), the form of the two equal signs changed to a stylized pair of narrower equal signs.  That happened because Mathematica understands certain sequences of keystrokes to be abbreviations.

How do you think less-than-or-equal is typed?  Try it:  Create a new cell below this one and in it type an expression that results in 2 ≤ 3.  Then evaluate 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  I

The output from that input was not especially interesting, and this is often the case when you assign a value to a symbol.  The resulting output can be supressed.

To suppress the result from evaluating an expression, terminate the expression with a semicolon.

Try this and compare what happens with what happened above:

myNumber  =  a2bc  + 5  I ;

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

myNumber/2

a2bc  =  7;

myNumber/2

Notice that the value of myNumber did not change, even though you just assigned the new value 7 to a2bc and had originally set myNumber = a2bc+5.

Recall that equality is denoted by two equal signs, not one.  What happens if you mistakenly use only a single equal sign?  Try it now:

2  =  1  +  1

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  +  3  I;  
    b  =  4  +  5  I;
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.  For example:

Re[3  +  4  I]
Im[3  +  4  I]

Abs[3  +  4  I]

Conjugate[3  +  4  I]

z  =  3  +  4  I;
z Conjugate[z]

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

Abs (3  +  4  I)

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

Log[2,  32]

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:

curve[t_]  :=  
        ParametricPlot[{Cos[θ],   Sin[θ]},  {θ,  
            0,  t},  AspectRatio  ->  Automatic];

curve[π/2]

Table[curve[(π/2) k],  {k,  1,  4}]

Do the obvious thing to define a function of several arguments.  For example, for a function of two arguments:
	fname[var1_, var2_] := expr
Try this:  Define a function of two arguments—which in use will be complex numbers—that calculates the distance between them.  For example, if the arguments are given the values 7  +  10  i  and 4  +  6  i, then the result of evaluating your function should be 5.  (Hint:  Use the function Abs, which finds the "modulus" of complex numbers.)

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'[z]

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

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π.  (Save lots of typing: copy and paste!)

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 beginning with Complex.

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,688-page definitive reference, "The Mathematica Book", 5th ed.

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.  Below, click the little triangle on the right to see what happens.

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.

Try it now:  Open the group of further examples in the Help Browser help about Solve.

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 the end of the resulting information.

?N

What's next?

If you have time at this lab session—you should!—continue this introduction with Intro3.nb.  Get that from the Math 421 web site Files page  (You did click on the underlined, blue hyperlink in the preceding sentence, didn't you?)  Open that notebook and work through it.
Otherwise, if you're out of time, then "clean up" as indicated  below.

Cleaning up