Introduction

Version 2.0
2/12/92
Originally "Tour Of Mathematica"
by Wolfram Research
Modified by John Schneider & John Weiss                                                        (Most modifications noted in "Changes" section)

Introduction to the Introduction

This NoteBook presents several different aspects of Mathematica.  It does not provide a detailed discussion of Mathematica, but it will give you some examples of the capabilities of Mathematica and expose you to the NoteBook structure.

     In some sections of this NoteBook it is assumed that you have executed the previous commands.  So, it is best to work through the commands in the sequence presented.

     Here are some points about scrolling and cell structure that you will need to know.

Scrolling

Using the mouse, click the scroll buttons in the bottom left of the window.  Your view of this file will scroll in the direction indicated by the arrow on the scroll button you choose.  Holding the mouse button down makes the scrolling continue until you release the button.  Notice that this scrolling causes the scroll bar to move vertically up and down.  The mouse can be used to directly drag the scroll bar to any desired position.

Cell Structure

NoteBooks are hierarchically structured in a manner similar to a table of contents in a book.  However, if one of the topics catches your eye, you do not thumb to the page corresponding to that topic;  rather, you double click on the cell grouping line that corresponds to that heading.  This will open the group and reveal individual cells that contain the text, formulae, and graphics associated with that heading.  A closed group of cells is indicated by a hook on the bottom of the cell grouping line.  An open grouping will not have a hook.  This section, "Cell Structure," is an open group of cells.  There is a cell containing the title and the cell containing this text.  The grouping line is on the far right, and it does not have a hook on the bottom.  To close a group, double-click on the grouping line to the right.  You should notice how the pointer changes shape when in position to click on the cell bracket.  To open a group, double-click again on the hooked line. (From now on, it is up to you to open the group in which you are interested.)

Numerical Calculations

You can do arithmetic with Mathematica just as you would on a calculator.  You type the input 5 + 7, hit "Enter", and Mathematica prints the result 12.  Since 5 + 7 is already typed for you in the cell below, simply click anywhere in the cell below with the equation and then hit "Enter."  Note that you must hit the "Enter" key (the one on the numeric key pad) to perform the calculation Ð not the return key.  Alternatively, you can hold the shift key and hit the return key.

5 + 7

Unlike a calculator, however, Mathematica can give you exact results.  Here is the exact result for 3 to the power 100.  The ^ is Mathematica notation for raising to a power.

3 ^ 100

You can use the Mathematica function N to get approximate numerical results.  The % stands for the last result.  The answer is given in scientific notation.

N[%]

You can find numerical results to any degree of precision. This calculates the square root of 10 to 40 decimal places.  The text enclosed in the (* *) delimiters is ignored by Mathematica.

N[ Sqrt[10], 40 (* Give answer to 40 decimal places *) ]

Mathematica can also handle complex numbers.  Here is (3 + i4)^10.  In Mathematica, "I" stands for the imaginary number square root of ±1.

(3 + 4 I) ^ 10

Mathematica can evaluate all standard mathematical functions.  Here is the value of the Bessel function Jo(10.5).

BesselJ[0, 10.5]

You can do numerical integrals.  Here is the numerical value of the integral of sin(sin(x)) from 0 to p.

NIntegrate[ Sin[Sin[x]], {x, 0, Pi} ]

Mathematica can do many kinds of exact computations with integers.  FactorInteger gives the factors of an integer.

FactorInteger[ 20654065386 ]

Note that all built-in Mathematica commands start with a capital letter.

Delimiters

Parentheses

Parentheses are used for grouping.  Without parentheses, multiplication and division have a higher precedence than addition and subtraction.

Square Brackets

Square brackets are used for specifying arguments to functions.  For example, the following statement calculates the square root of the sine of ±p/2.

Sqrt[ Sin[-Pi/2] ]

Some functions take a fixed number of arguments while others can be called with zero or more arguments.

Curly Braces

Curly Braces are used for specifying lists, vectors, and matrices.  A list or vector is several expressions separated by commas and enclosed in curly braces.  The command shown below sets the variable "m" equal to a 3 by 3 matrix.

m = {{1,2,3},{4,5,6},{7,8,9}}

The function MatrixForm displays a matrix in a more conventional form.

MatrixForm[m]

Double Square Brackets

Double square brackets are used for indexing, i.e., for referencing an object or set of objects in a list.  Suppose we have a vector "v" shown below.

v = {b,c,d}

The notation v[[i]] returns the ith element in the vector or list called "v".

v[[2]]

Using the matrix "m", we can manipulate rows or elements. Recalling the assignment for "m" from the above section, we can add row 1 to row 2.

m[[2]] = m[[1]] + m[[2]]

MatrixForm[m]

Comments

Text between (* and *) delimiters is not evaluated.  It is taken to be a comment.

Sqrt[6. (*This command evaluates the square root of 6.*)]

On-line Help

Most names of objects in Mathematica are complete words.  Mathematica rarely uses abbreviations.  Abbreviations are only used where they are well known.  For example:
     Abs     absolute value of a number
     Cos	     cosine
     D         derivative
      
     The ? operator can be used to obtain online help.  The symbol * used with the ? operator acts as a wild card character, i.e., it can match any alphanumeric character or sequence of characters.  If more than one command matches the request, Mathematica lists the names of all the commands. 

     Given ?Plot* as input, Mathematica lists the commands that begin with the word Plot.

?Plot*

If only one command matches the request, Mathematica will print the usage statement associated with the command.  The usage statement typically consists of a template showing how to use the command and a brief description.  Here is the information for Plot.

?Plot

The usage statement tells us that this command plots a function over a specified domain.  For example, evaluate the command below.

Plot[ Exp[-x], {x,-1,5} ]

Use ?? to obtain more information.  (This often provides more detail than you need.)

??Plot

Editing

Revising Text

To change a single character or word, position the vertical I-beam pointer to the right of the item to be deleted, click the mouse once, and press the backspace key to remove the text.  To insert text, just begin typing.  For larger text revisions as well as entire cells, it is more efficient to use the Undo, Cut, Copy, and Paste keys.  These are described below.

Undo

Undo reverses your most recent typing or any editing action that used the Cut, Copy, or Paste keys.  It can be activated by simultaneously pressing the Command and Undo keys.

Cut

Cut deletes selected text, graphics, or cells from your NoteBook and transfers them to the Pasteboard (special allocated memory).  To cut selected material from a Notebook and place it in the Pasteboard:

     1.  Select the material to be cut.  To select an entire cell, click its bracket.  To
           select text inside a cell, drag over the desired text with the mouse.
     2.  Press Command and the Cut keys simultaneously.

Copy

Copy also moves selected text into the Pasteboard but, unlike the Cut command, it does not remove the selected text.

Paste

This command inserts a copy of the Pasteboard contents at the current insertion point (chosen with the mouse).

Creating New Cells

You can create a new cell by placing the cell insertion point at the bottom or top edge of the existing cell and typing.  The mouse pointer will change to a horizontal I-beam when the mouse is properly positioned.

Graphics

Here is a plot of the function sin(x3), with x ranging from ±2 to 2.

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

Mathematica chooses appropriate scales for plots, even when there are singularities.

Plot[ 1 / Sin[x], {x, 0, 10} ]

Three-Dimensional Plots

This makes a contour plot of the function sin(x) sin(3y).

ContourPlot[ Sin[x] Sin[3y], {x, -2, 2}, {y, -2, 2} ]

Mathematica can also make three-dimensional pictures.

Plot3D[ Sin[x] Sin[3y], {x, -2, 2}, {y, -2, 2} ]

This displays the surface as seen from a different view point.

Show[ %, ViewPoint -> {1, 0, 1} ]

Here is the original picture, with a finer grid of sample points, and with shading determined by simulated illumination.

Plot3D[ Sin[x] Sin[3y], {x, -2, 2}, {y, -2, 2},
        PlotPoints -> 30, Lighting -> True ]

Ho cancellato le animazioni, per evitare ulteriori errori 
(8.12.98)

Making Definitions

This defines a value for the variable "v".

v = 1 + x

Now the value you have defined for "v" is used whenever v appears.

5 + 2 v + 3 v^2

Expand[expr] expands out products and positive powers in expr.

Expand[%]

This defines a function "f".  The definition can be thought of as a rule for transforming expressions of the form f[anything].

f[x_] := x^2

The occurrences of "f" in an expression like this are transformed according to the rules you have given.

f[3] + f[a+b]

You can define functions of multiple variables.  This defines the function "f1", which depends on two variables.

f1[x_,y_] := 2 x^2 + x y + Sqrt[y]

Mathematica will evaluate as much of the expression as possible.  Unknowns are left as unknowns.  Here are two examples.

f1[a,4]

f1[3,4]

Here is the recursive rule for the factorial function.

fac[n_] := n fac[n-1]

This gives a rule for the end condition of the factorial function.

fac[1] = 1

Here are the two rules you have defined for fac.

?fac

Mathematica can now apply these rules to find values for factorials.

fac[20]

Algebraic Formulae

Mathematica can work not only with numbers, but also with algebraic formulae.  Here is the formula (x + y)2 + 9 (2+x)(x+y).

(x + y)^2 + 9 (2 + x) (x + y)

This does the algebra to expand products and powers.

Expand[ % ]

Here is the third power of the expression.

% ^ 3

Expanding this gives a somewhat more complicated result.

Expand[ % ]

Factoring the previous expression puts it in a much simpler form.

Factor[ % ]

Mathematica can do many kinds of algebraic computations.  This gives an exact formula for the integral of x/(1-x^3) dx.

Integrate[ x / (1 - x^3), x ]

You can also find approximate formulae.  This computes the power series expansion of e±x sin(x) about the point x = 0 up to order x^6.

Series[ Exp[-x] Sin[2x], {x, 0, 6} ]

Lists

This makes a list of the first twenty factorials.

Table[ n!, {n, 1, 20} ]

This takes the logarithm of each entry in the list, and evaluates the result numerically. Functions like Log have the property of being ``listable'', so that they apply separately to each element in a list.

N[ Log[ % ] ]

Here is a plot of the entries in the list.

ListPlot[ % ]

Mathematica uses lists to represent vectors.  Here is the dot product of two three-dimensional vectors.

{x, y, z} . {a, b, c}

You can also do purely symbolic operations with lists.  Permutations gives all possible permutations of a list, i.e. all possible orderings of the elements in a list.

Permutations[{a, b, c}]

Flatten ``un-nests'' lists.

Flatten[%]

The list given as the permutations of {a,b,c} is actually a list of lists.  In this sense, the list is two dimensional.  Let us use the Permutations command again a set a variable equal to the output.  The list "list1" is essentially a matrix, as you will see in the next section.

list1 = Permutations[{a, b, c}]

The number of elements in each dimension of "list1" can be determined with the Dimensions command.  One of the dimensions of "list1" has 6 elements, and the second dimension has 3.

Dimensions[list1]

As noted in the Delimiters section, element locations within a list are specified with double brackets.  To get the second element of "list1," we write:

list1[[2]]

Notice that the second element is itself a list Ð one with three element.  To obtain the second element from this "sub-list," we can add another subscript.

list1[[2,2]]

Matrices

This generates a matrix whose (i , j)th element is 1/(i+j+1).  Mathematica represents the matrix as a list of lists.

m = Table[ 1 / (i + j + 1), {i, 3}, {j, 3} ]

The matrix can be displayed in matrix form rather than list form.

MatrixForm[ m ]

Here is the inverse of the matrix.

Inverse[ m ]

Multiplying the inverse by the original matrix gives an identity matrix.

% . m

This gives a new matrix, with a modified diagonal.

m - x IdentityMatrix[3]

The determinant of the new matrix gives the characteristic polynomial for the original matrix.

Det[ % ]

This finds (numerically) the roots of the characteristic polynomial using the Solve function.  These roots correspond to the eigenvalues of  "m".

N[ Solve[ % == 0, x ] ]

Using the function Eigenvalues, you can find the numerical eigenvalues of "m" directly.

Eigenvalues[ N[ m ] ]

Procedural Programming

Here is a simple Mathematica program that generates and expands products.

exprod[n_] := 
        Expand[ Product[ x + i, {i, 1, n} ] ]

The result of this function (or program) is the expansion of (x+1)(x+2)(x+3)...(x+n), where n is specified by you, the user.  The following statement runs the program with n=4.

exprod[4]

Defining Mathematical Relations

Here is an example of mathematical programming in Mathematica.   This definition gives the mathematical rule log(x y) = log(x) + log(y).

log[x_ y_] := log[x] + log[y]

Mathematica uses your definition to expand out this logarithm.

log[a b c^2 d]

You can add the rule log(xn) = n log(x).

log[x_ ^ n_] := n log[x]

Now logarithms of powers are also expanded out.

log[a b c^2 d]

This shows all the definitions you have given for log.

?log

Changes

This NoteBook was originally entitled "TourOfMathematica.ma" and was provided with the Mathematica distribution.  The modifications made are listed below.  This should not be of interest to the general reader.

Changes on 2/12/92: reformated the notebook to agree with 2.0 formattting; changes "pi" in the text to p; indicated exponentiation with superscripts rather than "^".

The font sizes were changed to agree with the default font sizes used in NeXT release 1.0 of Mathematica.  All the results were deleted so that the user must initiate the calculation.  All variables were enclosed in quotes in the text.

The original introductory paragraph was deleted and the "Introduction to the Introduction" section was added.

"Numerical Calculations" section:  Added statement about enter key.  Changed Rieman Zeta function to square root.  Added statement about Mathmatica commands starting with capital letters.  Added statement about comment delimiters.

"Cell Structure," "On Line Help," "Delimiters," and "Editing" sections were added.

"Graphics" section:  The ?Function construct was further explained.

"Three-Dimensional Graphics" section:  Deleted sentence about postscript output.

"Animations" section was added.

"Algebraic Formulae" section:  Deleted sentence about time to perform calculation.

"Solving Equations" section was deleted.

"Lists" section:  Statements about Fit function were deleted.  Statements about retrieving an element from a list were added.  Sentence about the ListPlot being nearly linear was deleted.

"Matrices" section:  Statements about the symbolic determination of the eigenvalues were deleted.  Statements added to demonstrate MatrixForm.  The phrase "leading diagonal" was changed to just "diagonal."

"Procedural Programming in Mathematica" section:  Continued fraction example was deleted.  Some explanatory text was added.

"Defining Mathematical Relations in Mathematica" section:  Typos were corrected (super- and sub-scripts were not properly rendered).  Statements about Laplace transform package were deleted.

"Interfacing with Mathematica" section:  TeXForm statements were deleted.  OpenRead and ReadList statements were deleted.

"Making Definitions in Mathematica" section:  Statements about defining w[2] were deleted.  Statements added about multi-variable functions.  Section was moved to follow "Three-Dimensional Plots."

"More Graphics" section was deleted.

"Mathematica Front Ends" section was deleted.

"Notebooks" section was deleted.