matrices and matlab

81
Matrices and MATLAB Dr Viktor Fedun Automatic Control and Systems Engineering, C09 Based on lectures by Dr Anthony Rossiter

Upload: huong

Post on 23-Mar-2016

94 views

Category:

Documents


5 download

DESCRIPTION

Matrices and MATLAB. Dr Viktor Fedun Automatic Control and Systems Engineering, C09 Based on lectures by Dr Anthony Rossiter. Examples of a matrix. Examples of a matrix. Examples of a matrix. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Matrices and MATLAB

Matrices and MATLAB

Dr Viktor FedunAutomatic Control and Systems Engineering, C09

Based on lectures by Dr Anthony Rossiter

Page 2: Matrices and MATLAB

Examples of a matrix

Page 3: Matrices and MATLAB

Examples of a matrix

Page 4: Matrices and MATLAB

Examples of a matrix

A matrix can be thought of simply as a table of numbers with a given number of rows and columns.

Example A has 3 rows and 3 columns, example B has 4 rows and 4 columns.

212354868752546432

;413965302

BAExamples of

square matrices

Page 5: Matrices and MATLAB

Examples of a matrix

A matrix can be thought of simply as a table of numbers with a given number of rows and columns.

Example A has 2 rows and 3 columns, example B has 3 rows and 1 column.

765

;501432

BAExamples of non-square

matrices

Page 6: Matrices and MATLAB

What use are matrices?• Compact form for storing data (equivalent to

a table but more flexible).• Each column(or row) can relate to a different

‘state’: e.g. Time, displacement, velocity, temperature, etc.

• Convenient for computer code (and algebra) as can store and share large quantities of related data with a single variable.

• Easily extend to dynamic relations/modelling.

Page 7: Matrices and MATLAB

Indexing of matrices

Always give as {row index,column index}Start counting from top left, so the top left

component is index {1,1}, e.g. indices are given as:

}4,4{}3,4{}2,4{}1,4{}4,3{}3,3{}2,3{}1,3{}4,2{}3,2{}2,2{}1,2{}4,1{}3,1{}2,1{}1,1{

Row index increasing

Column index

increasing

Page 8: Matrices and MATLAB

What is the index of the component -4

212354868752546432

B

1. {1,3}2. {1,2}3. {2,1}4. {3,1}5. {4,3}

Page 9: Matrices and MATLAB

What is the index of the component -4

212354868752546432

B

1. {1,3}2. {1,2}3. {2,1}4. {3,1}5. {4,3}

}4,4{}3,4{}2,4{}1,4{}4,3{}3,3{}2,3{}1,3{}4,2{}3,2{}2,2{}1,2{}4,1{}3,1{}2,1{}1,1{

Page 10: Matrices and MATLAB

Create a matrix with following information

2nd row and 3rd column is 54th row and 2nd column is 21st row and 5th column is 6Remainder are 0.

TRY this on MATLAB

A(2,3)=5; A(4,2)=2; A(1,5)=6;disp(A)

Note MATLAB syntax

matches that used in

mathematics, i.e. row index

and then column index.

Page 11: Matrices and MATLAB

Extracting parts of matrices

Example of selecting given rows and columns.

Can rearrange the order if desired.

Page 12: Matrices and MATLAB

Matrices

The main thing to note is that the default variable in MATLAB is a matrix (or vector if the row or column dimension is one).

Any BODMAS type operation that is valid with matrices can be carried out with the same syntax.

MATLAB also includes a large number of matrix analysis tools that you will find useful in the 2nd year.

Page 13: Matrices and MATLAB

Matrices and MATLAB for plotsA student collects data from an experiment

and stores the answer in a matrix data_exp1.• Column 1 corresponds to time.• Column 2 to temperature• Column 3 is voltage supplied.It is now simple to code and plot this data – the

corresponding code is shown next.

Page 14: Matrices and MATLAB

0 20 40 60 80 100 1200

5

10

15

20

25

30

Time (sec)

TemperatureVoltage

MATLAB example

Page 15: Matrices and MATLAB

Correcting dataStudent is told that the temperature readings

were all out by 2 degrees due to a calibration error.

Once the data is in a matrix, update is very efficient.

Use line: data_exp1(:,2)=data_exp1(:,2)+2This will work not matter how many items of

data are affected.

Page 16: Matrices and MATLAB

Matrix notationWithin a textbook, it is more normal to use

subscripts to represent the indices, so for example:

4,43,42,41,4

4,33,32,31,3

4,23,22,21,2

4,13,12,11,1

AAAAAAAAAAAAAAAA

A

Often the comma is omitted for low value integers

131211 BBBB

Page 17: Matrices and MATLAB

Matrix notation

Page 18: Matrices and MATLAB

Matrix notation

Page 19: Matrices and MATLAB

Matrix notation

Page 20: Matrices and MATLAB

Matrix notation

Page 21: Matrices and MATLAB

Matrix notation

Page 22: Matrices and MATLAB

Flexibility

As an engineer you need to be flexible and work out what notation a particular article, website, book, etc is using.

Usually it will be obvious from the context and usage as long as your understand the core principles.

Page 23: Matrices and MATLAB

Which statement is false?

042056238

D

1. D12=-32. D21=63. D32=24. D23=25. D33=0

Page 24: Matrices and MATLAB

Matrix dimensionsWhat are the dimensions of the following

matrices?

354

042154233

;042056238

ED

765

;501432

BA

0404512 C

Page 25: Matrices and MATLAB

Matrix dimensions summary

• Dimensions are always number of rows first and then number of columns second.Typical notation is 2 by 3 or 4 by 1 or similar. Describe dimensions of matrices on previous slide.

• Indexing begins from top left corner.

Page 26: Matrices and MATLAB

MATRIX TRANSPOSE

This is a very common operation so it is critical that students are familiar with the notation.There is nothing to understand as it is simply a definition.

Page 27: Matrices and MATLAB

MATRIX TRANSPOSE

This is a very common operation so it is critical that students are familiar with the notation.There is nothing to understand as it is simply a definition.

Page 28: Matrices and MATLAB

Matrix transpose definitionThe transpose is determined by swapping

the position of all the indices, that is {1,2} becomes {2,1}, etc.

You could view this a bit like doing a reflection about the principle diagonal.

Diagonal elements do not change.

;4332

;4332

TAA

Tijji AA ,,

Page 29: Matrices and MATLAB

Examples of Matrix transposeTijji AA ,,

443362

;44

3632

TAA

0404521

;0404512 TCC

Alternative insight.The jth column of A is the jth

row of A transpose.

Page 30: Matrices and MATLAB

Matrix transpose examplesFind the transpose of the following:

354

042154233

042056238

E

D

765

B

Page 31: Matrices and MATLAB

Which is the correct transpose of G?

1. A2. B3. C4. D5. None of these

22

22

22

;22

22

22

22

22

22

;22

22

22

;22

22

22

DC

BAG

Page 32: Matrices and MATLAB

ADDITION AND SUBTRACTION OF MATRICES

Page 33: Matrices and MATLAB

Addition and subtraction of matrices

Add (or subtract) components in same position, that is with the same {row, column} index.

Matrices must be same dimensions or you cannot add them!

2453

;6721

;4332

BABA

jijiji BACBAC ,,,

Page 34: Matrices and MATLAB

Do following matrices computations

BABA ;6724

;4332

BABA 2;

76

812

02;

44

3632

Page 35: Matrices and MATLAB

Which is incorrect (could be several)?

94

82

20

;9143

12

20

22

;5231

;82

62

02

;4312

EF

DC

AG

1. G+C=F2. G-C=-F3. A+D=E4. D-A=-E5. None of these

Page 36: Matrices and MATLAB

Do following matrix computations

BABA T 3;6724

;4332

TBABA ;

46

01

02;

05

3132

Page 37: Matrices and MATLAB

MATRIX handling in MATLAB

• Matrix addition/subtraction

• Matrix multiplication• Matrix powers• Matrix inversion• Extracting parts of

matrices

• A+B, A-B, A-2*B+C• A*B• A^3 • inv(A) or A^(-1)

• A([1,3],[2, 4])

Page 38: Matrices and MATLAB

MATRIX MULTIPLICATION

This is the most common skill you will need.The technique is simply a definition that you need to memorise.

There is no such thing as matrix division!

Page 39: Matrices and MATLAB

Basic rule for multiplication (BY DEFINITION – not to understand)

Index {i,j} of result is determined from row {i} of left hand matrix and column {j} of right hand matrix.

Multiplying a row on left by a column on the right is a ‘dot-product’ type of operation, e.g.

dhcgbfae

hgfe

dcba

Page 40: Matrices and MATLAB

What is the result of the following ?

862

533

1. 882. 523. 32

Page 41: Matrices and MATLAB

What is the result of the following ?

20432

72113

1. 182. 213. 274. Don’t know

Page 42: Matrices and MATLAB

What is the result of the following ?

301

21

1. 12. 63. NA4. Not sure

Page 43: Matrices and MATLAB

KEY OBSERVATIONA row vector can only be multiplied onto a

column vector if the two vectors are the same length.

Number of columns of left hand vector must match the number of rows of the right hand vector.

If vectors have different dimensions, multiplication is not defined.

Page 44: Matrices and MATLAB

Rules for matrix multiplicationAssume we wish to do C=A*B.A must have same number of columns as B has rows

C BARows of C

Columns of B

Rows of B

Columns of C

Columns of A

Rows of A

=

Page 45: Matrices and MATLAB

Basic rule for multiplication Index {i,j} or result is determined from row {i} of left hand matrix and column {j} of right hand matrix. Multiplying a row on left by a column on the right is a ‘dot-product’ type of operation, e.g.

phmgnfkedhcgbfae

hgfe

pd

mc

nb

ka

fpejdpcjbpaj

fmeidmcibmai

fnehdnchbnah

fkegdkcgbkag

pj

mi

nh

kg

fdb

eca

Page 46: Matrices and MATLAB

Student problemSolve the following multiplications.

21226110541971

86

65

42

265471

86

65

42

Page 47: Matrices and MATLAB

Student problem 2 – compute C,D

BADABCBA

;;

265471

;6846

65

42

Page 48: Matrices and MATLAB

KEY OBSERVATIONTwo matrices can only be multiplied if the

column dimension of the left matrix matches the row dimension of the right matrix.

In general

In fact, it is possible that AB exists but BA does not!

BAAB The property of

AB=BA is called commutativity and does not

hold for matrices in general.

Page 49: Matrices and MATLAB

Uses of matrix multiplicationA simple example involves the change of coordinates from one set to another – this is commonly needed for robotics and mechanics.

x

y

X’Y’

θ

V=(x,y)

cossin'sincos'yxYyxX

yx

YX

cossinsincos

''

Page 50: Matrices and MATLAB

SIMULTANEOUS EQUATIONS

Matrix/vector algebra is a compact and efficient method for handling linear simultaneous equations and most effective techniques deployed assume matrix representation of this problem. [Details in semester 2]MATLAB automatically handles simultaneous equations with matrices and vectors which greatly simplifies solution and manipulation. [Try this now!]

Page 51: Matrices and MATLAB

Using Matrices for simultaneous equations

Consider a single linear equation and write in MATRIX/VECTOR format, e.g.

6]61[65

4]23[423

yx

yx

yx

yx

64

5123

523

yx

yxyx

Page 52: Matrices and MATLAB

Put following into matrix-vector format

104132046332

23165

zyxyzyzx

yxyx

91412214432

12

dabdcadcbcba

Page 53: Matrices and MATLAB

Simultaneous equationsAssume user knows how to put simultaneous

equations into matrix vector format, ie.AX=b

Can solve with MATLAB several different ways;1. X=inv(A)*b2. X=A\b3. Numerically efficient code is discussed in text

books and semester 2 of ACS123.

Try on MATLAB

820241012

;231

;1 AbbAx

Page 54: Matrices and MATLAB

DETERMINANTS

You will cover solution of simultaneous equations in semester 2. However, in order to prepare for this, you will need familiarity and skill with the determinant of a matrix.Final matrix topic here is define ‘determinant’. NOTE: This is a definition and therefore must be learnt as a fact.

ONLY DEFINED FOR SQUARE MATRICES

Page 55: Matrices and MATLAB

Determinant in briefIn conceptual terms, a determinant gives an indication of the ‘magnitude’ of a matrix, that is, on average, what scale of impact does it have on vectors when multiplying those vectors.You will notice that the effective magnification is direction dependent, and so the determinant is an average in some sense.

;11

11

3223

;55

11

3223

Scaled by factor of 5. Scaled by factor of 1.

Page 56: Matrices and MATLAB

Determinant – key factIf the determinant is very small, then there exists a least one direction, which when multiplied by the matrix, results in a very small scaling factor.

A zero determinant means at least one direction can be mapped to zero.

;01.0

9.01.0

218110

;2011

11

218110

Scaled by factor of 16 (approx).

Scaled by factor of 0.1 (approx).

Page 57: Matrices and MATLAB

Determinant for 2 by 2 matricesThe determinant is defined as product of the diagonal elements minus the product of the off-diagonal elements:

Note use of vertical lines around matrix is notation use to define determinant.Try this on the examples of the previous few slides.

bcadAdcba

A

;

Page 58: Matrices and MATLAB

Determinant computations

AA ;

3223

BB ;

218110

Page 59: Matrices and MATLAB

Find the determinant of matrix D

8541

D

1. -122. 283. 124. -285. unsure

Page 60: Matrices and MATLAB

Find the determinant of matrix F

12431

F

1. 02. 243. -244. -325. unsure

Page 61: Matrices and MATLAB

Determinant for 3 by 3 matricesThe determinant is defined using the concept of a matrix of cofactors Aij. For convenience define the original matrix with lower case and the matrix of cofactors with upper case.

Next, by definition:

333231

232221

131211

333231

232221

131211

)(;AAAAAAAAA

Acofaaaaaaaaa

A

333332323131

232322222121

131312121111

AaAaAaorAaAaAaor

AaAaAaA

Page 62: Matrices and MATLAB

Defining the cofactor termsA cofactor is taken as the determinant of a minor with a sign change if appropriate to the position. A minor is the matrix remaining when the row and column associated to an element are crossed out.

3231

222113

333231

232221

131211

)(min;aaaa

aoraaaaaaaaa

A

;

sign 312232211313 )( aaaaacofA

Page 63: Matrices and MATLAB

Defining the cofactor termsA cofactor is taken as the determinant of a minor with a sign change if appropriate to the position. A minor is the matrix remaining when the row and column associated to an element are crossed out.

3332

131221

333231

232221

131211

)(min;aaaa

aoraaaaaaaaa

A

;

sign ][)( 133233122121 aaaaacofA

Page 64: Matrices and MATLAB

Find the matrix of cofactors and determinant

210018563

A131312121111 AaAaAaA

Page 65: Matrices and MATLAB

Find the matrix of cofactors and determinant

512212430

A

Page 66: Matrices and MATLAB

Determinant for 4 by 4 matricesThe determinant is defined using the concept of a matrix of cofactors Aij (as for the 3 by 3 case)For convenience define the original matrix with lower case and the matrix of cofactors with upper case. Next, by definition:

4

1

1414131312121111

jijijAaor

AaAaAaAaA

The extension to larger matrices should be obvious, although this is not a paper and pen exercise.

Page 67: Matrices and MATLAB

Defining the cofactor terms for a 4 by 4A cofactor is taken as the determinant of a minor with a sign change if appropriate to the position. A minor is the matrix remaining when the row and column associated to an element are crossed out.

4,43,42,41,4

4,33,32,31,3

4,23,22,21,2

4,13,12,11,1

aaaaaaaaaaaaaaaa

A

Page 68: Matrices and MATLAB

Remarks on 4 by 4 determinantsA minor is now a 3 by 3 matrix and therefore

each cofactor requires the computation of the determinant of a 3 by 3

For a 5 by 5 matrix, the minors would each require the computation of a 4 by 4 determinant.

Determinants of larger matrices are best done using properties and tricks to minimise the numeric

computations. Alternatively, and more likely, use a computer.

Page 69: Matrices and MATLAB

Properties of determinants1. Adding a multiple of any row to another row does not

change the determinant.2. If two rows (or two columns) are equal then the

determinant is zero.3. Scaling any row by λ results in a scaling of the

determinant by λ4. Interchanging rows (or columns) changes the sign of the

determinant.5. A lower or upper triangular matrix has a determinant

given as product of diagonal terms.6. The determinant of a matrix and determinant of its

transpose are equal

Page 70: Matrices and MATLAB

Properties of determinants1. Adding a multiple of any row to another row does not change the

determinant.

Page 71: Matrices and MATLAB

Properties of determinants2. If two rows (or two columns) are equal then the determinant is zero.

Page 72: Matrices and MATLAB

Properties of determinants3. Scaling any row by λ results in a scaling of the determinant by λ.

Page 73: Matrices and MATLAB

Properties of determinants

4. Interchanging rows (or columns) changes the sign of the determinant.

Page 74: Matrices and MATLAB

Properties of determinants5. A lower or upper triangular matrix has a determinant given as product of diagonal terms.

Page 75: Matrices and MATLAB

Properties of determinants6. The determinant of a matrix and determinant of its transpose are equal

Page 76: Matrices and MATLAB

Find the determinant (using properties)

200010563

A

Check answers using MATLAB – e.g det(A)

Page 77: Matrices and MATLAB

Find the determinant (using properties)

2135610403185002220001

B

Check answers using MATLAB – e.g det(A)

Page 78: Matrices and MATLAB

Find the determinant (using properties)

241012561

A

Page 79: Matrices and MATLAB

Find the determinant (using properties)

21356104731850151230541

B

Page 80: Matrices and MATLAB

Find the determinant (using properties)

004016561

A

213561040005051230021

B

Page 81: Matrices and MATLAB

Adjoint

The adjoint matrix is the transpose of the matrix of cofactors.

This will be used in semester 2 for solving linear simultaneous equations.

All the cofactors need to be computed! THIS IS TEDIOUS!

However: The adjoint is not needed if only the determinant is required.