 Chapter 10 Review: Matrix Algebra

Presentation on theme: "Chapter 10 Review: Matrix Algebra"— Presentation transcript:

Chapter 10 Review: Matrix Algebra
Introduction to MATLAB 7 Engineering 161

Matrix Computations In Chapter 9 we are going to explore more about matrix computations using MATLAB. Concepts like the transpose, dot product, matrix multiplication, determinants, and the inverse of a matrix help us solve sets of simultaneous equations often encountered in engineering. Additionally we’ll explore other built in functions that help us construct and manipulate matrices. We’ll work some problems manipulating matrices and I’ll show you a program I wrote to solve a set simultaneous equations encounter in circuit analysis that makes use of the control structures we studied in Chapter 8 and matrix computations.

The Transpose Operator
The transpose of a matrix is a new matrix in which the rows of the original matrix become the columns of the new matrix. Normally in mathematics the symbol “T” is used to designate the transpose operator, in MATLAB we use the single quote, “ ' “. We’ve already seen the use of the transpose operator in some of our first assignments, suppose >> x = [ 1, 2, 3]; >> y = x.^2 >> [x’, y’] prints the table Here the matrix [x’,y’] has two columns, the first being x’ and the second being y’.

The Transpose Operator II
More generally, for some arbitrary m x n matrix A, A' is a n x m matrix. For example, >>A = [ 1, 2, 3; 4, 5, 6]; %2 rows, 3 columns >>A' 1 4 2 5 3 6 Whereas A is a 2 x 3 matrix, A' is a 3 x 2 matrix. The (i,j) element of A becomes the (j,i) element of A'.

Dot Product The Dot Product operator is often used in manipulating vectors in calculus and physics as well as engineering. This operator operates on two row or column vectors and computes a scalar quantity. >> A = [ 1, 2, 3 ]; >> B = [ -1, 0, 1]; >> dot (A,B) = 1* (-1) + 2*0 + 3*1 = 2 or equivalently dot (A,B) = sum (A.*B) In engineering, the length of a vector L is given by the expression | L | = sqrt (dot (A,A)). sum (A.^2) is the length squared. For two vectors, A and B, the cosine of the angle between A and B is given by the dot product of A and B divided by the length of A and the length of B, namely, cos (angle) = dot (A,B)/ |A|*|B|

Matrix Multiplication
Matrix multiplication A*B is key for the process of solving simultaneous equations, so we need to understand how it works and when it is defined. It is defined only when the number of columns of A equals the number of rows of B, or If A is a 2 x 3 matrix and B is a 3 x 4 matrix, then the result written mathematically as AB is a 2 x 4, i.e., a 2 row and 4 column matrix. In MATLAB we would compute this new matrix with the command A*B. Note there is no “ . “ involved with the operator. If A is a 2 x 3 matrix and C is a 4 x 2 matrix, matrix multiplication is not defined. (In both of these cases, array multiplication, .*, is not defined. Why?)

Matrix Multiplication II
When the multiplication is defined, the element ci,j in the new matrix is computed by calculating the dot product of row i of A with column j of B. This operation must be performed for each element ci,j in the new matrix on the appropriate row and column. In MATLAB we’d write C = A*B to carry out this operation. With A and B being matrices conformable for multiplication, MATLAB knows what to do. Just because A*B is defined does not mean that B*A is defined. When A and B are square matrices of the same order, then A*B and B*A are both defined. Note that A*B need not be equal to B*A. We’ll see in a moment where matrix multiplication comes into play when solving sets of simultaneous equations.

Matrix Powers Square matrices, an n x n matrix A, can be raised to a power by using the ^ operator. Note this is different from the array exponentiation we discussed earlier, where each element of A is raised to a power using the .^ operator. Raising a matrix to a power involves matrix multiplication and hence only applies to square matrices. Here >> C = A^2 % A^2 ≠ A.*2 means that C is equal to A multiplied by itself. Similarly, C = A^3 is equivalent to C = A*A*A

Matrix Inverse Remember from mathematics that for two functions, f(x) and g(x) we say that g(x) is the inverse of f(x) if f(x) * g(x) = 1, similarly, f(x) is the inverse of g(x) if f(x) * g(x) = 1. For a square matrix A, if A*B = I and B*A = I where I is the identity matrix, ones along the diagonal and zeros everywhere else, then B is the inverse of A. To compute the inverse of A, simply write B = inv(A) Many square matrices A do not have inverses, they are called singular. Use the built in functions det(A) or rank(A) to determine if the square matrix A has an inverse before trying to use it in calculations. (More on this topic in a moment.)

Determinants A determinant is a scalar computed from the entries of a square matrix. It is used in the process of computing a matrix’s inverse and other interesting “things.” When you study linear algebra you will discover that not all square matrices have inverses. For the inverse of a square matrix to exist, the determinant of the matrix must be non-zero. A zero determinant tells us a number of things about a square matrix, again things you will learn in linear algebra. For our purposes, for a square matrix A of any order, MATLAB provides the built in function det to calculate the determinant of A, >> det (A) returns a scalar, the determinant of A.

Cross Products Cross product sometimes call vector product operates on two vectors (row vectors) and produces a third vector that is perpendicular to the plane formed by the original two vectors. For example, consider the two vectors in the x-y plane, A = [1 2 0] and B = [3 4 0], then C = cross(A,B) is a third vector given by C = [0 0 -2] Cross product and dot product are two operations commonly used in physics and electrical engineering.

Solutions to Systems of Linear Equations
Sets of simultaneous equations or systems of linear equations occur often in engineering problem solving. Consider the following set of linear equations; 3x + 2y -z = 10 -x +3y + 2z = 5 x -y -z = -1 Define two matrices A and B as >> A = [ 3, 2, -1; -1, 3, 2; 1, -1, -1]; % coefficient’s matrix >> B = [ 10; 5; 1] % column vector Note A is a 3 x 3 matrix and B is a 3 x 1 column vector.

Solutions to Systems of Linear Equations II
In linear algebra we would define a third matrix, X as X = [ x; y; z] a column vector of the unknowns and we’d write the relationship that AX = B. Convince yourself that A*X = B following the rules of matrix multiplications reproduces the set of linear equations that we started with. To solve this set of equations using linear algebra we need first to find the inverse of A. The inverse of a matrix is another matrix such that when you multiple the inverse of A and A together you get the identity matrix, a special matrix with 1’s along the diagonal and zeros everywhere else. The identity matrix is special because when you multiple any matrix by the identity matrix, it reproduces the original matrix. To make a long story short, consider the following;

Solutions of Systems of Linear Equations III
Continuing, A*X = B inv (A) * A *X = inv (A) * B or I *X = inv (A) * B or X = inv (A) * B So to compute the solution for x, y, and z, we need to find the inverse of the matrix A and multiple it by B. In MATLAB, to compute the inverse of a square matrix A, use the inv (A) command. Then to compute X we’d enter the statement >> X = inv(A)*B where X will be a 3 element column vector in this case. It is as easy as that.

Solutions to Systems of Linear Equations IV
Remember we said that not all square matrices have inverses. We require that determinant be non-zero. An equivalent condition is that all the equations in the set of linear equations be independent, i.e., you can not derive one or more of the equations from linear combinations of the others. This is equivalent to saying that the rank (A) = n, the order of A, i.e., for A an n x n matrix. Use the MATLAB command rank (A) to see that all the equations are independent before trying to compute the inv (A), if not you’ll get an error message from MATLAB. When rank (A) < n, we say the matrix is singular and hence doesn’t have an inverse. This also means that the solution to the set of linear equations can not be determined uniquely.

Solutions to Systems of Linear Equations V
MATLAB provides a second way to solve sets of equations like this that is generally faster than computing the inverse especially for large matrices. It is called Gaussian elimination. To use this method simply write following MATLAB expression >> X = A\B Note that we are using the forward slanted bar (called matrix left division) not the normal division bar “ / “. The forward slanted bar tells MATLAB to perform Gaussian elimination on the matrices A,B to compute the column vector X. The notation may seem strange until you study linear algebra, then the notation will seem obvious.

Special Matrices MATLAB contains a set of special functions that generate some special matrices. We’ll look at some of these. The zeros function generates a matrix containing all zeroes; >> A = zeros (3); % A is a 3X3 matrix of all 0’s >> B = zeros (2,3); % B is a 2X3 matrix of all 0’s To determine the size of a matrix, use the size command. For example, >>[m,n] = size (B); would return two values, m = 2 and n = 3. >> D = zeros (size (B)); creates a 2 x 3 matrix D of all zeros.

Special Matrices II Similarly for the ones function. Here, ones(2) would create a 2X2 square matrix of all ones, while the command ones(3,4) would create a matrix of 3 rows and 4 columns of all ones. The eye command is used to create diagonal matrices. eye(3) would create a 3X3 square matrix with ones along the diagonal and zeroes everywhere else. We’ve called this the identity matrix previously. eye(2,3) would create a matrix of 2 rows and 3 columns, element 1,1 and 2,2 would be set equal to 1 and zero everywhere else. A 3x3 identity matrix I is created by I = eye(3)

Special Matrices III The diag function can be used to either define a diagonal matrix or extract the diagonal from a matrix. For B = [1, 2, 3] a row vector, A = diag (B) creates a 3X3 matrix A with the elements along the diagonal being the elements from B. If A is a square matrix, then C = diag (A) extracts the elements along the diagonal of A and creates the row vector C. You can pass a second parameter to the diag function, an integer to tell the function to choose a diagonal other than the main diagonal. k = 1 tells the function to choose the diagonal above the main diagonal, while k = -1 specifies the one below the main diagonal. Consider the example,

Special Matrices IV >>B = [1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16] B = >>C = diag(B,1) C = By specifying k other than 1 or -1, you can selected arbitrary diagonals above or below the main diagonal.

Other Matrices MATLAB contains build in functions that will generate matrices that are useful for testing numerical techniques, that serve in computational algorithms or that are just plain interesting, consider pascal(n) magic(n) v= [1 2 3]; vander(v)

Chapter 9 Assignments These assignments let you practice the concepts discussed in Chapter 9. Assignments 10.18, 10.20 Note: Chp. 11 is about Data Types. You might want to read over the material on floating point, integers and complex numbers to acquaint yourself to these other data types.

Download ppt "Chapter 10 Review: Matrix Algebra"

Similar presentations