Math 21b: Linear Algebra, Spring 2003
Mathematica Exercise

If you are already familiar with the basic mechanics of Mathematica, please jump to section on DEFINING VECTORS AND MATRICES.

Cells

Mathematica notebooks are subdivided into cells, each of which is marked by a blue bracket at the right of the window.  These cells come in several different types.  For instance, the text you are currently reading is in a "text" cell, while the expression below is in an "input" cell.

76 - 23

You can tell the different types of cells apart by the font they use: text cells use a normal weight proportional width font (Times), while input cells use a boldface typewriter-style font (Courier).  Text cells usually contain instructions, descriptions, or explanations.  If you want Mathematica to evaluate a mathematical expression, you must put it in an input cell.  To create a new cell, position the cursor between two existing cells or after the last cell.  The cursor will then change from a vertical I-beam to a horizontal I-beam.  If you click there, Mathematica will draw a horizontal line across the window.  If you then start typing, Mathematica will create a new input cell for you.  Any new cell you create will automatically be an input cell.

Evaluating Expressions

All commands are entered into Mathematica as text.  To evalute an expression, click anywhere in the expression and press the <Enter> key on the keypad (not the one on the main keyboard) or hold down the <Shift> key and press <Enter>.  Mathematica will then think for a moment and write the answer below the expression.  Your input and Mathematica's output are labeled "In[#]:" and "Out[#]:" respectively, and Mathematica draws the input in boldface to distinguish it from the plain text output.  For example, to add the following two numbers, click the sum below and press <Enter>.

3 + 8

Throughout this Mathematica assignment, there are some expressions to evaluate, along with descriptive text.  Just go through it one cell at a time, reading the text and evaluating each of the expressions one by one.  In this notebooks, cells which require you to try out what you have learned will be boxed in blue.

Basic Operators

The basic operations are + (addition), - (subtraction), * (multiplication), / (division), and ^ (exponentiation).  Mathematica obeys the usual operator precedence, first performing exponentiation, then multiplication and division, and finally addition and subtraction.  If you want to group your operations differently, use parentheses ().  Try evaluating the following expressions:

12 - 3*5

(12 - 3)*5

6/2*3

6/(2*3)

-5^2

(-5)^2

If you put two expressions next to each other separated only by a space, Mathematica multiplies them, just like in normal mathematical notation.  This can be confusing if you just put two numbers next to each other, but it comes in handy with more complicated expressions.  Here are some examples:

5 2

3 (8 - 4)

Functions and Constants

Mathematica knows many functions and constants.  Some of the functions we will be using are Sin[x], Cos[x], Exp[x] (the exponential (e^x)), Log[x] (the natural logarithm ln x), Sqrt[x] (square root (Sqrt[x])), and Abs[x] (the absolute value |x|).  Note that the first letter of all built-in functions are capitalized, and you use square brackets [] to apply them, as opposed to regular parentheses ().  Some useful constants that Mathematica knows are Pi (or π) and E (or e ≈ 2.71828, the base for natural logarithms).  Again, be sure to capitalize the first letter.  Alternatively, you can use the palette of templates and symbols on the right of your screen to type in some of these operators and constants.  (If you do not see the palette, select from the menu File - Palettes - BasicInput.)  Here are some examples.

Sin[0.123]

Just like in standard mathematical  notation, if you write two things next to each other with only a space between them, Mathematica multiplies them.

Cos[2 Pi]

When you evaluated this next expression, note that Mathematica is clever enough to give you the exact answer, rather than a decimal approximation.

Sin[Pi/4]

Defining Variables and Functions

Mathematica lets you do both symbolic and numerical calculations using variables and functions.  You can use any string of letters for a variable name.  In addition to the usual variable names x, y, t, you can also use more descriptive names like theta or radius.  You can assign variables a value and then use that value in further computations.  Here are some examples:

a = 5

a^2  -  3  a  +  4

b = -3

a b

Notice that if you write two variable names next to each other separated only by a space, Mathematica multiplies them, just like in normal mathematical notation.  However, you must take care to put in the space between your variable names.  Otherwise, Mathematica will think that you are talking about a single variable with a long name!  For instance, in the following cell, instead of multiplying a by b, Mathematica assumes you are using a new variable ab:

ab

Defining Vectors and Matrices

Vectors and matrices in Mathematicaare simply represented by lists and by lists of lists,respectively.
For instance,
	{a,b,c} represents vector (a, b, c)
	 and
	{{a,b}, {c,d}} represents matrix (({{a, b}, {c, d}}))

You can define vectors the same way you assign values to variables.  For instance,

v = {x, y}

Matrices are defined in the same way, but only as a list of matrices.  For instance,

m = {{a, b}, {c, d}}

Instead of lists, you can view matrices in a standard 2-d table by using:

MatrixForm[m]

Alternatively, matrices can also be entered via the Create Table/Matrix/Palette command in the Input menu. In the dialog box, specify the desired number of rows and columns. Press ⇥ to fill in successive entries. Try creating a 2x2 matrix, p, now.

p=

Other ways of inputing matrices are available - see HELP topics (e.g. DiagonalMatrix, symbolic definitions, etc.).

Some Basic Vectors Operations

This is a two-component vector.

v = {x, y}

Since s and t have not been defined as lists, the objects s and t are treated as scalars.

s v + t

Vectors are added component by component.

v + {a, b} + {s, t}

This multiplies each element of the vector by the scalar k.

k {a, b, c}

The “dot” operator gives the scalar product of two vectors.

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

It is important to realize that you can use “dot” for both left- and right-multiplication of vectors by matrices. Mathematica makes no distinction between “row” and “column” vectors. Dot carries out whatever operation is possible! (In formal terms, a.b contracts the last index of the tensor a with the first index of b.)

The “cross” operator gives the cross product of two vectors.

Cross[{a, b, c}, {x, y, z}]

Of course, you can also pre-define functions and then use all of the above operations.  For instance, first define vectors m and n, and then find Cross product:

m={2,3,1}; n={3,2,5}

Cross[m,n]

Or, if you have the BasicInput palette open, you can use the "×" (small x) as cross product.

m \[Cross] n

Try some operations yourself:

Some Basic Matrices Operations

Almost all operations (including symbollic) can be done by Mathematica.  For instance, you can find multiply a matrix by a vector. First define

v = {x, y}

m = {{a, b}, {c, d}}

Then,

m . v

Or a matrix by a matrix.

m . p

Or a vector by a matrix.

v . m

Notice that the product of a matrix and a vector is the same as that of a vector and a matrix!  Keep this in mind when doing your own calculations.

In addition.  Mathematica can be used to quickly find some important values.
For Determinant...

Det[m]

For the Transpose...

Transpose[m]

For inverses...

Inverse[m]

To calculate Trace...

Tr[m]

Try some operations yourself:

Some Linear Systems Operations

Here is a 2×2 matrix.

m = {{1, 5}, {2, 1}}

m . {x, y} == {a, b}

You can use Solve directly to solve these equations.

Solve[ %, {x, y} ]

You can also get the vector of solutions by calling LinearSolve. The result is equivalent to the one you get from Solve.

LinearSolve[m, {a, b}]

RowReduce performs a version of Gaussian elimination and can also be used to solve the equations.

RowReduce[{{1, 5, a}, {2, 1, b}}]

To find the Kernel (nullspace) of a system..

m = {{1, 3, 4}, {2, 1, 3}}

NullSpace[m]

Try some operations yourself:

Some More Advanced Matrix Operations

This gives the eigenvectors to the matrix.

Eigenvectors[p]

This gives the eigenvalues to the matrix.

Eigenvalues[p]

Try some operations yourself: