Matrix analysis tools

advertisement
College of Engineering and Computer Science
Mechanical Engineering Department
Engineering Analysis Notes
Last updated: February 6, 2016 Larry Caretto
Computational Tools for Matrices and Solution of
Simultaneous Linear Equations
Introduction
Two common tools are available for students in ME 501AB: Excel and MATLAB. Both are
available in the Mechanical Engineering computing lab in room 1116-1118 of the Engineering
Building. Students usually have Excel on their own computers as part of the Microsoft Office
suite. A student version of MATLAB is available for educational use. This software costs about
$100. These notes describe the use of both tools for the solution of problems in matrix analysis
and the solution of simultaneous differential equations.
Excel
Array formulas. Excel contains a feature known as array formulas that allows a single formula
for an array calculation to be entered in multiple cells. It is also possible to have array formulas
that use operations on arrays to return a single value. Regardless of the nature of the result –
multiple cells or a single cell – array formulas must be completed by pressing control-shift-enter at
the same time instead of simply pressing the enter key.
Excel contains some built in array functions, such as the linest function for performing multiple
linear regressions. In addition, you can write a user defined function whose return value is an
array that occupies several cells.
Matrix formulas. Excel has three formulas for matrix analysis, minverse that computes the
inverse of a square array, mmult that computes the product of two compatible arrays, and
mdeterm that computes the determinant of a square array. These functions are described below
using the spreadsheet for the September 21 homework as an illustration. You can download this
spreadsheet Gaussian.xls from the course web site. The spreadsheet has the following URL:
http://www.csun.edu/~lcaretto/me501a/Gaussian.xls. The Excel matrix functions do not allow
blank cells in the matrices; you must enter a zero to get a value of zero.
You can work with the actual spreadsheet to see better how the formulas work and to practice
entering formulas on your own. The next page shows the formulas on the spreadsheet and there
is a copy of the spreadsheet results on page three.
minverse computes the inverse of a square array. To use this function for an n by n array select
a region of the spreadsheet that has the same size as the array for which you want the inverse.
Enter the formula =minverse( <range of original array> ) in the formula bar and press control-shiftenter. In the sample spreadsheet we find the inverse of the four-by-four array entered in cells
A4:D7. To do this we first select another four-by-four range: A11:D14. Once we have selected
these cells we enter the formula minverse(A4:D7) in the formula bar and press control-shift-enter.
This gives the inverse in the selected cells.
mmult multiplies two compatible matrices. This function has two arguments; the first argument is
the range for the matrix on the left and the second argument is the range for the matrix on the
right. If the left matrix has n rows and p columns, the matrix on the right must have p rows to be
compatible. If this right matrix has p rows and m columns, the product matrix will have n rows and
m columns.
Engineering Building Room 1333
Email: lcaretto@csun.edu
Mail Code
8348
Phone: 818.677.6448
Fax: 818.677.7062
Tools for matrix analysis
L. S. Caretto, February 6, 2016
Page 2
Copy of example Spreadsheet showing formulas in various cells
The formula display shows the same formula in each cell for which the array formula applies. However, the formula is only entered once after
selecting the entire range of cells.
The spreadsheet on the next page gives the results of the various formulas shown here.
Tools for matrix analysis
L. S. Caretto, February 6, 2016
Page 3
Copy of example spreadsheet showing results of array formulas
To apply the mmult function for multiplying an n by p matrix and a p by m matrix you first select a
range of n rows and m columns to contain the product matrix. Then you enter the following
formula in the formula bar: =mmult( <range for n by p matrix>, <range for p by m matrix> ). As
usual you press control-shift-enter after you type the formula to get the results.
The mmult function is illustrated in two locations on the spreadsheet. The first location is in cells
F11:F14. This range contains the formula mmult( A11:D14, F4:F7 ). In this application mmult is
finding the solution to the matrix equation Ax = b as x = A-1b. The range A11:D14 contains A-1
that was found by the minverse function as noted above. The range F4:F7 contains the b vector.
The mmult command gives the product A-1b, which is the solution x sought in this example.
A second example of the mmult function is contained in cells A17:D20. Here we multiply A by its
inverse to produce the expected unit matrix. We see that roundoff errors in the multiplication
leave us with small residual values instead of zeros for the off-diagonal terms. The diagonal
terms are all found to be 1, however. This computation was done by first selecting cells A17:D20
and then entering the formula =mmult(A11:D14, A4:D7) in the formula bar (followed by controlshift-enter.)
mdeterm computes the determinant of a square array. This is not illustrated in the spreadsheet.
This formula is not an Excel array formula. It is a single cell formula that takes a range as an
argument. We could compute the determinant of the original array in cells A4:D7 by entering the
formula = mdeterm(A4:D7) in a blank cell and pressing only the enter key. Similarly, we could
find the determinant of the inverse matrix by entering the formula mdeterm(A10:D14) in a blank
cell. How would you compute the determinant of the unit matrix shown in this spreadsheet?
Combining functions. A single formula may include more than one function. For example, we
could compute the product of three compatible matrices, ABC, by the following formula: =mmult(
mmult( <range for A>, <range for B> ), <range for C> ). We could also use the formula
Tools for matrix analysis
L. S. Caretto, February 6, 2016
Page 4
=mmult(<range for A>, mmult( <range for B> , <range for C> ) ). The final product will have the
same number of rows as the A matrix and the same number of columns as the C matrix.
We can also combine the mmult and minverse functions to solve Ax = b as x = A-1b in a single
step. This is illustrated in cells F17:20 of the example spreadsheet. These cells contain the
following formula: =mmult(minverse(A4:D7), F4:F7).
MATLAB
Basic ideas. MATLAB is a large-scale tool that started as a set of functions for matrix operations.
(The mat in the title MATLAB is short for matrix.) It now consists of a basic package along with
toolboxes that can be added for additional types of calculations. The student version has some of
the major toolboxes, including the ability to perform symbolic operations. These notes present
only a very brief overview of how MATLAB can be used to perform matrix calculations.
Variables, data entry and expressions. Mathematical quantities in MATLAB are represented by
case-sensitive variable names. Thus the symbol A is different from the symbol a. You can enter
data directly into MATLAB or you can import data from other files, including Excel files and text
files.
To enter the A matrix shown on the spreadsheet we use the MATLAB command below:
A=[1 -1 0 3; 4 -12 -9 1; 6
3 11 -4;13
6 -7
8]
This shows how the data are entered: row-by-row, giving all the elements in a row, separated by a
space. A semicolon is used to denote the end of a row. MATLAB ignores white spaces and the
enter key in the middle of a formula. Thus it is also possible to enter the formula as shown below.
This makes no difference, but may make it easier to compare the input with the original data.
A = [ 1 -1 0 3;
4 -12 -9 1;
6
3 11 -4;
13
6 -7 8 ]
Note that we would enter the b vector from the example spread sheet as [ 1; -21; 35; 44 ], placing
a semicolon after each data entry to indicate a column matrix. Without these semicolons, our
entry would be interpreted as a row matrix.
MATLAB formulas can be entered with or without a variable for the result. If we do not enter a
variable for the result, the program automatically stores the result in the variable ans. MATLAB
variables can apply to arrays or to scalars. (There is no dimensioning required as it typical in
programming languages like C++ or Basic.)
MATLAB has the usual set of arithmetic operators: + for addition, - for subtraction, * for
multiplication and / for division. When applied to matrices, the * performs matrix multiplication and
the / in the formula A/B is the same as AB-1. There is also a backslash operator such that A\B is
the same as A-1B. (It is also possible to perform element-by-element multiplication and division
using the operators .* and ./. These operators require both arrays to be the same size, as in
matrix addition and subtraction. The formula C = A./B produces a matrix, C, in which cij = aijbij.
MATLAB can handle multiplication and division of matrices by scalars by the usual * and /
operators. Thus 3 * A or A/10 would be give a result in which each element of A is respectively
multiplied by three or divided by 10. MATLAB also has the usual set of mathematical functions.
These include sin, cos, log, exp, sqrt, etc. It automatically handles complex numbers. Functions
that operate on matrices will give matrix results. Thus B = sin(A) gives a matrix, B, such that b ij =
sin(aij).
Tools for matrix analysis
L. S. Caretto, February 6, 2016
Page 5
Solving simultaneous equations in MATLAB. This is done by the / or \ operator. The \
operator is the preferred one. If we have entered the A matrix and the solution vector, b, as
MATLAB variables A and b, the formula x = A \ b solves the equation Ax = b. A copy of the
MATLAB command and solution for the A and b matrices entered above is shown below. The
command is the text following the EDU>> prompt. This is the prompt in the educational version of
MATLAB. The remainder of the test is the result.
EDU>> x = A\b
x =
3.0000
2.0000
1.0000
0
This is the same solution found previously.
Eigenvalues and eigenvectors. MATLAB can also be used to produce matrix eigenvalues and
eigenvectors. The command eig(A) produces a column matrix whose components are the
eigenvalues of the square matrix A. The command [V,D] = eig(A) produces two matrices, V and
D, which are the size of the original A matrix. The D matrix is a diagonal matrix whose
components are the eigenvalues of the A matrix. The columns of the V matrix are the
eigenvectors of the original A matrix. For the A matrix shown on the previous page, the command
and the results for the eigenvalues and eigenvectors are shown below.
EDU>> [V, D] = eig(A)
V =
0.2277 + 0.1429i
0.6893
-0.2826 - 0.1323i
-0.5404 - 0.2515i
0.2277 - 0.1429i
0.6893
-0.2826 + 0.1323i
-0.5404 + 0.2515i
0.3420
-0.1209
0.5099
0.7800
0.1672
0.1973
-0.4291
0.8654
0
-7.7724 - 2.1911i
0
0
0
0
8.1948
0
0
0
0
15.3501
D =
-7.7724 + 2.1911i
0
0
0
We see that this matrix has two real and two complex eigenvalues. This example shows the
general result that complex eigenvalues for a real matrix occur in complex conjugate pairs. In
addition, the eigenvectors corresponding to such eigenvalues are complex conjugates.
In MATLAB, matrix inverses can be found by the function inv(<matrix>). The MATLAB command
and results for finding the inverse of the V matrix are shown below.
EDU>> Vinv = inv(V)
Vinv =
0.0843
0.0843
1.1788
0.1333
+
+
+
1.8761i
1.8761i
0.0000i
0.0000i
0.7346
0.7346
0.1336
0.0176
+
-
1.3407i
1.3407i
0.0000i
0.0000i
0.2267
0.2267
0.9082
-1.0278
+
-
0.8470i
0.8470i
0.0000i
0.0000i
-0.0713
-0.0713
0.1921
0.6161
+
-
0.4767i
0.4767i
0.0000i
0.0000i
Tools for matrix analysis
L. S. Caretto, February 6, 2016
Page 6
All the elements in this matrix appear to be complex. However, the complex components with a
value of 0.0000 are probably zero to within the roundoff error of the calculations.
An important result of eigenvalues is that any matrix A with a set of linearly independent
eigenvectors will satisfy the equation V-1AV = D. We can check this result for the values obtained
for V, V-1, A, and D by computing the difference D – V-1AV. We can write this calculation as the
single MATLAB equation shown in the command line below. The results are also shown.
EDU>> diff = D - Vinv * A * V
diff =
1.0e-013 *
0.0444
0.0178
-0.0243
0.1151
+
+
+
0.0711i
0.0799i
0.0072i
0.0216i
0.0033
0.0444
-0.0223
0.1159
+
-
0.0844i
0.0622i
0.0050i
0.0228i
0.0189
0.0222
0.0355
-0.0089
+
-
0.0400i
0.0799i
0.0001i
0.0001i
0.0594
0.0250
0.0400
0
+
+
-
0.0577i
0.0266i
0.0005i
0.0001i
In this output, all the matrix elements are multiplied by the initial factor of 10-13. Although the
results are not exactly zero, they are reduced to the roundoff error, giving a specific example of
the result that D = V-1AV.
Other matrix functions. Additional functions available for matrix results are shown below. In all
cases, typing the command prints the result assigned to the default variable ans. It is also
possible to assign the result of the command to another variable. For example, the first item
below notes that the transpose of a MATLAB matrix, A, is given A’. Typing A’ on the command
line will give the transpose assigned to the variable ans. Typing B = A’ on the command line will
give the answer assigned to the variable B.
The transpose of a matrix represented by the MATLAB variable, A, is found as A’. For complex
numbers this gives the transpose complex conjugate. To get the simple inverse of a matrix with
complex elements use the notation A.’.
The inverse of a matrix represented by the MATLAB variable, A, is found as inv(A).
The norm of a matrix represented by the MATLAB variable, A, is found as norm(A). It is possible
to obtain different norms by the two argument function norm(A, p), where p may be 1, 2, inf, or
‘fro’ to give the one norm, two norm, infinity norm or Frobenius norm, respectively. For matrix that
is not a row or column matrix, the two norm is the largest singular value found in a singular value
decomposition. This is a transform that finds three matrices, U, V, and S, such that the original
matrix A = USVT, and S is a diagonal matrix, whose components are called the singular values. If
A is Hermitian, the singular values are the same as the eigenvalues.
The rank of a matrix represented by the MATLAB variable, A, is found as rank(A).
The determinant of a matrix represented by the MATLAB variable, A, is found as det(A).
The condition number of a matrix represented by the MATLAB variable, A, is found as cond(A).
You can generate an n by n identity matrix by the command eye(n).
You can generate an n by m matrix of zeros by the command zeros( n, m ).
You can generate an n by m matrix of ones by the command ones( n, m ).
You get the trace (sum of diagonal elements) by the command trace(A).
Tools for matrix analysis
L. S. Caretto, February 6, 2016
Page 7
You can compute the sum of matrix elements by the command sum(A). If A is a column vector,
this gives the sum of the elements. If A has more than one column this command returns a
column vector giving the sum of all the elements in a each column. To get a column vector of row
sums use the command sum(A,2). See the examples of the sum function in the symbolic math
section below.
You can generate a diagonal matrix by the command diag( V, k ), where V is a vector with n
components and k is an integer. The n components of the vector, V, are placed on diagonal k.
For this function k = 0 denotes the main diagonal; positive values of k indicate diagonals above
the main diagonal and negative values of k denote diagonals below the main diagonal. Since the
vector v has n components and the elements can be placed on any diagonal, the final size of the
matrix created by the diag command is n + abs(k).
The diag command can be combined with the ones command to produce multidiagonal matrices.
For example, a tridiagonal matrix that occurs in some finite-difference calculations has -2 on the
main diagonal and 1 on the diagonals above and below this diagonal. The creation of such a
matrix in MATLAB is illustrated below. (Prior to this command the value of m has been set to 10.)
EDU>> -2*diag(ones(m,1),0) + diag(ones(m-1,1),1) + diag(ones(m-1,1),-1)
ans =
-2
1
0
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
0
0
0
1
-2
1
0
0
0
0
0
0
0
0
1
-2
In this command we have generated the vector, V, used in the diag command by using the ones
command. Here the second argument to the ones command is 1 giving us a one column matrix,
which is the vector argument to diag. Our m by m matrix has m elements on the principal
diagonal. Thus we generate a vector with m ones to create the principal diagonal. Since the
diagonals that are one diagonal above or one diagonal below the principal diagonal have m – 1
elements, we have to generate a vector with m – 1 elements to generate these diagonals.
Symbolic mathematics. MATLAB uses the MAPLE engine developed at the University of
Waterloo for symbolic mathematical computations. There are many symbolic operations including
differentiation, integration and solution of differential equations that can be performed in MATLAB.
All such operations start with the use of the syms command to specify that variables are to be
treated as mathematical symbols instead of numerical values.
Symbolic mathematics can be applied to matrix operations as shown below.
EDU>> syms a b c d e f g h k A
EDU>> A = [ a b c; d e f ; g h k ]
A =
[ a, b, c]
[ d, e, f]
[ g, h, k]
EDU>> A'
ans =
Tools for matrix analysis
L. S. Caretto, February 6, 2016
Page 8
[ conj(a), conj(d), conj(g)]
[ conj(b), conj(e), conj(h)]
[ conj(c), conj(f), conj(k)]
MATLAB assumes that symbols may have complex values and the conj() function gives the
complex comjugate of a variable. Recall that the transpose operation give the complex conjugate
of the transposed matrix. To get the simple transpose, we use the command below.
EDU>> A.'
ans =
[ a, d, g]
[ b, e, h]
[ c, f, k]
EDU>> inv(A)
ans =
[
(e*k-f*h)/(a*e*k-a*f*h-d*b*k+d*c*h+g*b*f-g*c*e), rest of first row ]
[ -(d*k-f*g)/(a*e*k-a*f*h-d*b*k+d*c*h+g*b*f-g*c*e), rest of second row]
[ -(-d*h+e*g)/(a*e*k-a*f*h-d*b*k+d*c*h+g*b*f-g*c*e), rest of third row]
EDU>> det(A)
ans =
a*e*k-a*f*h-d*b*k+d*c*h+g*b*f-g*c*e
EDU>> rank(A)
ans =
3
EDU>> trace(A)
ans =
a+e+k
The following examples demonstrate the use of the sum function discussing above. If A was a
numerical matrix rather than a symbolic matrix, all the results below would be numerical.
EDU>> sum(A)
ans =
[ a+d+g, b+e+h, c+f+k]
EDU>> sum(A,2)
ans =
[ a+b+c]
[ d+e+f]
[ g+h+k]
EDU>> sum(sum(A))
ans =
a+d+g+b+e+h+c+f+k
Download