Chapter 6. Systems of Ordinary Differential Equations
Tải bản đầy đủ - 0trang 412
As when using
TableForm, the result of
using MatrixForm is no
longer a list that can be
manipulated using
Mathematica commands. Use
MatrixForm to view a
matrix in traditional
row-and-column form. Do
not attempt to perform
matrix operations on a
MatrixForm object.
Chapter 6 Systems of Ordinary Differential Equations
form using the command MatrixForm[A]. You can quickly construct 2
2
matrices by clicking on the
button from the BasicTypesetting palette, which
is accessed by going to File under the Mathematica menu, followed by Palettes
and then BasicTypesetting.
Alternatively, you can construct matrices of any dimension by going to the Mathematica menu under Input and selecting Create Table/Matrix/Palette...
6.1 Review of Matrix Algebra and Calculus
The resulting pop-up window allows you to create tables, matrices, and palettes.
To create a matrix, select Matrix, enter the number of rows and columns of the
matrix, and select any other options. Pressing the OK button places the desired
matrix at the position of the cursor in the Mathematica notebook.
a11 a12 a13
EXAMPLE 6.1.1: Use Mathematica to deﬁne the matrices a22 a22 a23
a31 a32 a33
b b b b
and 11 12 13 14 .
b21 b22 b23 b24
SOLUTION: In this case, both Table ai,j , i, 1, 3 , j, 1, 3 and Array
[a,{3,3}] produce equivalent results when we deﬁne matrixa to be
the matrix
a11 a12 a13
a22 a22 a23 .
a31 a32 a33
The commands MatrixForm or TableForm are used to display the
results in traditional matrix form.
In[1047]:= Clear a, b, matrixa, matrixb
In[1048]:= matrixa
Out[1048]=
Table ai,j , i, 1, 3 , j, 1, 3
a 1 , 1 , a1 , 2 , a1 , 3 ,
a2 , 1 , a2 , 2 , a2 , 3 , a 3 , 1 , a3 , 2 , a3 , 3
In[1049]:= MatrixForm matrixa
413
414
Chapter 6 Systems of Ordinary Differential Equations
Out[1049]=
a1 , 1 a1 , 2 a1 , 3
a2 , 1 a2 , 2 a2 , 3
a3 , 1 a3 , 2 a3 , 3
In[1050]:= matrixa
Out[1050]=
Array a, 3, 3
a 1, 1 , a 1 , 2 , a 1, 3 ,
a 2 , 1 , a 2 , 2 , a 2, 3 ,
a 3 , 1 , a 3 , 2 , a 3, 3
In[1051]:= MatrixForm matrixa
a 1, 1 a 1, 2 a 1, 3
Out[1051]= a 2, 1 a 2, 2 a 2, 3
a 3, 1 a 3, 2 a 3, 3
We may also use Mathematica to deﬁne nonsquare matrices.
In[1052]:= matrixb
Out[1052]=
Array b, 2, 4
b 1, 1 , b 1 , 2 , b 1, 3 , b 1, 4 ,
b 2 , 1 , b 2 , 2 , b 2, 3 , b 2 , 4
In[1053]:= MatrixForm matrixb
b 1, 1 b 1, 2 b 1, 3
Out[1053]=
b 2, 1 b 2, 2 b 2, 3
b 1, 4
b 2, 4
Equivalent results would have been obtained by entering Table bi,j ,
i, 1, 2 , j, 1, 4 .
More generally the commands Table[f[i,j],{i,imax},{j,jmax}] and
Array[f,{imax,jmax}] yield nested lists corresponding to the imax jmax
matrix
f 1, 1
f 1, 2
f 1, jmax
f 2, 1
f 2, 2
f 2, jmax
.
f imax, 1 f imax, 2
f imax, jmax
Table[f[i,j],{i,imin,imax,istep},{j,jmin,jmax,jstep}]
the list of lists
returns
{{f[imin,jmin],f[imin,jmin+jstep],...,f[imin,jmax]},
{f[imin+istep,jmin],...,f[imin+istep,jmax]},
...,{f[imax,jmin],...,f[imax,jmax]}}
and the command
Table[f[i,j,k,...],{i,imin,imax,istep},{j,jmin,jmax,jstep},
{k,kmin,kmax,kstep},...]
6.1 Review of Matrix Algebra and Calculus
calculates a nested list; the list associated with i is outermost. If istep is omitted,
the stepsize is one.
In Mathematica, a vector is a list of numbers and, thus, is entered in the same
manner as lists. For example, to use Mathematica to deﬁne the row vector
vector v to be v1 v2 v3 enter vector v={v1,v2, v3}. Similarly, to deﬁne the
v1
column vector vector v to be v2 enter vector v={v1,v2,v3} or vector v=
v3
button on the Basic{{v1},{v2},{v3}}. For a 2 1 vector, you can use the
Typesetting palette. Generally, with Mathematica you do not need to distinguish
between row and column vectors: Mathematica performs computations with vectors and matrices correctly as long as the computations are well-deﬁned.
4
EXAMPLE 6.1.2: Deﬁne the vector w
5 , vectorv to be the vector
2
v1 v2 v3 v4 , and zerovec to be the vector 0 0 0 0 0 .
SOLUTION: To deﬁne w, we enter
In[1054]:= w
Out[1054]=
4, 5 , 2
4, 5, 2
or
In[1055]:= w
4 ,
5 , 2
MatrixForm w
Out[1055]=
4
5
2
To deﬁne vectorv, we use Array.
In[1056]:= vectorv
Out[1056]=
Array v, 4
v 1 ,v 2 ,v 3 ,v 4
Equivalent results would have been obtained by entering Table vi ,
i, 1, 4 . To deﬁne zerovec, we use Table.
415
With Mathematica, you do
not need to distinguish
between row and column
vectors. Provided that
computations are
well-deﬁned, Mathematica
carries them out correctly.
Mathematica warns of any
ambiguities when they
(rarely) occur.
416
Chapter 6 Systems of Ordinary Differential Equations
In[1057]:= zerovec
Out[1057]=
Table 0, 5
0, 0, 0, 0, 0
The same result is obtained by going to Input under the Mathematica
menu and selecting Create Table/Matrix/Palette...
In[1058]:=
0 0 0 0 0
Out[1058]=
0 , 0 , 0, 0 , 0
6.1.2 Extracting Elements of Matrices
For the 2 2 matrix m
a1,1 , a1,2 , a2,1 , a2,2 deﬁned earlier, m[[1]] yields the
ﬁrst element of matrix m which is the list a1,1 , a1,2 or the ﬁrst row of m; m[[2,1]]
yields the ﬁrst element of the second element of matrix m which is a2,1 . In general,
if m is an i j matrix, m[[i,j]] or Part[m,i,j] returns the unique element in
the ith row and jth column of m. More speciﬁcally, m[[i,j]] yields the jth part
of the ith part of m; list[[i]] or Part[list,i] yields the ith part of list;
list[[i,j]] or Part[list,i,j] yields the jth part of the ith part of list, and
so on.
10 6 9
6 5 7 . (a) Extract
10 9 12
the third row of mb. (b) Extract the element in the ﬁrst row and third
column of mb. (c) Display mb in traditional matrix form.
EXAMPLE 6.1.3: Deﬁne mb to be the matrix
SOLUTION: We begin by deﬁning the command mb. mb[[i,j]]
yields the (unique) number in the ith row and jth column of mb.
Observe how various components of mb (rows and elements) can be
extracted and how mb is placed in MatrixForm.
In[1059]:= mb
10, 6, 9 , 6, 5, 7 ,
10, 9, 12
In[1060]:= MatrixForm mb
10 6 9
6 5 7
Out[1060]=
10 9 12
6.1 Review of Matrix Algebra and Calculus
In[1061]:= mb
Out[1061]=
3
10, 9, 12
In[1062]:= mb
Out[1062]=
417
1, 3
9
If m is a matrix, the ith row of m is extracted with m[[i]]. The command
Transpose[m] yields the transpose of the matrix m, the matrix obtained by
interchanging the rows and columns of m. We extract columns of m by computing
Transpose[m] and then using Part to extract rows from the transpose. Namely,
if m is a matrix, Transpose[m][[i]] extracts the ith row from the transpose of
m which is the same as the ith column of m.
EXAMPLE 6.1.4: Extract the second and third columns from A if A
0 2 2
1 1 3 .
2 4 1
SOLUTION: We ﬁrst deﬁne matrixa and then use Transpose to
compute the transpose of matrixa, naming the result ta, and then
displaying ta in MatrixForm.
In[1063]:= matrixa
2, 4 , 1
In[1064]:= ta
0, 2, 2 ,
1, 1, 3 ,
Transpose matrixa
MatrixForm ta
0 1 2
2 1 4
Out[1064]=
2 3 1
Next, we extract the second column of matrixa using Transpose
together with Part ([[...]]). Because we have already deﬁned ta
to be the transpose of matrixa, entering ta[[2]] would produce the
same result.
In[1065]:= Transpose matrixa
Out[1065]=
2, 1, 4
2
418
Chapter 6 Systems of Ordinary Differential Equations
To extract the third column, we take advantage of the fact that we have
already deﬁned ta to be the transpose of matrixa. Entering
Transpose [matrixa][[3]] produces the same result.
In[1066]:= ta
Out[1066]=
3
2, 3, 1
Other commands that can be used to manipulate matrices are included in the
MatrixManipulation package, which is contained in the Linear Algebra folder
(or directory).
6.1 Review of Matrix Algebra and Calculus
419
After this package has been loaded,
In[1067]:= << LinearAlgebra‘MatrixManipulation‘
we can use commands like TakeColumns and TakeRows to extract columns and
rows from a given matrix. For example, entering
In[1068]:= TakeColumns matrixa, 2
2
1
Out[1068]=
4
//MatrixForm
extracts the second column of the matrix A deﬁned in the previous example and
displays the result in MatrixForm while entering
In[1069]:= TakeColumns matrixa, 2, 3
MatrixForm
Out[1069]=
2
1
4
//
2
3
1
extracts the second and third columns of A and displays the result in Matrix
Form.
6.1.3 Basic Computations with Matrices
Mathematica performs all of the usual operations on matrices. Matrix addition
(A B), scalar multiplication (kA), matrix multiplication (when deﬁned) (AB),
and combinations of these operations are all possible. The transpose of A, At ,
is obtained by interchanging the rows and columns of A and is computed with
the command Transpose[A]. If A is a square matrix, the determinant of A is
obtained with Det[A].
If A and B are n n matrices satisfying AB
BA
I, where I is the n n
matrix with 1s on the diagonal and 0s elsewhere (the n n identity matrix), B is
called the inverse of A and is denoted by A 1 . If the inverse of a matrix A exists,
a b
the inverse is found with Inverse[A]. Thus, assuming that
has an inverse
c d
(ad bc 0), the inverse is
In[1070]:= Inverse
a, b , c, d
d
b
Out[1070]=
,
,
b c a d
b c a d
c
a
,
b c a d
b c a d
420
Chapter 6 Systems of Ordinary Differential Equations
3 4
EXAMPLE 6.1.5: Let A
8 0
5 2
(a)A
B; (b) B
4A; (c) the
of A 2B B; and (e) det A A .
5
10 6 9
. Compute
3 and B
6 5 7
1
inverse of AB; (d) the transpose
SOLUTION: We enter ma (corresponding to A) and mb (corresponding
to B) as nested lists where each element corresponds to a row of the
matrix. We suppress the output by ending each command with a semicolon.
In[1071]:= ma
mb
3 , 4 , 5 , 8 , 0 , 3 , 5 , 2, 1
10, 6, 9 , 6, 5, 7 ,
10, 9, 12
Entering
In[1072]:= ma mb//MatrixForm
13 10 4
Out[1072]= 14 5 10
5 11 13
adds matrix ma to mb and expresses the result in traditional matrix
form. Entering
In[1073]:= mb
Out[1073]=
4ma//MatrixForm
2 10 29
26 5 5
30 1 8
subtracts four times matrix ma from mb and expresses the result in traditional matrix form. Entering
Matrix products, when
deﬁned, are computed by
placing a period (.) between
the matrices being multiplied.
Note that a period is also
used to compute the dot
product of two vectors,
when the dot product is
deﬁned.
In[1074]:= Inverse ma.mb //MatrixForm
53
167
59
380 190 380
223 92 979
Out[1074]=
570 95 570
49
18
187
114 19
114
computes the inverse of the matrix product AB. Similarly, entering
In[1075]:= Transpose ma 2mb .mb //MatrixForm
352 90 384
269 73 277
Out[1075]=
373 98 389
6.1 Review of Matrix Algebra and Calculus
computes the transpose of A
421
2B B and entering
In[1076]:= Det ma
Out[1076]= 190
computes the determinant of ma.
EXAMPLE 6.1.6: Compute AB and BA if A
B
1
4
4
5
1
3
4
5
5
4
5
3
2
4
2 and
3
2
3
.
4
3
SOLUTION: Because A is a 3 4 matrix and B is a 4 2 matrix, AB
is deﬁned and is a 3 2 matrix. We deﬁne matrixa and matrixb with
the following commands.
In[1077]:= matrixa
1
3
4
5
5
4
In[1078]:= matrixb
1
4
4
5
2
3
4
3
5
3
2
4
2
3
We then compute the product, naming the result ab, and display ab in
MatrixForm.
In[1079]:= ab
matrixa.matrixb
MatrixForm ab
Out[1079]=
19 19
1 15
3 21
However, the matrix product BA is not deﬁned and Mathematica produces error messages when we attempt to compute it.
Remember that you can also
deﬁne matrices by going to
Input under the
Mathematica menu and
selecting Create
Table/Matrix/Palette....
After entering the desired
number of rows and columns
and pressing the OK button,
a matrix template is placed at
the location of the cursor
that you can ﬁll in.
422
Chapter 6 Systems of Ordinary Differential Equations
In[1080]:= matrixb.matrixa
Dot
dotsh
Tensors 1, 2 , 4, 3 , 4, 4 , 5, 3
and
1 , 5, 5, 4 , 3, 5, 3, 2 ,
4, 4, 2, 3 have incompatible shapes.
Out[1080]=
1, 2 , 4, 3 , 4, 4 , 5, 3
1, 5, 5, 4 ,
3 , 5, 3 , 2 , 4 , 4, 2 , 3
.
6.1.4 Eigenvalues and Eigenvectors
Let A be an n n matrix. The number Λ is an eigenvalue of A if there is a nonzero
vector, v, called an eigenvector, satisfying
Av
Λv.
(6.1)
We ﬁnd the eigenvalues of A by solving the characteristic polynomial
A
ΛI
0
(6.2)
for Λ. Once we ﬁnd the eigenvalues, the corresponding eigenvectors are found by
solving
A ΛI v 0
(6.3)
for v.
If A is a square matrix,
Eigenvalues[A]
ﬁnds the eigenvalues of A,
Eigenvectors[A]
ﬁnds the eigenvectors, and
Eigensystem[A]
ﬁnds the eigenvalues and corresponding eigenvectors.
CharacteristicPolynomial[A,lambda]
ﬁnds the characteristic polynomial of A as a function of Λ.