array operators - yilectronics.com · a and b must have the same number of columns. in terms of the...
TRANSCRIPT
Array Operators(element-by-element)
+ Addition A+B adds B and A
- SubtractionA-B subtracts B from A
.* Element-wise multiplicationA.*B is the element-by-element product of A and B.
.^ Element-wise powerA.^B is the matrix with elements A(i,j) to the B(i,j) power.
./ Right array divisionA./B is the matrix with elements A(i,j)/B(i,j).
.\ Left array divisionA.\B is the matrix with elements B(i,j)/A(i,j).
.' Array transposeA.' is the array transpose of A. For complex matrices, this does not involve conjugation.
Matrix Operators
* Matrix multiplication
C = A*B is the linear algebraic product of the matrices A and B. The number of columns of A must equal the number of rows of B.
/ Matrix right division
x = B/A is the solution to the equation xA = B. Matrices A and B must have the same number of columns. In terms of the left division operator, B/A = (A'\B')'.
\ Matrix left division x = A\B is the solution to the equation Ax = B. Matrices A and B must have the same number of rows.
^ Matrix power A^B is A to the power B, if B is a scalar. For other values of B, the calculation involves eigenvalues and eigenvectors.
' Complex conjugate transpose
A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.
Matrix Multiplication
𝐴11 𝐴12𝐴21 𝐴22𝐴31 𝐴32
∗𝐵11𝐵21
= 𝐴11𝐵11+ 𝐴12𝐵21
𝐴21𝐵11 + 𝐴22𝐵21
𝐴31𝐵11 + 𝐴32𝐵21
Matrix notation can represent equations:
3a -2b = 9
5a + 9b = 16
can be written:3 −25 9
𝑎𝑏
=916
Matrix Multiplication
because… 3 −25 9
𝑎𝑏
=916
3a -2b
5a + 9b
Matrix Multiplication
A =
𝐴11 𝐴12 𝐴13𝐴21 𝐴22 𝐴23𝐴31 𝐴32 𝐴33𝐴41 𝐴42 𝐴43
, B =𝐵11 𝐵12𝐵21 𝐵22𝐵31 𝐵32
A * B = (𝐴11𝐵11 + 𝐴12𝐵21 + 𝐴13𝐵31) (𝐴11𝐵12 + 𝐴12𝐵22 + 𝐴13𝐵32)(𝐴21𝐵11 + 𝐴22𝐵21 + 𝐴23𝐵31) (𝐴21𝐵12 + 𝐴22𝐵22 + 𝐴23𝐵32)(𝐴31𝐵11 + 𝐴32𝐵21 + 𝐴33𝐵31) (𝐴31𝐵12 + 𝐴32𝐵22 + 𝐴33𝐵32)(𝐴41𝐵11 + 𝐴42𝐵21 + 𝐴43𝐵31) (𝐴41𝐵12 + 𝐴42𝐵22 + 𝐴43𝐵32)
Rules on size
These
dimensions
must match
The result
has these
dimensions
3 x 2 2 x 2 = 3 x 22 −11 30 4
2 31 1
=3 55 64 4
2 x 2 3 x 2
2 31 1
2 −11 30 4
=
A*B ≠ B*A !!
Legal: dimensions match
Illegal: dimensions do not
match
Even when it is legal,A*B is not commutative
3 x 3 2 x 2
1 2
A = 3 4
5 6
A*B =
9 12 15
19 26 33
29 40 51
B*A =
22 28
49 64
B = 1 2 3
4 5 6
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, which we’ll cover later.
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)
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) = (2 x 1)
Linear Simultaneous Equations Finally, let’s add a third equation:
Our matrix equation is now:
(3 X 3) X (3 X 1) = (3 x 1)
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 b is the constant matrix (n × 1)
Ax=b
Our matrix equation
Ax = b
Practice Write these four equations in matrix form:
Rewrite with coefficients on the left and constants on the right
7a + 2b = 3 + c + d
2d + 5b = 5
a + b = -3(c+d)
2a – b + 4c – 9 = 0
7a + 2b – c – d = 3
5b +2d = 5
a + b +3c+3d = 0
2a – b + 4c = 9
Solution
we will learn how to find the unknowns a, b, c, and dwith matrix methods – in both MATLAB and Excel
Solution to Simultaneous Eqns Consider the following 2 linear equations
Are there values of x and y that fit both equations?
That is, are there values of x and y that simultaneously satisfy both equations?
Before using MATLAB or Excel, we will solve this problem manually:
Begin by writing the second equation as:
Solution by Substitution Substitute the second equation into the first:
Solve for x:
Substitute x into the second equation:
Graphical Solution For equations with two variables, a graphical solution
is possible
For each equation, plot two points to define a line
If the lines intersect, then the intersection point is a solution to both equations
Graphical Solution
2x + 3y = 14
-4x + y = 28
Linear Equations - MATLAB
Solving sets of linear equations manually can be a difficult task for all but the simplest systems.
MATLAB tools can be used to do this painlessly.
The simplest case of 2 unknowns will be considered first.
Definitions Determinant: The determinant is a special number
associated to any square matrix.
A matrix is invertible if and only if its determinant is non-zero.
The determinant is calculated according to specific rules.
In MATLAB, the determinant is found with the determinant function, det(A)
For a 2 x 2 matrix, the determinant is:
21122211
2221
1211
aaaa
aa
aaA
Definitions The determinant of a 3×3 matrix is given by
det (A) = aei + bfg + cdh − ceg − afh − bdi.
Definitions Identity Matrix, I: The identity matrix is a square
matrix in which the diagonal elements are 1’s and the rest of the elements are 0’s.
When the identity matrix multiplies another matrix (or vector), that matrix (or vector) is unchanged. This is equivalent to multiplying a scalar by 1. AI = IA = A
In MATLAB, eye(N), is an N x N identity matrix
15
2
8
15
2
8
1 0 0
0 1 0
0 0 1
or 5 11 4
8 3 7
1 0 0
0 1 0
0 0 1
5 11 4
8 3 7
Definitions Inverse of a Matrix – a matrix B is the inverse of matrix
A if when the two matrices are multiplied the product is the identity matrix.
Both matrices must be square and the multiplication can be BA or AB
The inverse of a matrix A is typically written at A-1.
In MATLAB, the inverse matrix can be obtained either by raising the matrix to the power of -1, or with inv(A) function.
Linear Simultaneous Equations Recall that if there are more unknowns than
equations, then we cannot find a unique solution
If there are more equations than unknowns, then some equations may 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
Ax=b
Solution of System of Linear Equations – Matrix approach
Multiply both sides of the equation by the inverse of the coefficient matrix. Remember that the order of multiplication is important.
Since the inverse of a matrix times that matrix is equal to the identity matrix,
bAxAA 11
bAxI 1
Solution of System of Linear Equations – Matrix approach
Since the identity matrix times another matrix is equal to that matrix,
Therefore, we can find the unknown variables by multiplying the inverse of the coefficient matrix by the constant matrix
bAxI 1
bAx 1
Consider:
2x + 3y = 12
4x – 3y = 6
In matrix notation:
This represents a set of linear equations, which is commonly put in the form of Ax=b.
6
12
3- 4
3 2
y
x
6
12b , x,
3- 4
3 2
y
xA
In MATLAB, the matrix equation Ax=b is solved for x by the left division operator:
x = A\b
The problem we solved by substitution:
>> A = [2 3;-4 1]
A =
2 3
-4 1
>> b = [14 28]'
b =
14
28
>> x = A\b
x =
-5
8
Good idea to check it:
2(-5) + 3(8) = 14?
Array DivisionThere are 2 types of array division
Left division \:
Used to solve the matrix equation Ax = b (seen on previous slide.)
Where x = A\b
Right division /:
Used to solve the matrix equation xC=d (where x and d are row vectors…also note that C = A’, and d = b’)
x = d/C
Example of Left and Right Division
Use matrix operations to solve the following system of linear equations
Using the rules of linear algebra demonstrated earlier, the above system of equations can be written in the matrix form
Ax=b or xC=d
03106
4282
8624
zyx
zyx
zyx
Left Division Solution
>> A=[4 -2 6;2 8 2; 6 10 3];
>> b=[8; 4; 0];
>> x=A\b
x =
-1.8049
0.2927
2.6341
0
4
8
3 10 6
2 8 2
6 2- 4
z
y
x
xyz
Ax=b
Right Division Solution
0 4 8
3 2 6
10 8 2-
6 2 4
zyx
x
>> C=[4 2 6; -2 8 10; 6 2 3];
>> d=[8 4 0];
>> x=d/C
x =
-1.8049 0.2927 2.6341
y z
xC=d
Remember
C=A’
d=b’
Exercises Solve the following sets of equations using left
division and check the answer
31835
7753
36542
zyx
zyx
zyx
0
1201215
801220
321
32
31
xxx
xx
xx
If the number of equations in the set equals the number
of unknown variables, the matrix A is square and
MATLAB provides two ways of solving the equation set
Ax b:
1. The matrix inverse method; solve for x by typing x = inv(A)*b
2. The matrix left-division method; solve for x by typing x = A\b
Solving Linear Eqns: Summary
Method we’ll use
Left division is2-3 times fasterOften produces smaller error than inv()
Sometimes inv()can produce erroneous results
(continued)
If A is square and if MATLAB does not generate an
error message when you use one of these methods,
then the set has a unique solution, which is given by
the left-division method.
You can always check the solution for x by typing A*x
to see if the result is the same as b.
Solving Linear Eqns: Summary
Note: This is Matrix
multiplication
In-class exercise I1. Extract the highlighted area and assign it to A
1. Find the max value in each column of matrix A and assign it
to Amaxcol.
2. Find the single max value in A and assign it to Amax.
3. Extract the first column of A and assign it to A1stCol.
4. If Ax=b, b=[16;8;-20]; use Matlab to solve x.
1. Find the single max value in the highlighted area.
2. Extract the first row of the highlighted area.
3. Solve the linear equation if the highlighted area is called B,
and Bx=[-5;9;9]
In-class exercise II