KYM134 Computer Programming - PowerPoint PPT Presentation

1 / 108
About This Presentation
Title:

KYM134 Computer Programming

Description:

Fall 2005/2006. KYM134. 1. KYM134. Computer Programming. Ankara University. Chemical Engineering Department. Assistant Prof. Dr. Refik SAMET ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 109
Provided by: ytarh
Category:

less

Transcript and Presenter's Notes

Title: KYM134 Computer Programming


1
KYM134Computer Programming
Matrices and Operations
  • Ankara University
  • Chemical Engineering Department
  • Assistant Prof. Dr. Refik SAMET

2
Crash course on Matrices
  • Definitions and Terms
  • Special Matrices
  • Matrix Algebra
  • Determinant and Inverse
  • Application Examples

3
  • Definitions and Terms
  • Vector, matrix
  • Element, subscript
  • "Matrices of the same kind"

4
Vector
A vector is a column of numbers consisting of n
numbers
The values shown by ai are the elements of
vector a.
A
5
Matrix (Pl. Matrices)
A matrix is a rectangular array of numbers
consisting of m rows and n
columns.
The values shown by aij are the elements of
vector a.
6
Matrix example
This matrix has 3 rows rows and 4 columns. We
say it is a 3 x 4 (3 by 4) matrix.
We denote the element on the second row and
fourth column with a2,4
7
Subscripted expression
Elements are denoted using the indexed notation
ai,j

Subscripted expression
Column index
Row index
8
"Matrices of the same kind"
Matrices A and B are of the same kind if and
only if A has as many rows as B, and
A has as many columns as B AMN BMN
are of the same kind.
We may often say that A and B are "of the same
order."
9
  • Special Matrices
  • Row / column matrices
  • Zero matrix
  • Square matrix
  • Diagonal matrix
  • Unit matrix
  • Symmetric matrix
  • Transpose
  • Orthogonal matrix

10
Row matrix / column matrix
A matrix with one row is called a row matrix.
A matrix with one column is called a column
matrix.
Also called row-vector and column-vector.
11
Zero Matrix
A zero matrix is a matrix with all elements 0.
12
Square Matrix
A square matrix has the same number of rows and
columns.
The elements ai,i , with i 1,2,3,... are
called diagonal elements.
13
Diagonal matrix
A diagonal matrix is a square matrix with all
non-diagonal elements 0.
14
Unit matrix
A unit matrix is a diagonal matrix with all
diagonal elements 1.
Also called identity matrix - I.
15
Symmetric matrix
A symmetric matrix is a square matrix where aI, J
aJ, I for all elements.
16
Transpose
  • The transpose of the m x n matrix A is shown by
    AT.
  • AT is an n x m matrix,
  • row i of A column i of AT for (i 1,2,3,
    .. n)

17
Transpose (2)
18
Transpose (3)
Matrix A' is the transpose of matrix A if and
only if The ith row of A the ith column of A'
for (i 1,2,3,..n)
Transposing a matrix means converting and m x n
matrix into an n x m matrix, by flipping the
rows and columns.
19
Transpose (4)




4 x 3
3 x 4
20
Transpose (5)



3 x 3
3 x 3
21
Orthogonal matrix
An orthogonal matrix is a square matrix which
produces a unit matrix if it is multiplied by its
own transpose A ? AT I
22
Properties of special matrices
A (AT)T (((AT)T )T )T
(A B)T (AT BT)
If A is a symmetric matrix, then A AT
If A is an orthogonal matrix, then A ? AT I
23
  • Matrix Algebra
  • Scalar multiplication
  • Matrix addition
  • Matrix subtraction
  • Vector addition
  • Vector multiplication
  • Matrix multiplication

24
Scalar multiplication
To multiply a matrix by a number k, multiply each
element of the matrix by the number k.

25
Matrix addition
To add two matrices, add elements of the first
to the corresponding element of the second.
!!! Addition is possible if matrices are of the
same order.
26
Matrix subtraction
A - B To subract a matrix B from A, subtract
elements of B from the corresponding element of A.
!!! Subtraction is possible if matrices are of
the same order.
27
Vector addition
To add two vectors, add the corresponding
elements.
The result is a vector of the same size.
!!! Addition is possible if vectors are of the
same size.
28
Vector multiplication
You can multiply a row vector with a column
vector.
To multiply two vectors, multiply the
corresponding elements, then add the results.
The result is a single number.
29
Vector multiplication (2)
Multiply the corresponding elements, then add the
results
2
?

2 (- 4) 0 2
4
0
!!! Multiplication is possible if vectors are of
the same order.
30
Matrix multiplication
Let A be a k x m matrix and B be
an m x n matrix, Then the product A ? B C is
defined. C is a k x n matrix.
31
Matrix multiplication
AKM ? BMN CKN
  • The number of rows of C is equal to that of A,
    (which is K)
  • The number of columns of C is equal to that of
    B (which is M).

32
Matrix multiplication
!!! AB ? BA (Unless A, B are both NxN)
33
Matrix multiplication (2)
Element ci j is the vector product of row i with
column j.

34
Properties of matrix addition
Addition is associative ABC (AB) C
A (BC)
A 0 A 0 A (0 zero matrix)
Addition is symmetric A B B A
35
Properties of Scalar multiplication
(k, s scalar numbers)
k (AB) kA kB
(ks) A kAsA
(ks) A k (sA)
(kA)T k AT
36
Properties of Matrix multiplication
Multiplication is associative A?B?C (A?B) ?
C A ? (B?C)
Multiplication is distributive A?(B C) A?B
A?C
(A?B)T BT ? AT
A?0 0?A 0 (0 is the zero matrix)
37
  • Determinant and Inverse
  • Minor
  • Cofactor
  • Determinant
  • Inverse

38
Inverse of a number
  • Let n be a scalar (number).
  • The inverse of the number is shown by n-1 1
    / n
  • From algebra n n-1 1
  • If n 0, then n-1 1 / 0
    (which is undefined)

39
Inverse of a matrix
  • Let A be a square matrix.
  • The inverse of matrix A is shown by A-1
  • By definition, AA-1 A-1A I,
    where I is the identity matrix.
  • If ?A? 0, then the inverse A-1 is
    undefined.

40
Determinant of a matrix
?A? is called the determinant of matrix
A. Sometimes written also as det(A) When
computed, ?A? turns into a single
number. We'll see how ?A? can be computed in a
short while.
41
Finding the inverse of a matrix
Only square matrices have inverses.
To find the inverse of A , ... you need to find
?A?.
To find the determinant ?A?, ... you need to
find the cofactors of elements of A.
... you need to find the minors of ?A?
42
Finding minor of element aij
The minor of element aij is the matrix obtained
by deleting row i and column j of A.

Minor(a12)
43
Finding cofactor of element aij
The cofactor of element aij is Cofactor (aij)
Cij (-1)ij ?minor(aij)?
We need to find the cofactor matrix C computing
Cij for each aij . (This is a recursive process)
44
Finding the inverse of A
The inverse of A A-1 1/?A? CT
Note that ?A? is a number ... so is 1 /
?A? .
45
Finding the determinant of a 2x2 matrix
46
Finding the cofactor matrix of a 2x2 matrix
47
Finding the inverse of a 2x2 matrix
48
Finding the determinant of a 3x3 matrix
49
Properties of determinants
A matrix A and its transpose have the same
determinant ?A? ?AT?
The determinant of a diagonal matrix is the
product of the diagonal elements.
When we exchange two columns in A, or, when we
exchange two rows in A, A changes sign.
50
  • Matrix Algebra - Applications
  • Linear equations
  • Gaussian elimination

51
Linear equations
A system of n equations, with n unknowns. a11
x1 a1n xn b1 a21 x1 a2n xn
b2 an1 x1 ann xn bn
52
Linear equations (2)
Can be written as a matrix equation A X B
53
Linear equations (3)
If the determinant A is nonzero, the equation
can be solved to produce n numerical values for x
that satisfy all the simultaneous equations
  • Find A-1 (the inverse of A) (it must exist
    because A is nonzero)
  • Multiply both sides of the equation by A-1
  • A-1A X A-1 B
  • Since A-1 A I
  • X A-1 B

54
Linear equations (3) Example 1
55
Linear equations (3) Example 1
The equation becomes
56
Solving linear equations Gaussian elimination
If we had a single equation, multiplying (or
dividing) both sides with the same number would
not change the equation 2x 12
1/2 (2x) 1/2 (12)
57
Solving linear equations Gaussian elimination (2)
If we had a two equations, multiplying (or
dividing) both sides of one equation with the
same number would not change the equation 4x
2y 0 2x - y -4
Divide the first equation by 2, then add to the
second equation to eliminate y the resulting
system is 2x y 0 2x - y -
4 x -2 y 1
58
Solving linear equations Gaussian elimination (3)
If we had a matrix equation, multiplying (or
dividing) both sides of a row with the same
number would not change the equation


59
Solving linear equations Gaussian elimination (4)
Gaussian elimination is designed to solve a
general set of n equations having n unknowns.
  • Two steps
  • Forward Elimination - multiple of one equation is
    subtracted from another to eliminate unknowns
  • Back Substitution last equation yields the
    unknown, substituting back into the other
    equations yields the rest.

60
Solving linear equations Gaussian elimination (5)
Consider n equations in n unknowns a11 x1
a1n xn b1 a21 x1 a2n xn
b2 an1 x1 ann xn bn
  • The solution vector X will not change the
    following row operations are performed
  • Multiply or divide any equation (row) by a
    constant.
  • Add or subtract any equation to another
    equation.

61
Solving linear equations Gaussian elimination (6)
2x1x24x316 3x12x2x310 x13x23x316
Forward Elimination
2x1 x2 4x3 16 1/2 x2 - 5x3 -
14 x1 3x2 3x3 16
62
Solving linear equations Gaussian elimination (7)
2x1 x2 4x3 16 1/2 x2 - 5x3 -14 x1
3x2 3x3 16
Forward Elimination
2x1 x2 4x3 16 1/2 x2 - 5x3 -
14 5/2 x2 x3 8
63
Solving linear equations Gaussian elimination (8)
2x1 x2 4x3 16 1/2 x2 - 5x3 -14
5/2 x2 x3 8
Forward Elimination
2x1 x2 4x3 16 1/2 x2 - 5x3 -
14 26 x3 78
64
Solving linear equations Gaussian elimination (7)
Back substitution
65
MatricesandVectorsMatrix and Array Operations
66
Matrices and Vectors
  • Matrix
  • MATLAB input command
  • gtgt A 2 5 7 8 5 6 8 3 1 6 4 0

67
Matrices and Vectors
  • Matrix
  • MATLAB input command
  • gtgt B 2x log(x)sin(y) 5x 32i

68
Special cases vectors and scalars
  • A vector is a special case of a matrix, with just
    one row or one column
  • gtgt u 1 3 9 produces a row vector
  • gtgt v 1 3 9 produces a column vector
  • A scalar does not need brackets.
  • gtgt g 9.81
  • Null Matrix
  • gtgt X

69
Continuation
  • The following three commands are equivalent
  • A 1 3 9 5 10 15 0 0 -5
  • A 1 3 9
  • 5 10 15
  • 0 0 -5
  • A 1 3 9 5 10 ...
  • 15 0 0 -5
  • ... usage is not limited to matrix input

70
Indexing (or Subscripting)
  • A 1 2 3 4 5 6 7 8 8
  • A
  • 1 2 3
  • 4 5 6
  • 7 8 8
  • A(2, 3)
  • ans
  • 6
  • A(3, 3) 9
  • A
  • 1 2 3

71
Indexing (or Subscripting)
  • B A(23, 13)
  • B
  • 4 5 6
  • 7 8 9
  • B A(23, )
  • B
  • 4 5 6
  • 7 8 9
  • B(, 2)
  • B
  • 4 6

72
Dimensioning
  • No explicit dimension declarations are required
  • MATLAB creates a matrix just big enough.
  • If the matrices B and C do not exist already
  • B(2, 3) 5 produces

73
Dimensioning
  • C(3, 13) 1 2 3 produces

74
Dimensioning
  • C(3, 13) 1 2 3
  • C
  • 0 0 0
  • 0 0 0
  • 1 2 3
  • size(C)
  • ans
  • 3 3
  • m, n size(C)
  • m
  • 3

75
Matrix Manipulation
  • By specifying vectors as the row and column
    indices of a matrix one can reference and modify
    any submatrix.
  • If A is a 10 X 10 matrix, B is a 5 X 10 matrix,
    and y is a 20 elements long row vector, then
  • A(1 3 6 9, ) B(13, ) y(1 10)
  • replaces 1st, 3rd, and 6th rows of A by the
    first 3 rows of B, and the 9th row of A by the
    first 10 elements of y.

76
Matrix Manipulation
  • Reshaping matrices
  • As a vector All the elements of a matrix A can
    be strung into a single column vector b by the
    command b A() (A is stacked in vector B).
  • As a differently sized matrix If A is m x n, it
    can be reshaped into a p x q matrix as long as
  • m x n p x q
  • Thus, for a 6 x 6 matrix A,
  • gtgtreshape(A, 9, 4) transforms A into a 9 x 4
    matrix,
  • gtgtreshape(A, 3, 12) transforms A into a 3 x 12
    matrix

77
Matrix Manipulation
  • Transpose of matrix A
  • Obtained by typing A
  • A2 3 6 7
  • A
  • 2 3
  • 6 7
  • B A'
  • B
  • 2 6
  • 3 7

78
Matrix Manipulation
  • Transpose example
  • u 0 1 2 3 4 5 6 7 8 9
  • v u(36)'
  • v
  • 2
  • 3
  • 4
  • 5

79
Initialization
  • Not necessary but advisable for
  • Large matrices (To reserve a continuous block in
    memory for efficient processing).
  • gtgt A zeros(m, n) initializes m x n matrix
  • Dynamix matrices If the rows or columns of a
    matrix is computed in a loop you may initialize
    the matrix to a null mutrix (A ) and append
    rows or columns later

80
Appending a row or column
  • A 1 0 0 0 1 0 0 0 1
  • A
  • 1 0 0
  • 0 1 0
  • 0 0 1
  • u 5 6 7
  • u
  • 5 6 7
  • A A u
  • A
  • 1 0 0

81
Appending a row or column
  • A 1 0 0 0 1 0 0 0 1
  • A
  • 1 0 0
  • 0 1 0
  • 0 0 1
  • v 2 3 4
  • v
  • 2
  • 3
  • 4
  • A A v
  • A

82
Appending a row or column
  • A 1 0 0 0 1 0 0 0 1
  • A
  • 1 0 0
  • 0 1 0
  • 0 0 1
  • u 5 6 7
  • u
  • 5 6 7
  • A A u'
  • A
  • 1 0 0 5

83
Appending a row or column
  • A 1 0 0 0 1 0 0 0 1
  • A
  • 1 0 0
  • 0 1 0
  • 0 0 1
  • u 5 6 7
  • u
  • 5 6 7
  • A A u
  • ??? All matrices on a row in the bracketed
    expression must have the
  • same number of rows.

84
Appending a row or column
  • B B B 1 2 3
  • B
  • 1 2 3
  • B for k13, BB k k1 k2 end
  • B
  • B
  • 1 2 3
  • 2 3 4
  • 3 4 5

85
Deleting a row or column
  • Any row(s) or column(s) of a matrix can be de-
    leted by setting the row or column to null vector
  • A(2, ) deletes the 2nd row of matrix A
  • A(, 35) deletes the 3rd through 5th
    columns
  • A(1 3, ) deletes the 1st and the 3rd row
    of A
  • U(5length(u)) deletes all elements of
    vector U except 1 through 4

86
Utility matrices
  • eye(m, n) returns an m x n matrix with
  • 1s on the main diagonal
  • zeros(m, n) returns an m x n matrix of zeros
  • ones(m, n) returns an m x n matrix of ones
  • rand(m, n) returns an m x n matrix of
  • random numbers
  • Above commands with a single argument, e.g.
    ones(m), produce square matrices of dimension m

87
Utility matrices
  • diag(v) generates a diagonal matrix with
  • vector v on the diagonal
  • diag(A) extracts the diagonal of matrix A
  • as a vector
  • diag(A, 1) extracts the first upper offdiagonal
  • vector of matrix A
  • A matrix can be built with many block matrices as
    well.

88
Utility matrices
  • eye(3)
  • ans
  • 1 0 0
  • 0 1 0
  • 0 0 1
  • B ones(3) zeros(3, 2) zeros(2, 3) 4eye(2)
  • B
  • 1 1 1 0 0
  • 1 1 1 0 0
  • 1 1 1 0 0
  • 0 0 0 4 0
  • 0 0 0 0 4
  • diag(B)'

89
Utility Matrices
  • d 2 4 6 8
  • d1 -3 -3 -3
  • d2 -1 -1
  • D diag(d) diag(d1, 1) diag(d2, -2)
  • D
  • 2 -3 0 0
  • 0 4 -3 0
  • -1 0 6 -3
  • 0 -1 0 8

90
Creating Vectors
  • v InitialValueIncrementFinalValue
  • (If no increment is specified, increment
  • is 1)
  • a 010100 produces a 0 10 20 ... 100,
  • u 210 produces a 2 3 4 ... 10
  • No square brackets are required above.
  • However, to force concatenation you need them
  • u 110 33-219

91
Creating Vectors
  • linspace(a, b, n) generates a linearly spaced
    vector of length n from a to b.
  • logspace(a, b, n) generates a logarithmically
    spaced vector of length n from 10a to 10b.
  • u zeros(1, 1000) initializes a 1000 element
    long row vector
  • v ones(10, 1) creates a 10 element long column
    vector of 1s.
  • u linspace(0, 20, 5)
  • u
  • 0 5 10 15 20
  • v logspace(0, 3, 4)
  • v
  • 1 10 100 1000

92
Matrix and Array Operations
  • AB or A-B is valid if A and B are of the
  • same size
  • AB is valid if As number of columns
  • equals Bs number of rows
  • A/B is valid, and equals A.B-1 for same
  • size square matrices A B
  • A2 makes sense only if A is square
  • and equals AA

93
Matrix and Array Operations
  • In all the previous comands if B is replaced by a
    scalar, say w, the arithmetic operations are
    still carried out. Aw adds w to each element of
    A, wA (or Aw) multiplies each element of by w
    and so on.
  • The right division This division is used to
    solve a matrix equation. In particular,
  • X A\b solves the matrix equation A x b.

94
Matrix and Array Operations
  • Element-by-element multiplication, division and
    exponentiation between two matrices or vectors of
    the same size are done by preceding the
    corresponding arithmetic operators by a period
    (.)
  • . element-by-element multiplication
  • ./ element-by-element left-division
  • .\ element-by-element right-division
  • . element-by-element exponentiation
  • . nonconjugated transpose

95
Matrix and Array Operations
  • A1 2 3 4 5 6 7 8 9
  • x A(1, )'
  • x
  • 1
  • 2
  • 3
  • x'x
  • ans
  • 14
  • xx'
  • ans

96
Matrix and Array Operations
  • v 1 2 3 4 5 6
  • v
  • 1 2 3 4 5 6
  • 1./v
  • ans
  • 1.0000 0.5000 0.3333 0.2500
    0.2000 0.1667

97
Matrix and Array Operations
  • There is a big difference between A2 and A.2
  • A 1 2 3 4 5 6 7 8 9
  • A
  • 1 2 3
  • 4 5 6
  • 7 8 9
  • A2
  • ans
  • 30 36 42
  • 66 81 96
  • 102 126 150
  • A.2

98
Matrix and Array Operations
  • Relational Operations
  • lt less than
  • lt less than or equal
  • gt greater than
  • gt greater than or equal
  • equal
  • not equal
  • These operations result in a vector or matrix of
    the same size as the operands, with 1 where the
    relation is true and 0 where false.

99
Matrix and Array Operations
  • Relational operations
  • x 1 5 3 7
  • y 0 2 8 7
  • k x lt y
  • k
  • 0 0 1 1
  • k x gt y
  • k
  • 1 1 0 1

100
Matrix and Array Operations
  • Logical operations
  • logical AND
  • logical OR
  • logical complement (NOT)
  • xor exclusive OR

101
Matrix and Array Operations
  • x 0 5 3 7
  • y 0 2 8 7
  • m (xgty)(xgt4)
  • m
  • 0 1 0 0
  • n xy
  • n
  • 0 1 1 1
  • m (xy)
  • m
  • 1 0 0 0

102
Matrix and Array Operations
  • Built-in logical functions
  • all true (1) if all elements of a vector are
    true
  • any true (1) if any element of a vector is
    true
  • exist true (1) if the argument (a variable or
  • function) exists
  • isempty true(1) for an empty matrix
  • isinf true for all infinite elements of a matrix
  • isfinite true for all finite elements of a matrix
  • isnan true for all elements of a matrix that are
  • Not-A-Number
  • find finds indices of non-zero elements of a
    matrix

103
Matrix and Array Operations
  • x 7 -2 9 -1 -3 10
  • all(xlt0)
  • ans
  • 0
  • A 1 inf 2 3
  • isfinite(A)
  • ans
  • 1 0
  • 1 1
  • x 0 2 5 7
  • find(x)
  • ans

104
Matrix and Array Operations
  • Matrix functions
  • expm(A) finds the exponential of matrix A, eA
  • logm(A) finds log(A) such that A e log(A)
  • sqrtm(A) finds A
  • Array Counterparts that operate on each element
    of the input matrix
  • exp
  • log
  • sqrt

105
Matrix and Array Operations
  • A 1 2 3 4
  • asqrt sqrt(A)
  • asqrt
  • 1.0000 1.4142
  • 1.7321 2.0000
  • Asqrt sqrtm(A) Thus AsqrtAsqrtA
  • Asqrt
  • 0.5537 0.4644i 0.8070- 0.2124i
  • 1.2104- 0.3186i 1.7641 0.1458i
  • exp_aij exp(A)
  • exp_aij

106
Character strings
  • Character Strings
  • gtgt message Leave me alone
  • creates a vector of size 1 x 14
  • To create a column vector of text objects, each
    text string must have exactly the same number of
    characters
  • gtgt names John Ravi Mary Xiao
  • Thus
  • gtgt A Ali Veli
  • generates an error

107
Matrix and Array Operations
  • Character Strings
  • char command pads the necessary spaces
  • gtgt howdy char(Hi, Hello, Namaste)
  • is valid.
  • Character strings can be manipulated just like
    matrices.
  • gtgt c howdy(2,) names(3,) produces
  • Hello Mary as the output in variable c.

108
Matrix and Array Operations
  • Charater Strings
  • Check page 62 of youe book for a number of
    character functions.
  • eval function evaluates text strings and
    execute them if they contain valid commands
  • eval(x 5sin(pi/3)) is equivalent to
  • x 5sin(pi/3)
  • You make eval useful by using variables in the
    string.
Write a Comment
User Comments (0)
About PowerShow.com