You are on page 1of 11

Matrices with Mathematica

Demo
Utilities`CleanSlate`
CleanSlate;
(CleanSlate) Contexts purged: Global`
(CleanSlate) Approximate kernel memory recovered: 0 Kb
OffGeneral::spell1
Set size and aspect ratio of figures:
Printlimage 8.0 1, 1 GoldenRatio; limage 72 ;
8., 4.94427
Vectors and Matrices
Basic Operations
In Mathematica vectors and matrices are represented as lists of numbers. For
example, consider the two vectors
u 1, 0, 1
1, 0, 1
v 0, 1, 0
0, 1, 0
When evaluating inner products of vectors in Mathematica, it is not necessary
to take the transpose of the first vector. Thus, taking the inner product of u and
v is performed as follows.
u.v
0
Because the inner product is zero, u and v are orthogonal. Now let us consider
the matrix from the first example in the section on systems of first-order differen-
tial equations. Essentially, a matrix is entered as a list of lists, or a list of
vectors, with each row being entered as a vector.
Printed by Wolfram Mathematica Student Edition
Because the inner product is zero, u and v are orthogonal. Now let us consider
the matrix from the first example in the section on systems of first-order differen-
tial equations. Essentially, a matrix is entered as a list of lists, or a list of
vectors, with each row being entered as a vector.
A 0, 1, 1, 1, 0, 1, 1, 1, 0;
The semicolon at the end of the input line tells Mathematica to suppress its
standard output. Instead, it is more convenient to output A in the usual matrix
form as follows.
MatrixFormA
0 1 1
1 0 1
1 1 0
We note that the coefficient matrix is real and symmetric; therefore, it has
linearly independent eigenvectors. However, before solving the example
problem, we will illustrate some other matrix operations. The determinant of a
square matrix is determined as follows.
DetA
2
Thus, A is nonsingular and invertible. The inverse is evaluated by the com-
mand (note that % represents the most recent output).
Ainv InverseA

1
2
1
2
1
2
1
2

1
2
1
2
1
2
1
2

1
2
Matrix addition is performed just as you would think.
A A
0 2 2
2 0 2
2 2 0
Multiplication of a scalar times a matrix is also intuitive.
3 A
0 3 3
3 0 3
3 3 0
Unlike most programming languages (C, Fortran, etc.), one can eliminate the '*'
for multiplication, so that Mathematica equations more closely resemble the
way that you would write them on paper. For example,
2 Matrices Example.nb
Printed by Wolfram Mathematica Student Edition
Unlike most programming languages (C, Fortran, etc.), one can eliminate the '*'
for multiplication, so that Mathematica equations more closely resemble the
way that you would write them on paper. For example,
3 A
0 3 3
3 0 3
3 3 0
Matrix multiplication is performed using the dot, the same command that is
used to take the inner product of vectors. For example, we can confirm that
muliplying A by its inverse gives the identity matrix
Ainv.A
1 0 0
0 1 0
0 0 1
Powers of matrices can be evaluated easily. For example, we raise A to the
5th power using.
MatrixPowerA, 5
10 11 11
11 10 11
11 11 10
Because A is invertible, we can solve a system Az = v, where v is given above,
as follows.
z Ainv.v
{
1
2
,
1
2
,
1
2

Alternatively, Mathematica has a LinearSolve[] command that will do this for


us.
z LinearSolveA, v
{
1
2
,
1
2
,
1
2

We can also row reduce A to, for example, check its rank.
RowReduceA
1 0 0
0 1 0
0 0 1
Note that A has been row reduced to reduced row echelon form and has rank
three.
Matrices Example.nb 3
Printed by Wolfram Mathematica Student Edition
Note that A has been row reduced to reduced row echelon form and has rank
three.
Jordan Canonical Form
Consider the example from section 2.3 in the lecture notes in which the Jordan
canonical form of a non-symmetric matrix with repeated eigenvalues is sought.
First we 'clear' the previous values of variables that we want to reuse.
ClearA, Q
The given matrix A is:
A 2, 1, 2, 0, 0, 3, 1, 0, 0, 1, 1, 0, 0, 1, 3, 5
2 1 2 0
0 3 1 0
0 1 1 0
0 1 3 5
We use the JordanDecomposition[] procedure to obtain both the modal
(similarity) matrix and the Jordan canonical form.
rslt JordanDecompositionA;
The procedure returns a list of two matrices; the first matrix is the modal matrix
Q, and the second matrix is the Jordan canonical form. We can extract each
matrix by using the Part[] command. For example, Q is the first matrix in the
list, so we use:
Q Partrslt, 1
1 0 0 0
0 1 2 0
0 1 1 0
0
2
3
5
9
1
The Jordan canonical matrix is the second part.
J Partrslt, 2
2 1 0 0
0 2 1 0
0 0 2 0
0 0 0 5
To obtain the original matrix A, we take
4 Matrices Example.nb
Printed by Wolfram Mathematica Student Edition
Q.J.InverseQ
2 1 2 0
0 3 1 0
0 1 1 0
0 1 3 5
Systems of Ordinary Differential Equations
There are two approaches that one can use to solve systems of ordinary
differential equations using Mathematica. First, we could use Mathematica to
carry out the individual steps in the diagonalization procedure to emulate the
method we would use to solve the system by hand. Alternatively, we can use
an internal Mathematica procedure, DSolve[], to obtain the solution. We illus-
trate each approach in turn.
Using Mathematica to Perform Steps in Diagonalization
Now we turn our attention to solving the system of first-order differential equa-
tions given in the example in section 2.4. The coefficient matrix is:
A 0, 1, 1, 1, 0, 1, 1, 1, 0
0 1 1
1 0 1
1 1 0
To solve this system, we first determine the eigenvalues.
lam1, lam2, lam3 EigenvaluesA
2, 1, 1
Although one of the eigenvalues has multiplicity two, the eigenvectors are
linearly independent because the coefficient matrix is symmetric. To form the
modal matrix, we determine the eigenvectors.
Q TransposeEigenvectorsA
1 1 1
1 0 1
1 1 0
Taking the transpose is necessary because the Eigenvector[] command in
Mathematica outputs a matrix with the eigenvectors as rows rather than
columns. We can check to be sure that A is diagonalized by the modal matrix
Matrices Example.nb 5
Printed by Wolfram Mathematica Student Edition
Taking the transpose is necessary because the Eigenvector[] command in
Mathematica outputs a matrix with the eigenvectors as rows rather than
columns. We can check to be sure that A is diagonalized by the modal matrix
Adiag InverseQ.A.Q
2 0 0
0 1 0
0 0 1
This is a diagonal matrix with the eigenvalues along the diagonal as expected.
Now we can form the solution of the differential equations in terms of the
transformed variable y.
y c1 Explam1 t, c2 Explam2 t, c3 Explam3 t
|c1
2 t
, c2
t
, c3
t
|
To transform back to the original variable x, we premultiply y by Q.
x Q.y
|c1
2 t
c2
t
c3
t
, c1
2 t
c3
t
, c1
2 t
c2
t
|
This is the solution of the system of first-order differential equations.
Using Mathematica's Internal DSolve[] Procedure
DSolve[] is a general procedure that can solve systems of linear and nonlinear
ordinary differential equations and some systems of partial differential equa-
tions. The required arguments for the DSolve[] procedure are DSolve[{eqn1,
eqn2, eqn3,...}, {list of dependent variables}, {list of independent vari-
able}]. For the previous example, therefore, we use
DSolvex1't x2t x3t, x2't x1t x3t,
x3't x1t x2t, x1t, x2t, x3t, t
{{x1(t)
1
3
c
1

t
|
3 t
2]
1
3
c
2

t
|
3 t
1]
1
3
c
3

t
|
3 t
1],
x2(t)
1
3
c
1

t
|
3 t
1]
1
3
c
2

t
|
3 t
2]
1
3
c
3

t
|
3 t
1],
x3(t)
1
3
c
1

t
|
3 t
1]
1
3
c
2

t
|
3 t
1]
1
3
c
3

t
|
3 t
2]
Note that the double equal sign, i.e. ==, is used in Mathematica to represent a
symbolic equation, in contrast to assignment statements that use a single equal
sign and 'assign' the value/expression on the right hand side to the variable on
the left hand side. The constants of integration are C[1], C[2] and C[3]. The
Simplify[] procedure attempts to reduce the expression(s) to a more compact
form.
6 Matrices Example.nb
Printed by Wolfram Mathematica Student Edition
Simplify
{{x1(t)
1
3

t
|c
1
|
3 t
2] (c
2
c
3
) |
3 t
1]],
x2(t)
1
3

t
|c
1
|
3 t
1] c
2
|
3 t
2] c
3
|
3 t
1]],
x3(t)
1
3

t
|c
1
|
3 t
1] c
2
|
3 t
1] c
3
|
3 t
2]]
Minor algebraic manipulation shows that this is the same result as obtained
previously (the constants of integration are different).
Although not given in the original problem, we could impose initial and/or
boundary conditions to determine the constants of integration. The initial/bound-
ary conditions are input as additional equations; for example, specifying initial
conditions at t = 0 would be done as follows.
soln DSolvex1't x2t x3t,
x2't x1t x3t, x3't x1t x2t, x10 1,
x20 0, x30 2, x1t, x2t, x3t, t
{{x1(t)
1
3

t
|
3 t
4], x2(t)
1
3

t
|
3 t
1], x3(t)
1
3

t
|
3 t
5]
In order to see how the solution behaves, let us plot each of the dependent
variables with time: x1(t) - solid line, x2(t) - dashed line, x3(t) - dot-dashed line.
Matrices Example.nb 7
Printed by Wolfram Mathematica Student Edition
PlotEvaluatex1t, x2t, x3t .soln,
t, 0, 1, AxesLabel "t", " ",
PlotStyle Dashing, Dashing0.01`, 0.01`,
Dashing0.01`, 0.01`, 0.005`, 0.01`,
AspectRatio GoldenRatio^1, ImageSize limage
0.2 0.4 0.6 0.8
1
1
2
3
Parallel Circuit Analysis Example
For one last example of solving systems of ordinary differential equations,
consider the parallel circuit example discussed in class. Here, we will solve the
system of equations using the DSolve[] procedure. First we shall input some
parameters for the problem:
R1 10;
R2 3;
L 30;
Cap 5; 'C' is protected,
so we can not use it as a variable name
Now we are ready to solve the system of equations.
8 Matrices Example.nb
Printed by Wolfram Mathematica Student Edition
soln DSolveL I1't R1 I1t R1 I2t,
Cap R1 I1't Cap R1 R2 I2't I2t,
I10 10, I20 5, I1t, I2t, t
{{I1(t)
5
3 tf65
2 114 cos
1
65
38
3
t 177 sin
1
65
38
3
t
114
,
I2(t)
5
19

3 tf65
26 114 sin
1
65
38
3
t 19 cos
1
65
38
3
t
This result looks a bit messy, so let's try the Simply[] command again. As you
can see, the result is not much better.
Simplify
{{I1(t)
5
3 tf65
2 114 cos
1
65
38
3
t 177 sin
1
65
38
3
t
114
,
I2(t)
5
19

3 tf65
26 114 sin
1
65
38
3
t 19 cos
1
65
38
3
t
Let us plot the solution for the currents as functions of time: I1(t) - solid line,
I2(t) - dashed line.
Matrices Example.nb 9
Printed by Wolfram Mathematica Student Edition
PlotEvaluateI1t, I2t .soln, t, 0, 150,
AxesLabel "t", " ", PlotRange 30, 10,
PlotStyle Dashing, Dashing0.01`, 0.01`,
AspectRatio GoldenRatio^1, ImageSize limage
20 40 60 80 100 120
30
20
10
10
Therefore, the solutions for the currents undergo a damped oscillation, eventu-
ally becoming zero because there is no voltage source.
Singular Value Decomposition
ClearA, V, S, U
As an illustration of singular value decomposition, consider the example treated
in section 2.5.2 in the lecture notes. We begin with the 2x3 matrix A:
A 1, 0, 1, 1, 1, 0
1 0 1
1 1 0
The SingularValueDecomposition[] procedure in Mathematica is invoked as
follows.
V, S, U SingularValueDecompositionA;
The procedure returns three lists as follows. The second list is the matrix S,
which contains the singular values themselves.
10 Matrices Example.nb
Printed by Wolfram Mathematica Student Edition
The procedure returns three lists as follows. The second list is the matrix S,
which contains the singular values themselves.
S
3 0 0
0 1 0
The first list is the matrix V, which in this case is 2x2:
V
1
2

1
2
1
2
1
2
The third list is the transpose of the 3x3 matrix U given in the notes:
U
2
3
0
1
3
1
6
1
2
1
3
1
6

1
2
1
3
The original matrix A is then obtained from:
V.S.ConjugateTransposeU
1 0 1
1 1 0
Matrices Example.nb 11
Printed by Wolfram Mathematica Student Edition

You might also like