Docsity

Notes on Vector and Matrices - Computing for Engineers | CS 1371, Study notes of Computer Science

Partial preview of the text

Download Notes on Vector and Matrices - Computing for Engineers | CS 1371 and more Study notes Computer Science in PDF only on Docsity!

CS

Introduction to Computing

for Engineers^ Matrices

Vectors and Matrices

Lecture •^ Basic matrix concepts •^ Creating arrays and matrices •^ Accessing matrix components •^ Manipulating matrices •^ Matrix functions •^ Solving simultaneous equations •^ Regression analysis

LearningObjectivesUnderstand thenature of matricesUnderstand how tomanipulatematrices in Matlab

Why Matrices?

-^ A matrix is an array that obeys a different set of rules^ –^ addition & subtraction are same as for arrays,^ –^ but multiplication, division, etc. are DIFFERENT!^ –^ a matrix can be of any dimension but 2D square matrices are themost common by far •^ A large and very useful area of mathematics deals withwhat is called “linear algebra” and matrices are anintegral part of this. •^ Many advanced computational methods in engineeringmake extensive use of linear algebra, and hence ofmatrices

A Simple Example

-^ A set of simultaneous linear algebraic equationswill often arise in engineering applications •^ How do you solve these?^ –^ Solve first for x in terms of y; substitute insecond and solve for y; use this in first to find x^ –^ Use “Cramer’s Rule”^ –^ Other? •^ Let’s try a more abstract notation:

(^14) x y 4 14 −^ = x y +^ = − 3 2

x^14 y −⎡ ⎤ ⎧^ ⎫^

⎧^ ⎫=⎨ ⎬ ⎨^ ⎬

⎢^ ⎥^

⎣^ ⎦ ⎩

⎭^ ⎩^

⎭^

C z = b* OR

A Few Notes on Matrices

-^ Matlab handles matrix multiplication with the * symbol (NOTE: this isNOT array multiplication!)^ –^ _From our formula we see that in general: AB_*

≠^ _BA_*

-^ In other words, matrix multiplication is NOT commutative • Matrices behave just like arrays for addition and subtraction • Matrix division is not strictly defined but a matrix inverse is availableto address this situation, among others. –^ suppose: 3y=6 and you need to find y… –^ The usual approach: y=6/3=

(division by 3)

-^ Also useful: y=

-1^ _6=2_*^ (multiplication by the inverse of 3)

-^ If we don’t know how to divide, we can accomplish the same byusing the notion of the inverse. Recall definition of inverse: –^ Turns out we know how to compute matrix inverses (but itrequires a lot of computational effort)

(^ ) *(

)^1

value^

inverse value

Let’s Solve Our Problem Using Matlab 8

>> coef=[3 -2; 1 4]coef^ =^3

**-2 1 4

inv(coef)**

% Matlab has the inv() function ans =0.

**0.1429-0.0714 0.

b=[14 -14]'b^ =^14 -14>> z=inv(coef)bz^ =^2 -4>> coefz^

% Let's check our answer! ans =^14 -

Let’s Try This Out…

coef^ =^3

**-2 1 4

bb**^ =^14 -14>> zz=coef\bzz =2.0000-4. OK, now what do you think these expressions yield?

coef\eye(2,2)coef\eye(2,2)coef*

Things Can Get Weird…

-^ We usually think of the unknown (z) as a column matrix andthe RHS (b) as a column matrix also •^ In some fields, it is more useful if these are ROW matrices^ –^ One formulation can easily be converted into the other!^ –^ We can treat either formulation in Matlab •^ First, ON YOUR OWN, prove from our multiplication formulathat: •^ Now, using this, we take the transpose of our equation:

(^ *^ )^

T^ T^ T *^ =

A^ B^

B^ A

T^ ( * )*

T T^ T^

= T =

C^ z^

b z^ C^

b

[^ ]^ [^

] Tx^ y = z T^14 14 =^ − b^3 1 ⎡^ ⎤ T =^ C ⎢^ ⎥^2 4 −⎣^ ⎦ where

Other Matlab Matrix Functions

-^ So far we’ve only scratched the surface of Matlab’sabilities to work with matrices… •^ Matrices can contain COMPLEX numbers •^ Some of the other matrix functions are:^ –^ det(A):

determinant of the matrix

-^ rank(A):

rank of the matrix

-^ trace(A):

sum of diagonal terms

-^ sqrtm(A):

matrix square root

  • (i.e., if

B=sqrtm(A),

then^ A=BB*

-^ norm(A):

matrix norm (useful for vector magnitudes)

-^ eig(A):

eigenvalues and eigenvectors of matrix

-^ … • Keep in mind that Matlab is using some of the latest andmost powerful algorithms to compute these functions.

Finally, What About Vectors?

-^ The matrix and array operations and functions can beused to manipulate vectors, but you’ll have to be careful •^ Vector dot product: •^ ON YOUR OWN:^ –^ Vector magnitude?^ –^ Vector cross product?

>>^ f=[1^ 2]'f^ =^12 >>^ g=[4^ -3]'g^ =^4 -3 >>^ fdotg=f'gfdotg*^ = -

>>^ f=[1^ 2] f^ =^1 2 >>^ g=[4^ -3]g^ =^4 -3 >>^ fdotg=fg'fdotg^ = -2 >>^ gdotf=gf'gdotf^ = - Column vectorsRow vectors^ Matlab hassome functionsto do these,too!

Matrix Multiplication

-^ If I want to multiply A[5,3] by B[3,4], the result will be a[5,4] matrix. •^ In general, multiplying A[m,n] by B[n,p] results in a [m,p]matrix. •^ The inner dimensions must match.

p

m

n^

p

m = n

Matrix Multiplication

-^ If I want to multiply A[5,3] by B[3,4], the result will be a[5,4] matrix. •^ In general, multiplying A[m,n] by B[n,p] results in a [m,p]matrix. •^ The inner dimensions must match.

m

n

p^ = n

p^ m

Matrix Multiplication

-^ If I want to multiply A[5,3] by B[3,4], the result will be a[5,4] matrix. •^ In general, multiplying A[m,n] by B[n,p] results in a [m,p]matrix. •^ The inner dimensions must match.

m

n

p^ = n

p^ m

Matrix Multiplication

-^ If I want to multiply A[5,3] by B[3,4], the result will be a[5,4] matrix. •^ In general, multiplying A[m,n] by B[n,p] results in a [m,p]matrix. •^ The inner dimensions must match.

m

n^

p^

p^ m = n