Mathematica for Linear Algebra	      C.Golé, Fall 2001

Double Click on the right hand side arrows to open the different sections

General Facts About Mathematica

Mathematica is a powerful symbolic manipulator, which can also perform numerical routines. That means you can do all the algebra you ever learned in HS (and more).
When you work with Mathematica, you create cells (in ocre in this file) which are bound by a blue bracket. Cells may contain one or more commands. To enter the commands in a cell  press enter or shift-return. The shape of the different [, {, ( brackets is very important in Mathematica, as are commas and semi-colons. Commands are usually followed by square brackets. Perform all the following commands as you go through the tutorial.

Let's start with examples of elementary algebra:

Expand[(x + 3) (x^2 - 1)]

Factor[-3 - x + 3 (x^2) + (x^3)]

It can also graph functions and other data:

Plot[(x + 3) (x^2 - 1),  {x,  -5,  3}]

It can also do Calculus, Linear Algebra, etc... Here is a derivative (with respect to x)

D[(x + 3) (x^2 - 1),  x]

Entering a Matrix

To enter the matrix (({{1, 3, 5}, {4, 5, 3}, {(Sqrt[2]), -1, 2}})) do:

M = {{1, 3, 5},  {4,  5, 3},  {Sqrt[2],  -1,  2}}

If you want to see the matrix in its usual form do:

MatrixForm[M]

YouI could also have entered MatrixForm[%n], where %n is the output number you got when you entered the matrix M.

Row Reduction

To put the matrix A=(({{2, 5, -2, 4, -2}, {2, -1, 3, 4, 4}, {5, 3, 0, -0.5`, 5}})) in Reduced Row Echelon form, you can enter

A = {{2, 5, -2, 4,  -2},  {2, -1, 3, 4,  4},  {5, 3, 
          0, - .5,  5}}

f, and then

RowReduce[A]

or, directly:

RowReduce[{{2, 5, -2, 4,  -2},  {2, -1, 3, 4,  4},  {5, 
          3, 0, - .5,  5}}]

You could also combine 2 operations in one:

MatrixForm[RowReduce[A]]

This matrix comes form a system of the form: (({{2, 5, -2, 4}, {2, -1, 3, 4}, {5, 3, 0, -0.5`}}))(({{x1}, {x2}, {x3}, {x4}}))=(({{-2}, {4}, {5}})), which you can now solve
choosing the nonleading variable x4=t, as a free parameter: (x1, x2, x3, x4)=(1.68571+1.9 t,  -1.14286-3 t , -0.171429-3.6 t,  t) or, in vector form: (({{x1}, {x2}, {x3}, {x4}}))=(({{1.685714285714286`}, {-1.1428571428571428`}, {-0.17142857142857146`}, {0}}))+t(({{-1.9`}, {3}, {3.6`}, {1}}))

Cheating...

Does not pay!   Here is apparently, another way to solve the linear system  (({{2, 5, -2, 4}, {2, -1, 3, 4}, {5, 3, 0, -0.5`}}))(({{x1}, {x2}, {x3}, {x4}}))=(({{-2}, {4}, {5}})):

LinearSolve[{{2, 5, -2, 4},  {2, -1, 3, 4},  {5, 3, 
          0, - .5}},  {-2,  4,  5 }]

Which only gives you one point out of the line of solutions we got previously! (just set t=0 in the previous solution). This example illustrates that,using programs such as Mathematica, you need to question the answers and bring to bear your understanding of Linear Algebra.

Multiplication of Matrices

Enter the following matrices (as you see, you can enter several commands at once):

M = {{1, 3, 5},  {4,  5, 3},  {Sqrt[2],  -1,  
          2}}
 B = {{w,  c},  {Pi,  q},  {2, w}}

The matrix product MB is computed by:

M . B

MatrixForm[%]

For You To Play With

1. What happens when you enter B.M? Why? 
2. There is another way that Mathematica computes products of matrices. Try to enter M.M and then M*M. Do you see what the latter product is? 
3. Use matrix multiplication to verify that (({{x1}, {x2}, {x3}, {x4}}))=(({{1.685714285714286`}, {-1.1428571428571428`}, {-0.17142857142857146`}, {0}}))+t(({{-1.9`}, {3}, {3.6`}, {1}})) is indeed the solution of : (({{2, 5, -2, 4}, {2, -1, 3, 4}, {5, 3, 0, -0.5`}}))(({{x1}, {x2}, {x3}, {x4}}))=(({{-2}, {4}, {5}}))