Matrices and MATLAB Dr Viktor Fedun

Slides:



Advertisements
Similar presentations
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
Advertisements

Rules of Matrix Arithmetic
CS 450: COMPUTER GRAPHICS LINEAR ALGEBRA REVIEW SPRING 2015 DR. MICHAEL J. REALE.
Elementary Linear Algebra Anton & Rorres, 9th Edition
Applied Informatics Štefan BEREŽNÝ
Further Pure 1 Matrices Introduction. Definitions A matrix is just a rectangle of numbers. It’s a bit like a two-way table. You meet this concept in D1.
Linear Algebra Applications in Matlab ME 303. Special Characters and Matlab Functions.
Matrix Algebra Matrix algebra is a means of expressing large numbers of calculations made upon ordered sets of numbers. Often referred to as Linear Algebra.
Matrix Algebra Matrix algebra is a means of expressing large numbers of calculations made upon ordered sets of numbers. Often referred to as Linear Algebra.
SOLVING SYSTEMS OF LINEAR EQUATIONS. Overview A matrix consists of a rectangular array of elements represented by a single symbol (example: [A]). An individual.
Chapter 2 Matrices Finite Mathematics & Its Applications, 11/e by Goldstein/Schneider/Siegel Copyright © 2014 Pearson Education, Inc.
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Chapter 9 Gauss Elimination The Islamic University of Gaza
Linear Algebraic Equations
Review of Matrix Algebra
GG313 Lecture 12 Matrix Operations Sept 29, 2005.
Economics 2301 Matrices Lecture 13.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Matrix Mathematics in MATLAB and Excel
Matrices and Determinants
Copyright © Cengage Learning. All rights reserved. 7.6 The Inverse of a Square Matrix.
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
1 Chapter 2 Matrices Matrices provide an orderly way of arranging values or functions to enhance the analysis of systems in a systematic manner. Their.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Recall that a square matrix is one in which there are the same amount of rows as columns. A square matrix must exist in order to evaluate a determinant.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Chapter 10 Review: Matrix Algebra
Compiled By Raj G. Tiwari
Spring 2013 Solving a System of Linear Equations Matrix inverse Simultaneous equations Cramer’s rule Second-order Conditions Lecture 7.
Sundermeyer MAR 550 Spring Laboratory in Oceanography: Data and Methods MAR550, Spring 2013 Miles A. Sundermeyer Linear Algebra & Calculus Review.
Array Math.
Chapter 4.1 Mathematical Concepts
Rev.S08 MAC 1140 Module 10 System of Equations and Inequalities II.
Chapter 6.7 Determinants. In this chapter all matrices are square; for example: 1x1 (what is a 1x1 matrix, in fact?), 2x2, 3x3 Our goal is to introduce.
Some matrix stuff.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Chapter 4 Matrices By: Matt Raimondi.
Statistics and Linear Algebra (the real thing). Vector A vector is a rectangular arrangement of number in several rows and one column. A vector is denoted.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Matrices Addition & Subtraction Scalar Multiplication & Multiplication Determinants Inverses Solving Systems – 2x2 & 3x3 Cramer’s Rule.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 5 Systems and Matrices Copyright © 2013, 2009, 2005 Pearson Education, Inc.
1 C ollege A lgebra Systems and Matrices (Chapter5) 1.
Unit 3: Matrices.
Multivariate Statistics Matrix Algebra I W. M. van der Veld University of Amsterdam.
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Elementary Linear Algebra Anton & Rorres, 9 th Edition Lecture Set – 02 Chapter 2: Determinants.
Chapter 8 Matrices and Determinants Matrix Solutions to Linear Systems.
Chapter 9 Gauss Elimination The Islamic University of Gaza
TH EDITION LIAL HORNSBY SCHNEIDER COLLEGE ALGEBRA.
LEARNING OUTCOMES At the end of this topic, student should be able to :  D efination of matrix  Identify the different types of matrices such as rectangular,
Unit 3: Matrices. Matrix: A rectangular arrangement of data into rows and columns, identified by capital letters. Matrix Dimensions: Number of rows, m,
Computer Graphics Mathematical Fundamentals Lecture 10 Taqdees A. Siddiqi
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 3 - Chapter 8 Linear Algebraic Equations and Matrices.
Matrices Introduction.
MTH108 Business Math I Lecture 20.
MAT 322: LINEAR ALGEBRA.
Linear Algebra Review.
ECE 3301 General Electrical Engineering
Chapter 7 Matrix Mathematics
Matrix Algebra - Overview
Chapter 2 Determinants Basil Hamed
Lecture 11 Matrices and Linear Algebra with MATLAB
Unit 3: Matrices
Dr Huw Owens Room B44 Sackville Street Building Telephone Number 65891
Matrices and Determinants
Presentation transcript:

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 square matrices 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

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

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.

Column index increasing 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

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

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

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)

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

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.

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.

MATLAB example

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.

Often the comma is omitted for low value integers 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

Matrix notation

Matrix notation

Matrix notation

Matrix notation

Matrix notation

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.

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

Matrix dimensions What are the dimensions of the following matrices?

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.

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.

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.

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.

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

Matrix transpose examples Find the transpose of the following:

Which is the correct transpose of G? B C D None of these

Addition and subtraction of matrices

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!

Do following matrices computations

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

Do following matrix computations

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

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!

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.

What is the result of the following ? 88 52 32

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

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

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.

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

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.

Student problem Solve the following multiplications.

Student problem 2 – compute C,D

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.

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

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!]

Using Matrices for simultaneous equations Consider a single linear equation and write in MATRIX/VECTOR format, e.g.

Put following into matrix-vector format

Simultaneous equations Assume user knows how to put simultaneous equations into matrix vector format, ie. AX=b Can solve with MATLAB several different ways; X=inv(A)*b X=A\b Numerically efficient code is discussed in text books and semester 2 of ACS123. Try on MATLAB

determinants ONLY DEFINED FOR SQUARE MATRICES 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

Determinant in brief In 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. Scaled by factor of 5. Scaled by factor of 1.

Determinant – key fact If 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. Scaled by factor of 16 (approx). Scaled by factor of 0.1 (approx).

Determinant for 2 by 2 matrices The 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.

Determinant computations

Find the determinant of matrix D -12 28 12 -28 unsure

Find the determinant of matrix F 24 -24 -32 unsure

Determinant for 3 by 3 matrices The 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:

Defining the cofactor terms A 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.

Defining the cofactor terms A 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.

Find the matrix of cofactors and determinant

Find the matrix of cofactors and determinant

Determinant for 4 by 4 matrices The 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: The extension to larger matrices should be obvious, although this is not a paper and pen exercise.

Defining the cofactor terms for a 4 by 4 A 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.

Remarks on 4 by 4 determinants A 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.

Properties of determinants Adding a multiple of any row to another row does not change the determinant. If two rows (or two columns) are equal then the determinant is zero. Scaling any row by λ results in a scaling of the determinant by λ Interchanging rows (or columns) changes the sign of the determinant. A lower or upper triangular matrix has a determinant given as product of diagonal terms. The determinant of a matrix and determinant of its transpose are equal

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

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

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

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

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

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

Find the determinant (using properties) Check answers using MATLAB – e.g det(A)

Find the determinant (using properties) Check answers using MATLAB – e.g det(A)

Find the determinant (using properties)

Find the determinant (using properties)

Find the determinant (using properties)

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.