Matrices with Mathematica - · PDF fileMatrices with Mathematica Demo

11
Matrices with Mathematica Demo << Utilities`CleanSlate` CleanSlate@D; HCleanSlateL Contexts purged: 8Global`< HCleanSlateL Approximate kernel memory recovered: 0 Kb Off@General::spell1D Set size and aspect ratio of figures: Print@limage = 8.0 81, 1 GoldenRatio<D; limage *= 72 ; 88., 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 = 81, 0, 1< 81, 0, 1< v = 80, 1, 0< 80, 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- Printed by Wolfram Mathematica Student Edition

Transcript of Matrices with Mathematica - · PDF fileMatrices with Mathematica Demo

Page 1: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

Matrices with Mathematica

Demo

<< Utilities`CleanSlate`

CleanSlate@D;

HCleanSlateL Contexts purged: 8Global`<

HCleanSlateL Approximate kernel memory recovered: 0 Kb

Off@General::spell1D

Set size and aspect ratio of figures:

Print@limage = 8.0 81, 1 � GoldenRatio<D; limage *= 72 ;

88., 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 = 81, 0, 1<

81, 0, 1<

v = 80, 1, 0<

80, 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

Page 2: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

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 = 880, 1, 1<, 81, 0, 1<, 81, 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.

MatrixForm@AD0 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.

Det@AD

2

Thus, A is nonsingular and invertible. The inverse is evaluated by the com-

mand (note that % represents the most recent output).

Ainv = Inverse@AD

-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

Page 3: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

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.

MatrixPower@A, 5D10 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 = LinearSolve@A, vD

:1

2

, -1

2

,

1

2

>

We can also row reduce A to, for example, check its rank.

RowReduce@AD1 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

Page 4: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

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.

Clear@A, QD

The given matrix A is:

A = 882, -1, 2, 0<, 80, 3, -1, 0<, 80, 1, 1, 0<, 80, 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 = JordanDecomposition@AD;

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 = Part@rslt, 1D1 0 0 0

0 1 2 0

0 1 1 0

02

3

5

91

The Jordan canonical matrix is the second part.

J = Part@rslt, 2D2 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

Page 5: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

Q.J.Inverse@QD2 -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 = 880, 1, 1<, 81, 0, 1<, 81, 1, 0<<0 1 1

1 0 1

1 1 0

To solve this system, we first determine the eigenvalues.

8lam1, lam2, lam3< = Eigenvalues@AD

82, -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 = Transpose@Eigenvectors@ADD1 -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

Page 6: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

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 = [email protected]

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 = 8c1 Exp@lam1 tD, c2 Exp@lam2 tD, c3 Exp@lam3 tD<

9c1 ã2 t

, c2 ã-t

, c3 ã-t=

To transform back to the original variable x, we premultiply y by Q.

x = Q.y

9c1 ã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

DSolve@8x1'@tD � x2@tD + x3@tD, x2'@tD � x1@tD + x3@tD,

x3'@tD � x1@tD + x2@tD<, 8x1@tD, x2@tD, x3@tD<, tD

::x1HtL ®1

3

c1 ã-t Iã

3 t+ 2M +

1

3

c2 ã-t Iã

3 t- 1M +

1

3

c3 ã-t Iã

3 t- 1M,

x2HtL ®1

3

c1 ã-t Iã

3 t- 1M +

1

3

c2 ã-t Iã

3 t+ 2M +

1

3

c3 ã-t Iã

3 t- 1M,

x3HtL ®1

3

c1 ã-t Iã

3 t- 1M +

1

3

c2 ã-t Iã

3 t- 1M +

1

3

c3 ã-t Iã

3 t+ 2M>>

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

Page 7: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

Simplify@%D

::x1HtL ®1

3

ã-t Ic1 Iã

3 t+ 2M + Hc2 + c3L Iã

3 t- 1MM,

x2HtL ®1

3

ã-t Ic1 Iã

3 t- 1M + c2 Iã

3 t+ 2M + c3 Iã

3 t- 1MM,

x3HtL ®1

3

ã-t Ic1 Iã

3 t- 1M + c2 Iã

3 t- 1M + c3 Iã

3 t+ 2MM>>

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 = DSolve@8x1'@tD � x2@tD + x3@tD,

x2'@tD � x1@tD + x3@tD, x3'@tD � x1@tD + x2@tD, x1@0D � -1,

x2@0D � 0, x3@0D � 2<, 8x1@tD, x2@tD, x3@tD<, tD

::x1HtL ®1

3

ã-t Iã

3 t- 4M, x2HtL ®

1

3

ã-t Iã

3 t- 1M, x3HtL ®

1

3

ã-t Iã

3 t+ 5M>>

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

Page 8: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

Plot@Evaluate@8x1@tD, x2@tD, x3@tD< �.solnD,

8t, 0, 1<, AxesLabel ® 8"t", " "<,

PlotStyle ® 8Dashing@8<D, [email protected]`, 0.01`<D,

[email protected]`, 0.01`, 0.005`, 0.01`<D<,

AspectRatio ® GoldenRatio^H-1L, ImageSize ® limageD

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; H* 'C' is protected,

so we can not use it as a variable name *L

Now we are ready to solve the system of equations.

8 Matrices Example.nb

Printed by Wolfram Mathematica Student Edition

Page 9: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

soln = DSolve@8L I1'@tD � -R1 I1@tD + R1 I2@tD,

-Cap R1 I1'@tD + Cap HR1 + R2L I2'@tD � -I2@tD,

I1@0D � 10, I2@0D � -5<, 8I1@tD, I2@tD<, tD

::I1HtL ®

5 ã-3 t�652 114 cos

1

65

38

3t - 177 sin

1

65

38

3t

114

,

I2HtL ® -5

19

ã-3 t�65

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@%D

::I1HtL ®

5 ã-3 t�652 114 cos

1

65

38

3t - 177 sin

1

65

38

3t

114

,

I2HtL ® -5

19

ã-3 t�65

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

Page 10: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

Plot@Evaluate@8I1@tD, I2@tD< �.solnD, 8t, 0, 150<,

AxesLabel ® 8"t", " "<, PlotRange ® 8-30, 10<,

PlotStyle ® 8Dashing@8<D, [email protected]`, 0.01`<D<,

AspectRatio ® GoldenRatio^H-1L, ImageSize ® limageD

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

Clear@A, V, S, UD

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 = 881, 0, 1<, 81, 1, 0<<1 0 1

1 1 0

The SingularValueDecomposition[] procedure in Mathematica is invoked as

follows.

8V, S, U< = SingularValueDecomposition@AD;

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

Page 11: Matrices with Mathematica -   · PDF fileMatrices with Mathematica Demo

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

30 -

1

3

1

6

1

2

1

3

1

6

-1

2

1

3

The original matrix A is then obtained from:

V.S.Conjugate@Transpose@UDD1 0 1

1 1 0

Matrices Example.nb 11

Printed by Wolfram Mathematica Student Edition