Math 455  •  8 February 2009

Introduction to Mathematica

Part 2: Names, constants, and functions

Your name(s):

Mathematical notation in Mathematica

Names of 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)]]

arcTan=Pi/4

ArcTan[1]

arcTan=ArcTan[1]

Since names may consist of more than a single letter, be especially careful when entering something a product of two quantities.  Try the following and be sure you understand what happened.

a = 2; b = v; 
ab
a b

However, 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.

2v

Exponents, fractions, and square-roots

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

Common math constants

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 a doubled equal sign.  Evaluate the following two cells now.

2 == 1 + 1

3 == 5 - 1

Try typing another 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.

You probably don't see a double equal sign above, since immediately after you type one = and then the second, Mathematica converts the pair to an elongated double equal.  To see the difference, do this:  Type each of the following in an input cell; then evaluate the cells.

	2 == 1+1   (* two equal signs in a row *)
 	2 = 1+1     (* just one equal sign

You should have obtained an error message from the second of these.  Why?

The notation for "not-equal" is another abbreviation.  To type the not-equal symbol ≠, type the not symbol ! (an exclamation point) followed immediately by the equals symbol =.  Try it now:  Create a new cell below this one.  In it type 3 != 2 and watch what happens as you type.  Then evaluate the cell.

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 Pi

Did you notice that the names myNumber and a2bc there are colored blue, whereas Pi is black?  Names of objects such as Pi that are built-in to Mathematica are ordinarily shown in black, whereas names that you make up are shown in blue.

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 +3 ;

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

myNumber/2

a2bc = 7;

myNumber/2

myNumber

Notice that the value of myNumber has changed, because you just assigned the 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; 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 (inputs) 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 (where Mathematica automatically inserted the red parentheses):

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

A function need not return the same value even though you supply the same input!  In fact, that's the way Mathematica simulates creating "random" numbers.

RandomInteger[100]

RandomInteger[100]

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

Log[2, 32]

Be sure to append the underscore (⇧ - ) to the argument name var on the left side of the definition (but not on the right side).  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}]

To create an interactive version of the above plots in which the coefficient c is variable, use Manipulate:

Manipulate[Plot[Sin[c x], {x, 0, 2π}], {c,1,5}]

Move the slider above to change the value of c and hence the frequency of the sinusoidal graph that's plotted.

Defining a function of two arguments is similar to the case of one argument.

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.)

In Mathematica you may define a function of any number of arguments.

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 Documentation Center.

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.75Cos[2x] 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.

The Documentation Center (very important!)

The Documentation Center provides complete, hyper-linked documentation about Mathematica.

To open the Documentation Center, from the Help menu select Documentation Center.  Then find the topic you want much as if you were using a web browser with a search bar at its top.

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

Many Documentation Center pages include a section like the following:

Examples

Basic Examples

(examples would go here)

You may not only read the examples there, but you may re-evaluate the input cells and even modify them (or copy them and modify them) right there on the Documentation Center page.

Try it now:  Modify the first example about Solve by changing the equation, then evaluate that input cell.

Notice that many cell groups on Documentation Center pages do not have cell brackets.  To open or close such groups, click the little triangle on the left side of the group's header cell.  (In Mathematica Version 7, the brackets of most groups, and some others, are hidden; to see them, hover the mouse near the right edge of the notebook window.)

Below the Basic Examples you'll usually see additional groups of examples, including Applications and, sometimes, Neat Examples.  These are often quite interesting—and worth studying.

Another way to open the Documentation Center from your notebook and jump directly to the "home page" for a built-in object name is to follow the » hyperlink you see whenever you use the ?name form of information request.  Try it now:  Evaluate the following cell, then click on th » hyperlink at the end of the resulting information.

?N

Palettes and prototypes

Mathematica includes a number of palettes useful for entering various kinds of input. To open a palette, select it from the Palettes menu item.

Palettes in Mathematica Version 7

Mathematica Version 7 has some palettes that provide very informative information about the syntax of built-in functions and just what kinds of arguments they take.

Try this:  
• In this notebook, create a new Input cell below this text cell and leave the cursor there.
• From the Palettes menu open the Basic Math Assistant palette.
• Under the Basic Commands heading on the palette, click the 3D tab.
• In the revealed 3D menu, click the first button now revealed: Plot3D.
   (In your input cell, you should now see an automatically inserted "prototype" for a Plot3D expression.)
• Into the displayed boxes in the prototype, type the appropriate inputs to plot the surface that is the graph of (x^2)-(y^2) for -1≤x≤1 and -2≤y≤2.
  You may move from one of the input boxes to the next by using the ⇥ key.
• Evaluate that input cell to see the graph.

What's next?

If you have time at this lab session—you should!—continue this introduction by opening the notebook Intro3.nb from the Math 455.1 web site Files page.

Otherwise, if you're out of time, then "clean up" as indicated  below.

Cleaning up