1 / 81

Matrices and MATLAB

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.

huong
Télécharger la présentation

Matrices and MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Examples of a matrix

  3. Examples of a matrix

  4. 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. Examples of square matrices

  5. 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. Examples of non-square matrices

  6. 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.

  7. 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: Column index increasing Row index increasing

  8. What is the index of the component -4 • {1,3} • {1,2} • {2,1} • {3,1} • {4,3}

  9. What is the index of the component -4 • {1,3} • {1,2} • {2,1} • {3,1} • {4,3}

  10. Create a matrix with following information 2nd row and 3rd column is 5 4th row and 2nd column is 2 1st row and 5th column is 6 Remainder are 0. Note MATLAB syntax matches that used in mathematics, i.e. row index and then column index. TRY this on MATLAB A(2,3)=5; A(4,2)=2; A(1,5)=6; disp(A)

  11. Extracting parts of matrices Example of selecting given rows and columns. Can rearrange the order if desired.

  12. 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.

  13. Matrices and MATLAB for plots A 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.

  14. MATLAB example

  15. Correcting data Student 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)+2 This will work not matter how many items of data are affected.

  16. Matrix notation Within a textbook, it is more normal to use subscripts to represent the indices, so for example: Often the comma is omitted for low value integers

  17. Matrix notation

  18. Matrix notation

  19. Matrix notation

  20. Matrix notation

  21. Matrix notation

  22. 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.

  23. Which statement is false? • D12=-3 • D21=6 • D32=2 • D23=2 • D33=0

  24. Matrix dimensions What are the dimensions of the following matrices?

  25. 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.

  26. 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. Matrix transpose

  27. 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. Matrix transpose

  28. Matrix transpose definition The 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.

  29. Examples of Matrix transpose Alternative insight. The jth column of A is the jth row of A transpose.

  30. Matrix transpose examples Find the transpose of the following:

  31. Which is the correct transpose of G? • A • B • C • D • None of these

  32. Addition and subtraction of matrices

  33. 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!

  34. Do following matrices computations

  35. Which is incorrect (could be several)? • G+C=F • G-C=-F • A+D=E • D-A=-E • None of these

  36. Do following matrix computations

  37. 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])

  38. 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! Matrix multiplication

  39. 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.

  40. What is the result of the following ? • 88 • 52 • 32

  41. What is the result of the following ? • 18 • 21 • 27 • Don’t know

  42. What is the result of the following ? • 1 • 6 • NA • Not sure

  43. KEY OBSERVATION A 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.

  44. Rules for matrix multiplication Assume we wish to do C=A*B. A must have same number of columns as B has rows Columns of A Columns of C Columns of B B Rows of C C A Rows of B = Rows of A

  45. 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.

  46. Student problem Solve the following multiplications.

  47. Student problem 2 – compute C,D

  48. KEY OBSERVATION Two 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! The property of AB=BA is called commutativity and does not hold for matrices in general.

  49. Uses of matrix multiplication A simple example involves the change of coordinates from one set to another – this is commonly needed for robotics and mechanics. V=(x,y) y Y’ X’ θ x

  50. 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!] Simultaneous equations

More Related