Matrix Commands in Mathematica
By Example

For Engineering Mathematics III:Matrix Algebra 22M:033
Spring 2003
WS

Read Me First

This Mathematica file contains examples of how one can use Mathematica to perform many of the matrix operations which are discussed in Engineering Mathematics III:Matrix Algebra 22M:033. In each of the cell groups below you find example matrices, and examples of the Mathematica commands used in getting information about those matrices. Open the various cell groups, then the subgroups, then initialize the commands to see them work.

This file is 'bare bones' in that almost no text is given detailing the commands. But all of the commands shown here are discussed in more detail (and with additional examples) in the Help browser of Mathematica. To access the Help browser, click on the  word "Help"on the  menu bar, then choose Help Browser. Then in the open window at the top of the help browser type in the word matrix . This should bring up a list of choices including Matrix Operations. If you want more detail on a specific command given below, for example, Det (the Mathematica command for determinant) type that word det into the open window. Then scroll down to get additional information about that topic.

There are many more matrix operations Mathematica can perform (e.g. finding various 'canonical forms' for the matrix) which we have not included in this file. We heartily encourage you to examine those additional commands. You should try using the matrix operations given below on other matrices which you want to work on. Please report any errors in programming or difficulties with Mathematica to your course instructor.

One way to enter a matrix with Mathematica

Naming entries individually

Clear[a,b,c,d,e,f,g,h,i];
MatrixForm[{{a, b, c},{d,e,f},{g,h,i}}]
d=DiagonalMatrix[{3,4,57,121}]
MatrixForm[d]

Clear[a,b,c,d,e,f,g,h,i];
a=1;
b=2;
c=3;
d=4;
e=5;
f=6;
g=7;
h=8;
i=9;
m=MatrixForm[{{a, b, c},{d,e,f},{g,h,i}}]
mm=MatrixForm[{{2a, b, c},{d,3e,f},{g,h,4i}}]

Naming Entries with Functions

Clear[a,b,c,d,e,f,g,h,i];
f[x_,y_]:=2*x+3*y;
example1=Table[f[x,y],{x,1,3},{y,4,8}]
b=MatrixForm[example1]

IdentityMatrix[3]

Matrix arithmetic with Mathematica

2*m+5*mm, and mm.m where m and mm are three by three matrices

Clear[a,b,c,d,e,f,g,h,i];
a=1;
b=2;
c=3;
d=4;
e=5;
f=6;
g=7;
h=8;
i=9;
m={{a, b, c},{d,e,f},{g,h,i}}
mm={{2a, b, c},{d,3e,f},{g,h,4i}}
MatrixForm[3*m+5*mm]
MatrixForm[mm.m]

Additional examples

Clear[a,b,c,d,e,f,g,h,i];
f[x_,y_]:=2*x+3*y;
example1=Table[f[x,y],{x,1,3},{y,4,8}]
example11=Table[f[x,y],{x,-2,0},{y,10,14}]
MatrixForm[example1]
MatrixForm[example11]
MatrixForm[2*example1+3*example11]

Reduced Row Echelon Form

Clear[a,b,c,d,e,f,g,h,i];
f[x_,y_]:=2*x+3*y;
example1=Table[f[x,y],{x,1,3},{y,4,8}]
b=MatrixForm[example1]
RowReduce[example1]//MatrixForm

Determinant, Trace, MatrixPowers, Inverse

Clear[a,b,c,d,e,f,g,h,i];
f[x_,y_]:=-2x+5y;
example2=Table[f[x,y],{x,1,3},{y,4,6}]
b=MatrixForm[example2]
Transpose[example2]//MatrixForm
Det[example2]
Tr[example2]
MatrixPower[example2,2]//MatrixForm

Clear[a,b,c,d,e,f,g,h,i];
a=1;
b=2;
c=3;
d=4;
e=5;
f=6;
g=7;
h=8;
i=9;
m={{a, b, c},{d,e,f},{g,h,i}}
mm={{2a, b, c},{d,3e,f},{g,h,4i}}
MatrixForm[Inverse[m]]
MatrixForm[Inverse[mm]]

Basis for the Null Space (kernel)

Clear[a,b,c,d,e,f,g,h,i];
f[x_,y_]:=-2x+5y;
example2=Table[f[x,y],{x,1,3},{y,4,6}]
MatrixForm[example2]
NullSpace[example2]

Clear[a,b,c,d,e,f,g,h,i];
a=1;
b=2;
c=3;
d=4;
e=5;
f=6;
g=7;
h=8;
i=9;
m = {{a, b, c}, {d, e, f}, {g, h, i}}
n = {{a, b, c}, {a, b, c},{a, b, c}}
mm = {{2*a, b, c}, {d, 3*e, f}, {g, h, 4*i}}
NullSpace[m]
NullSpace[n]
NullSpace[mm]

Characteristic Polynomial, Eigenvalues, Eigenvectors

Clear[a,b,c,d,e,f,g,h,i];
f[x_,y_]:=-2x+5y;
example2=Table[f[x,y],{x,1,3},{y,4,6}]
b=MatrixForm[example2]
CharacteristicPolynomial[example2,x]
Eigenvalues[example2]
Eigenvectors[example2]//MatrixForm
Eigensystem[example2]//MatrixForm

Clear[a,b,c,d,e,f,g,h,i,θ];
θ=π/2;
rotationθ={{Cos[θ],-Sin[θ]},{Sin[θ],Cos[θ]}};
MatrixForm[rotationθ]
CharacteristicPolynomial[rotationθ,x]
Eigenvalues[rotationθ]
Eigenvectors[rotationθ]//MatrixForm
Eigensystem[rotationθ]//MatrixForm

Clear[a,b,c,d,e,f,g,h,i,θ];
θ=π/3;
rotationθ={{Cos[θ],-Sin[θ]},{Sin[θ],Cos[θ]}};
MatrixForm[rotationθ]
CharacteristicPolynomial[rotationθ,x]
Eigenvalues[rotationθ]
Eigenvectors[rotationθ]//MatrixForm
Eigensystem[rotationθ]//MatrixForm

Gram-Schmidt Procedure, Normalize, Projection

Note: In order for these programs to run, the line phrase 
<<LinearAlgebra`Orthogonalization`
should appear at the top of the input cell. This phrase is present in the examples below.

<< LinearAlgebra`Orthogonalization`
Clear[a, b, c, d, e, f, g, h, i];
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
f = 6;
g = 7;
h = 8;
i = 9;
{{2  a, b, c}, {d, 3  e, f}, {g, h, 4  i}}
GramSchmidt[{{2  a, b, c}, {d, 3  e, f}, {g, h, 
          4  i}}]
GramSchmidt[{{2.  a, b, c}, {d, 3.  e, f}, {g, h, 4.  i}}]

<< LinearAlgebra`Orthogonalization`
Clear[a, b, c, d, e, f, g, h, i];
f[x_, y_] := -2 x + 5  y;
example2 = Table[f[x, y], {x, 1, 3}, {y, 4, 6}]
GramSchmidt[example2]

<< LinearAlgebra`Orthogonalization`
Normalize[{2, 3, -5}]
Normalize[{2. , 3, -5}]

<< LinearAlgebra`Orthogonalization`
Normalize[{2, 3, -5}]
Normalize[{2. , 3, -5}]

<< LinearAlgebra`Orthogonalization`
Projection[{2, 3, -5}, {-1, 8, 4}]
Projection[{-1, 8, 4}, {2, 3, -5}]