Chapter 7: Matrix Math (part 2)

advertisement
Matrix Mathematics
in MATLAB and Excel
Chapter 7
Matrix Mathematics
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Review: Multiplication of Matrices
• To multiple two matrices together, the matrices
must have compatible sizes:
This multiplication is possible only if the number
of columns in A is the same as the number of rows
in B
• The resultant matrix C will have the same number
of rows as A and the same number of rows as B
Engineering Computation: An Introduction Using MATLAB and Excel
Review: Multiplication of Matrices
• Easy way to remember rules for multiplication:
These values
must match
Size of Product
Matrix
Engineering Computation: An Introduction Using MATLAB and Excel
Review: Multiplication of Matrices
• Element ij of the product matrix is computed by
multiplying each element of row i of the first
matrix by the corresponding element of column j
of the second matrix, and summing the results
• In general, matrix multiplication is not
commutative:
AB ≠ B A
Engineering Computation: An Introduction Using MATLAB and Excel
Practice Problem
• Find C = A B
(2 X 4) X (4 X 3) = (2 X 3)
Engineering Computation: An Introduction Using MATLAB and Excel
Review: Transpose
• The transpose of a matrix by switching its row and
columns
• The transpose of a matrix is designated by a
superscript T:
• The transpose can also be designated with a prime
symbol (A’). This is the nomenclature used in
MATLAB
Engineering Computation: An Introduction Using MATLAB and Excel
Review: Determinate
• The determinate of a square matrix is a scalar
quantity that has some uses in matrix algebra.
Finding the determinate of 2 × 2 and 3 × 3
matrices can be done relatively easily:
• The determinate is designated as |A| or det(A)
• 2 × 2:
Engineering Computation: An Introduction Using MATLAB and Excel
Review: Inverse
• Some square matrices have an inverse
• If the inverse of a matrix exists (designated by -1
superscript), then
where I is the identity matrix – a square matrix with
1’s as the diagonal elements and 0’s as the other
elements
• The inverse of a square matrix exists only of the
determinate is non-zero
Engineering Computation: An Introduction Using MATLAB and Excel
Matrix Operations in Excel
• Excel has commands for:
• Multiplication (mmult)
• Transpose (transpose)
• Determinate (mdeterm)
• Inverse (minverse)
• Important to remember that these commands
apply to an array of cells instead of to a single cell
• When entering the command, you must identify
the entire array where the answer will be
displayed
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Multiplication
• Repeat previous problem – first enter the two
matrices:
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Multiplication
• Good practice to label the matrices:
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Multiplication
• Shading and borders help the matrices stand out:
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Multiplication
• Array of cells for the product must be selected – in
this case, a 2 × 3 array:
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Multiplication
• The MMULT function has two arguments: the
ranges of cells to be multiplied. Remember that
the order of multiplication is important.
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Multiplication
• Using the Enter key with an array command only
returns an answer in a single cell. Instead, use
Ctrl + Shift + Enter keys with array functions
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Multiplication
• Answer cells formatted:
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Transpose
• Use Ctrl + Shift + Enter to input command
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Determinate
• Since determinate is a scalar, select a single cell
and use Enter to input command
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Inversion
• Remember that only square matrices can have
inverses
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Matrix Inversion
• Ctrl + Shift + Enter to input command
Engineering Computation: An Introduction Using MATLAB and Excel
Check:
• A X A-1 = I, the identity matrix:
Engineering Computation: An Introduction Using MATLAB and Excel
Matrix Operations in MATLAB
• Matrices are easily handled in MATLAB
• We will look at commands for:
• Multiplication
• Transpose
• Determinate
• Inverse
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Matrix Input
• Recall that to enter matrices in MATLAB, the
elements are enclosed in square brackets
• Spaces or commas separate element within a row;
semi-colons separate rows
>> G = [2 5 8; -1 9 10; 3 1 2]
G =
2
5
8
-1
9
10
3
1
2
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Matrix Multiplication
>> G = [2 5 8; -1 9 10; 3 1 2]
G =
2
5
8
-1
9
10
3
1
2
>> H = [4 5; 6 12; 1 1]
H =
4
5
6
12
1
1
• Matrix multiplication
symbol is same as for
scalar multiplication
(*):
>> L = G*H
L =
46
78
60
113
20
29
Engineering Computation: An Introduction Using MATLAB and Excel
Repeat Earlier Example
• Find C = A B
>> A = [3 5 1 0; 2 -2 1 -1];
>> B = [2 3 1;0 1 0; -1 2 5; 2 2 1];
>> C = A*B
C =
5
16
8
1
4
6
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Transpose
• The transpose command switches rows and
columns of a matrix:
>> A = [10 6; 12 9; 1 3; 0 1]
A =
10
6
12
9
1
3
0
1
>> B = transpose(A)
B =
10
12
1
0
6
9
3
1
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Transpose
• Shortcut: The prime symbol (') after a matrix also
performs the tranposition:
>> A = [10 6; 12 9; 1 3; 0 1]
A =
10
6
12
9
1
3
0
1
>> B = A'
B =
10
12
1
0
6
9
3
1
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Determinate
• The det command finds the determinate of a
square matrix:
>> A = [2 5 6; -1 0 5; 3 2 2]
A =
2
5
6
-1
0
5
3
2
2
>> B = det(A)
B =
53
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Inversion
• The inv command finds the inverse of a square
matrix:
A =
2
-1
3
5
0
2
6
5
2
>> B = inv(A)
B =
-0.1887
0.0377
0.3208
-0.2642
-0.0377
0.2075
0.4717
-0.3019
0.0943
Engineering Computation: An Introduction Using MATLAB and Excel
Check:
>> C = A*B
C =
1.0000
0.0000
0
0
1.0000
-0.0000
-0.0000
-0.0000
1.0000
• Product of a matrix and its inverse is the identity
matrix
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Matrix Inversion
• Another example:
A=
Can you guess why
3 4 1
this matrix has no
5 -2 2
inverse?
6 8 2
>> B = inv(A)
Warning: Matrix is singular to working precision.
B=
Inf Inf Inf
Inf Inf Inf
What is the
Inf Inf Inf
determinate of A?
Engineering Computation: An Introduction Using MATLAB and Excel
MATLAB Matrix Inversion
3
5
6
4
-2
8
1
2
2
• Note that the third row of the matrix is simply the
first row multiplied by a constant (2). When this
happens, the matrix is singular and has no inverse.
Also, notice that the determinate of the matrix is
zero:
>> det(A)
ans =
0
Engineering Computation: An Introduction Using MATLAB and Excel
Linear Simultaneous Equations
• Consider an equation with three unknowns x, y,
and z
• If the equation contains only linear terms of x, y,
and z – that is, only constants multiplied by each
variable – and constants, then the equation is
linear
• If the equation contains any terms such as x2,
cos(x), ex, etc., then the equation is non-linear
Engineering Computation: An Introduction Using MATLAB and Excel
Linear Simultaneous Equations
• Example of a linear equation:
• With what we know about multiplying matrices,
we can write this equation as:
(1 X 3)
X (3 X 1)
=
(1 x 1)
Engineering Computation: An Introduction Using MATLAB and Excel
Linear Simultaneous Equations
• Let’s add a second equation:
• We can add this to our previous matrix equation
by adding a second row to the first matrix and to
the product:
(2 X 3)
X
(3 X 1)
=
(1 x 2)
Engineering Computation: An Introduction Using MATLAB and Excel
Linear Simultaneous Equations
• Finally, let’s add a third equation:
• Our matrix equation is now:
(3 X 3)
X
(3 X 1)
=
(1 x 3)
Engineering Computation: An Introduction Using MATLAB and Excel
Linear Simultaneous Equations
• We will use matrix mathematics to solve the
equations for x, y, and z
• The first step in solving a series of linear
simultaneous equations is to write them in matrix
form
• For n simultaneous equations and n unknowns:
where A is the coefficient matrix (n × n); X is the matrix of
unknowns (n × 1), and C is the constant matrix (n × 1)
Engineering Computation: An Introduction Using MATLAB and Excel
Linear Simultaneous Equations
• Recall that if there are more unknowns then
equations, then we cannot find a unique solution
• If there are more equations than unknowns, then
some equations must be redundant
• If there are exactly the same number of equations
and unknowns, then there may be a unique
solution. In this case the coefficient matrix will be
square
Engineering Computation: An Introduction Using MATLAB and Excel
Practice
• Write these four equations in matrix form:
Engineering Computation: An Introduction Using MATLAB and Excel
Solution
• In Chapter 8, we will learn how to find the
unknowns a, b, c, and d with matrix methods
Engineering Computation: An Introduction Using MATLAB and Excel
Download