SECTION 2: VECTORS AND MATRICES. ENGR 112 Introduction to Engineering Computing

Similar documents
TOPIC 2 Computer application for manipulating matrix using MATLAB

Review of Linear Algebra

Computational Foundations of Cognitive Science

Matrices. Chapter Definitions and Notations

Basic Linear Algebra in MATLAB

R.3 Properties and Order of Operations on Real Numbers

January 18, 2008 Steve Gu. Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,

Rational Expressions and Functions

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

Numerical Methods Lecture 2 Simultaneous Equations

Linear Algebra (Review) Volker Tresp 2017

Chapter 2 Notes, Linear Algebra 5e Lay

Identity Matrix: EDU> eye(3) ans = Matrix of Ones: EDU> ones(2,3) ans =

Linear Algebra, Vectors and Matrices

Numerical Methods Lecture 2 Simultaneous Equations

Linear Algebra (Review) Volker Tresp 2018

Using MATLAB. Linear Algebra

Lesson 1: Successive Differences in Polynomials

Getting Started with Communications Engineering. Rows first, columns second. Remember that. R then C. 1

Chapter 2. Matrix Arithmetic. Chapter 2

Multiplying matrices by diagonal matrices is faster than usual matrix multiplication.

Eureka Math. Algebra II Module 1 Student File_A. Student Workbook. This file contains Alg II-M1 Classwork Alg II-M1 Problem Sets

Linear Algebra II. 2 Matrices. Notes 2 21st October Matrix algebra

Elementary maths for GMT

Linear Algebra. The analysis of many models in the social sciences reduces to the study of systems of equations.

Math, Stats, and Mathstats Review ECONOMETRICS (ECON 360) BEN VAN KAMMEN, PHD

P.2 Multiplication of Polynomials

Chapter 1: Systems of linear equations and matrices. Section 1.1: Introduction to systems of linear equations

Rational Expressions and Functions

Lesson 1: Inverses of Functions Lesson 2: Graphs of Polynomial Functions Lesson 3: 3-Dimensional Space

An Introduction to Scilab for EE 210 Circuits Tony Richardson

Linear Algebra review Powers of a diagonalizable matrix Spectral decomposition

This appendix provides a very basic introduction to linear algebra concepts.

Lecture 3: Matrix and Matrix Operations

MATH.2720 Introduction to Programming with MATLAB Vector and Matrix Algebra

Large Scale Data Analysis Using Deep Learning

A Review of Linear Algebra

Linear Algebra Using MATLAB

1 Matrices and matrix algebra

Matrix Arithmetic. (Multidimensional Math) Institute for Advanced Analytics. Shaina Race, PhD

SECTION 5: POWER FLOW. ESE 470 Energy Distribution Systems

Chapter 2: Numeric, Cell, and Structure Arrays

Worksheets for GCSE Mathematics. Algebraic Expressions. Mr Black 's Maths Resources for Teachers GCSE 1-9. Algebra

Mathematics Department. Summer Course Work. Geometry

Computational Foundations of Cognitive Science. Addition and Scalar Multiplication. Matrix Multiplication

ECE 102 Engineering Computation

Linear Systems of n equations for n unknowns

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

ANALYTICAL MATHEMATICS FOR APPLICATIONS 2018 LECTURE NOTES 3

Introduction to Matrices

Linear Algebra review Powers of a diagonalizable matrix Spectral decomposition

Matrices A matrix is a rectangular array of numbers. For example, the following rectangular arrays of numbers are matrices: 2 1 2

F.1 Greatest Common Factor and Factoring by Grouping

Systems of Linear Equations and Matrices

Preparing for the HNC Electrical Maths Components. online learning. Page 1 of 15

A primer on matrices

MATH2210 Notebook 2 Spring 2018

Linear Algebra V = T = ( 4 3 ).

Systems of Linear Equations and Matrices

Math 123, Week 2: Matrix Operations, Inverses

Final Exam Practice Problems Answers Math 24 Winter 2012

x + 2y + 3z = 8 x + 3y = 7 x + 2z = 3

10.4 The Cross Product

[ Here 21 is the dot product of (3, 1, 2, 5) with (2, 3, 1, 2), and 31 is the dot product of

Chapter Contents. A 1.6 Further Results on Systems of Equations and Invertibility 1.7 Diagonal, Triangular, and Symmetric Matrices

3. Array and Matrix Operations

Note: The command name is upper case in the description given by help, but must be lower case in actual use. And the backslash Anb is dierent when A i

Linear Algebra and TI 89

MAT 343 Laboratory 3 The LU factorization

P.3 Division of Polynomials

SECTION 7: FAULT ANALYSIS. ESE 470 Energy Distribution Systems

Linear Systems of Differential Equations

Linear Algebra Tutorial for Math3315/CSE3365 Daniel R. Reynolds

Review of Operations on the Set of Real Numbers

Lecture 3: Special Matrices

Definition 2.3. We define addition and multiplication of matrices as follows.

Mathematics 13: Lecture 10

PREPARED BY: J. LLOYD HARRIS 07/17

Lecture 3 Linear Algebra Background

Math Assignment 3 - Linear Algebra

Phys 201. Matrices and Determinants

2. Similarly, 8 following generalization: The denominator of the rational exponent is the index of the radical.

Mathematics for Graphics and Vision

+ MATRIX VARIABLES AND TWO DIMENSIONAL ARRAYS

Elementary Linear Algebra

Order of Operations. Real numbers

7.3 The Jacobi and Gauss-Seidel Iterative Methods

a11 a A = : a 21 a 22

New Mexico Tech Hyd 510

Review of linear algebra

B553 Lecture 5: Matrix Algebra Review

Finite Math - J-term Section Systems of Linear Equations in Two Variables Example 1. Solve the system

Chapter 2. Linear Algebra. rather simple and learning them will eventually allow us to explain the strange results of

Review of Basic Concepts in Linear Algebra

1 Multiply Eq. E i by λ 0: (λe i ) (E i ) 2 Multiply Eq. E j by λ and add to Eq. E i : (E i + λe j ) (E i )

due date: third day of class estimated time: 10 hours (for planning purposes only; work until you finish)

Quantum Computing Lecture 2. Review of Linear Algebra

Variations. ECE 6540, Lecture 02 Multivariate Random Variables & Linear Algebra

A Review of Matrix Analysis

Lesson 7: Algebraic Expressions The Commutative and Associative Properties

Transcription:

SECTION 2: VECTORS AND MATRICES ENGR 112 Introduction to Engineering Computing

2 Vectors and Matrices

The MAT in MATLAB 3 MATLAB The MATrix (not MAThematics) LABoratory MATLAB assumes all numeric variables are matrices Vectors and scalars are special cases of matrices This section of notes will introduce concept of vectors and matrices Matrix math linear algebra fundamentals You ll cover this in much more detail in your Linear Algebra course MATH 306

Matrices 4 Matrix Array of numerical values, e.g.: AA = 7 0 1 4 4 2 9 5 8 3 4 0 The variable, AA, is a matrix An mm nn matrix has mm rows and nn columns These are the dimensions of the matrix AA is a 3 4 matrix

Matrix Dimensions and Indexing 5 An mm nn matrix: AA = aa 11 aa 12 aa 1nn aa 21 aa 22 aa 2nn aa mmm aa mmm aa mmmm Use indices to refer to individual elements of a matrix aa iiii : the element of AA in the ii ttt row and the jj ttt column

Vectors 6 Vectors A matrix with one dimension equal to one A matrix with one row or one column Row vector One row a 1 nn matrix, e.g.: A 1 3 row vector Column vector One column an mm 1 matrix, e.g.: A 3 1 column vector xx = 9 1 4 xx = 5 1 8

Scalars 7 Scalar A 1 1 matrix The numbers we are we are familiar with, e.g.: bb = 4, xx = 3 + jjj.8, yy = 1 10 9 We understand simple mathematical operations involving scalars Can add, subtract, multiply, or divide any pair of scalars Not true for matrices Depends on the matrix dimensions

8 Mathematical Matrix Operations

Matrix Addition and Subtraction 9 As long as matrices have the same dimensions, we can add or subtract them Addition and subtraction are done element-by-element, and the resulting matrix is the same size 4 8 0 3 + 1 4 6 1 = 5 4 6 2 4 8 0 3 1 4 6 1 = 3 12 6 4 We can also add scalars to (or subtract from) matrices 1 4 6 1 + 5 = 6 1 11 4

Matrix Addition and Subtraction 10 If matrices are not the same size, and neither is a scalar, addition/subtraction are not defined The following operations cannot be done 4 8 0 3 + 1 4 6 6 1 9 =? 8 3 1 4 6 1 =? Addition is commutative (order does not matter): AA + BB = BB + AA = CC 4 8 0 3 + 1 4 6 1 = 1 4 6 1 + 4 8 0 3 = 5 4 6 2

Matrix Multiplication 11 In order to multiply matrices, their inner dimensions must agree We can multiply AA BB only if the number of columns of AA is equal to the number of rows of BB Resulting Matrix has same number of rows as AA and same number of columns as BB AA BB = CC (m x n) (n x p) = (m x p)

Matrix Multiplication AA BB = CC 12 aa 11 aa 1nn aa mmm aa mmnn bb 11 bb 1pp = bb nn1 bb nnnn cc 11 cc 1pp cc mmm cc mmpp The ii, jj ttt entry of CC is the dot product of the ii ttt row of AA with the jj ttt column of BB nn cc iiii = kk=1 aa iiii bb kkkk Consider the multiplication of two 2 2 matrices: aa 11 aa 12 aa 21 aa 22 bb 11 bb 12 bb 21 bb 22 = aa 11bb 11 + aa 12 bb 21 aa 11 bb 12 + aa 12 bb 22 aa 21 bb 11 + aa 22 bb 21 aa 21 bb 12 + aa 22 bb 22

Matrix Multiplication Examples 13 A 2 2 and a 2 3 yield a 2 3 1 4 2 1 3 1 5 6 2 0 = 27 7 5 12 0 10 A 3 3 and a 3 1 result in a 3 1 1 5 0 0 4 8 2 7 3 6 1 2 = 11 20 25

Matrix Multiplication Properties 14 Matrix multiplication is not commutative Order matters Unlike scalars In general, AA BB BB AA If AA and/or BB is not square then one of the above operations may not be possible anyway Inner dimensions may not agree for both product orders

Matrix Multiplication Properties 15 Matrix multiplication is associative Insertion of parentheses anywhere within a product of multiple terms does not affect the result: (AA BB) CC = DD AA (BB CC) = DD Matrix multiplication is distributive Multiplication distributes over addition Must maintain correct order, i.e. left- or right-multiplication AA(BB + CC) = AAAA + AACC (BB + CC)AA = BBBB + CCCC

Identity Matrix 16 Multiplication of a scalar by 1 results in that scalar aa 1 = 1 aa = aa The matrix version of 1 is the identity matrix Ones along the diagonal, zeros everywhere else Square nn nn matrix Denoted as II or II nn, where nn is the matrix dimension, e.g. 1 0 0 II 33 = 0 1 0 0 0 1 Left- or right-multiplication by an identity matrix results in that matrix, unchanged AA II = II AA = AA

Identity Matrix 17 Right-multiplication of an nn nn matrix by an nn nn identity matrix, II nn 1 5 0 0 4 8 2 7 3 1 0 0 0 1 0 0 0 1 Same result if we left-multiply by II nn = 1 5 0 0 4 8 2 7 3 1 0 0 0 1 0 0 0 1 1 5 0 0 4 8 2 7 3 = 1 5 0 0 4 8 2 7 3

Identity Matrix 18 Right-multiplication of an mm nn matrix by an nn nn identity matrix 1 0 0 1 5 0 0 4 8 0 1 0 0 0 1 = 1 5 0 0 4 8 Same result if we left-multiply the mm nn matrix by an mm mm identity matrix 1 0 0 1 1 5 0 0 4 8 = 1 5 0 0 4 8

Vector Multiplication 19 Vectors are matrices, so inner dimensions must agree Two types of vector multiplication: Inner product (dot product) Result is a scalar aa 11 aa 12 bb 11 bb 21 = aa 11 bb 11 + aa 12 bb 21 Outer product Result for n-vectors is an n x n matrix aa 11 aa 21 bb 11 bb 12 = aa 11bb 11 aa 11 bb 12 aa 21 bb 11 aa 21 bb 12

Exponentiation 20 As with scalars, raising a matrix to the power, n, is the multiplication of that matrix by itself n times AA 33 = AA AA AA What must be true of a matrix for exponentiation to be allowable? Inner matrix dimensions must agree Rows of AA must equal columns of AA n x n Matrix must be square

Matrix Division Multiplication by the Inverse 21 Scalar division that we are accustomed to can be thought of as multiplication by an inverse: aa bb = aa 1 bb = aa bb 1 This is how we divide matrices as well AA BB BB 11 = AA Multiplication of a scalar by its inverse is equal to 1. For a matrix, the result is the identity matrix AA AA 11 = II = 1 0 0 1

Matrix Inverse 22 Recall that matrix multiplication is not commutative Right- and left-multiplication are different operations AA BB BB 11 = AA BB 11 AA BB The inverse does not exist for all matrices Non-invertible matrices are referred to as singular Matrix must be square for its inverse to exist

Matrix Inverse 23 Possible to calculate matrix inverses by hand Simple for small matrices Quickly becomes tedious as matrices get larger For example, the inverse of a 2 2 matrix: aa cc For example: bb dd 1 AA = 2 5 2 4 = AA 11 = 1 8 10 1 aaaa bbbb dd cc 4 5 2 2 bb aa = 2 2.5 1 1

Matrix Inverse - Example 24 Multiplication of a matrix by its inverse yields the identity matrix For example: AA AA 11 = 2 5 2 4 2 2.5 1 1 = 1 0 0 1 Or, for a 3 3: AA = 2 0 2 0 1 2 0 0 2 2 0 2 0 1 2 0 0 2, AA 11 = 0.5 0 0.5 0 1 1 0 0 0.5 0.5 0 0.5 0 1 1 0 0 0.5 = 1 0 0 0 1 0 0 0 1 You ll learn more about this in Linear Algebra not critical here

Matrix Transpose 25 The transpose of a matrix is that matrix with rows and columns swapped First row becomes the first column, second row becomes the second column, and so on For example: AA = 0 9 2 7 6 3 AA TT = 0 2 6 9 7 3 Row vectors become column vectors and vice versa xx = 7 1 4 xx TT = 7 1 4

Why Do We Use Matrices? 26 Vectors and matrices are used extensively in many engineering fields, for example: Modeling, analysis, and design of dynamic systems Controls engineering Image processing Etc. Very common usage of vectors and matrices is to represent systems of equations These regularly occur in all fields of engineering

Systems of Equations 27 Consider a system of three equations with three unknowns: 3xx 1 + 5xx 2 9xx 3 = 6 3xx 1 + 7xx 3 = 2 xx 2 + 4xx 3 = 8 Can represent this in matrix form: 3 5 9 3 0 7 0 1 4 xx 1 xx 2 xx 3 = 6 2 8 Or, more compactly as: AAAA = bb Perform algebra operations as we would if AA, xx, and bb were scalars Observing matrix-specific rules, e.g. multiplication order, etc.

28 Vectors & Matrices in MATLAB

Defining Vectors and Matrices in MATLAB 29 Let s say we want to assign the following matrix variable in MATLAB: AA = 2 5 1 4 6 0 Enclose matrices in square brackets Elements on the same row are separated by spaces or commas Rows are separated by semicolons In MATLAB: A = [2, 5, 1; -4, 6, 0]; or A = [2 5 1; -4 6 0];

Ellipsis Continuation Operator 30 An ellipsis can be used as a continuation operator Tells MATLAB that a single command continues on the next line Improves readability Long expressions Large matrices

Vector and Matrix Generation 31 Often want to automatically generate vectors and matrices without having to enter them element-byelement A few of MATLAB s array-generation functions: Colon operator (:) ones( ) zeros( ) diag( ) eye( )

Vector Generation Colon operator 32 Create vectors of evenly-spaced values using the colon (:) operator x = xstart:xstep:xstop; xstart: value of the first element in the vector xstep: optional increment value default: xstep = 1 xstop: maximum value of vector entries x: vector of points that is created Number of elements in the vector: NN = floor xx ssssssss xx ssssssssss xx ssssssss + 1 Value of the last element in the vector is xx llllllll = xx ssssssssss + NN 1 xx ssssssss

Vector Generation Colon operator 33 Default increment is 1 Can specify increment value Vector values will not exceed the stop value May not include stop value Increment value can be negative

Vector Generation linspace( ) 34 x = linspace(xstart,xstop,n) xstart: value of the first element in the vector xstop: value of the last element in the vector N: Number of elements in the vector x: vector of linearly spaced points Colon operator: Stop value may not be in the vector Number of points not directly specified linspace( ): x(end) = xstop Increment value not directly specified

Array Generation ones( ), zeros( ) 35 Generate an NN NN square matrix of all 1 s or all 0 s: A = ones(n); or A = zeros(n) Generate an mm nn vector of all 1 s or 0 s A = ones(m,n); or A = zeros(m,n)

Identity Matrix eye( ) 36 I = eye(n) N: identity matrix dimension I: NN NN identity matrix

Random Number Generation rand( ) 37 Very often useful to generate random numbers Simulating the effect of noise Monte Carlo simulation, etc. x = rand(m,n) m: number of rows in the matrix of random numbers n: number of columns in the matrix of random numbers x: mm nn matrix of uniformly-distributed random values on the interval [0,1] If only one dimension specified (i.e. rand(n)), result is an NN NN matrix of random values For normally-distributed (Gaussian) values, use: x = randn(m,n)

38 Array Indexing in MATLAB

Array Indexing 39 We ve seen how we can refer to specific elements in an array by their row, column indices, aa iiii : AA = aa 11 aa 12 aa 1nn aa 21 aa 22 aa 2nn aa mmm aa mmm aa mmmm MATLAB allows us to do the same thing Indices specified in parentheses immediately following the array variable name Indices must be positive Numbering begins at 1 For example, B(2,5) refers to the element in the 2 nd row and 5 th column of the matrix B Also possible to specify ranges of elements within an array

Array Indexing 40 Element of A in row i, column j: A(i,j) Elements of A in row i, all columns: A(i,:) Elements of A in all rows, column j: A(:,j) Elements of A in rows i through k, columns j through q: A(i:k,j:q) Elements of A in the second through last row and the last column: A(2:end,end)

Array Indexing Single Index 41 MATLAB also allows for indexing elements within an array with a single index linear indexing Elements are counted down each column sequentially Very useful for vectors Not often useful for matrices For example, for a 3 4 matrix: AA = aa 1 aa 4 aa 7 aa 10 aa 2 aa 5 aa 8 aa 11 aa 3 aa 6 aa 9 aa 12 In MATLAB: A(8) = A(2,3) = aa 8

Array Indexing 42 A(2,5) is the value in the 2 nd row, 5 th column of A A colon (:) indicates all rows or columns Can index over a range of rows and/or columns Use end to index the last row or column

Array Indexing 43 Use indexing to redefine specific elements in an array Use colon indexing to replace entire row/column with a vector Can replace all elements within a range Can set all equal to a scalar Or, redefine as a matrix

Array Indexing Single Index 44 Single indexing counts down successive columns end index indicates last row, last column Generally, more useful for vectors

Matrix Size Functions size, length 45 size(a) Returns a 1 2 row vector containing number of rows and columns of A length(a) Returns a scalar equal to the greater of the number of rows or columns of A length(a) = max(size(a)) Useful for vectors

Multidimensional Arrays 46 MATLAB allows for the definition of arrays with more than two dimensions Arbitrary number of dimensions allowed Three dimensional arrays are common Index an N-dimensional array with N indices For example, a 3 3 3 array looks like this: row (1,1,3) (1,2,3) (1,3,3) (2,1,3) (2,2,3) (2,3,3) (1,1,2) (1,2,2) (1,3,2) (3,1,3) (3,2,3) (3,3,3) (2,1,2) (2,2,2) (2,3,2) (1,1,1) (1,2,1) (3,1,2) (1,3,1) (3,2,2) (3,3,2) (2,1,1) (2,2,1) (2,3,1) (3,1,1) (3,2,1) (3,3,1) column

Multidimensional Arrays 47 A did not exist prior to assignment Size was undefined Defined as smallest possible array allowing for assignment (3 3 3) All other elements set to zero Three-dimensional array requires three indices

48 Matrix Operations in MATLAB

Matrix Operations in MATLAB 49 MATLAB treats all numeric variables as matrices Mathematical operations are matrix operations by default Addition, subtraction, multiplication Matrix dimensions must be compatible Built-in functions designed to accept matrices as input arguments, e.g.: Trigonometric functions Exponential Square root Statistical functions, etc.

Matrix Operations in MATLAB 50 Matrices can be added, as long as they are the same size Multiplication is matrix multiplication Inner dimensions must agree Otherwise, an error results Here, transposing d satisfies inner dimension requirement

Passing Matrices to Functions 51 Can pass vectors and matrices to most functions, just as we would a scalar The sine of a vector of angles calculated all at once No need to pass one-ata-time Result is a vector of the same size abs( ) calculates the absolute value

Array Operations 52 Often, we want to operate on vectors and matrices element-by-element Array operations not matrix operations MATLAB s array operators:.*,./,.^ For example: AA = 1 2 3 4 BB = 3 4 7 5 but AA BB = 17 14 37 32 AA. BB = 3 8 21 20

Array Operations 53 Matrices must be the same size to perform array operations Not only inner dimensions must agree For example: aa = 1 2 bb = 3 4 but Similarly, but aa bb = EEEEEEEEEE aa. bb = 3 8 bb/aa = EEEEEEEEEE bb./aa = 3 2

Array Operations 54 Matrix exponentiation requires square matrix and scalar exponent Array exponentiation by a scalar works for any matrix Also allows for exponentiation by another matrix of the same size For example: aa = 1 2 3 4 bb = 4 3 2 1 but And, but aa^2 = EEEEEEEEEE aa. ^22 = 1 4 9 16 aa^bb = EEEEEEEEEE aa. ^bb = 1 8 9 4