Top Banner
Lecture 18 Chapter 12 Matrices continued
21

Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Dec 30, 2015

Download

Documents

Fay Hicks
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Lecture 18

Chapter 12 Matricescontinued

Page 2: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Outline from Chapter 12 -1

12.2 Matrix Operations12.2.1 Matrix Multiplication12.2.2 Matrix Division12.2.3 Matrix Exponentiation

12.3 MATLAB Implementation12.3.1 Matrix Multiplication12.3.2 Matrix Division

12.4 Rotating Coordinates12.4.1 2-D Rotation12.4.2 3-D Rotation

Page 3: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Outline from Chapter 12 -2

• 12.5 Solving Simultaneous Linear Equations– 12.5.1 Intersecting Lines– 12.5.2 Curve Fitting

Page 4: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Matrix Multiplication

• A x = b• Number of columns in A = number of rows in x• Number of rows in b is number of rows in A• Number of columns in b is number of columns

in x.• b(i, j) = (å k=1 to nColsA) A(i,k)*x(k,j)

Page 5: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Matrix Division

• With Ax = b, want to know x.• A -1 A x = A -1 b uses the inverse of A,

which does not always exist• Requirements for inverse to exist:– Number of rows of A = number of columns of A– i.e., A is square

Page 6: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Matrix Exponentiation

• Raising matrix A to non-zero integer power• For positive power, multiply A by itself that

many times.• For negative power, first obtain positive power

then invert.• Non-integer scalar powers are also

meaningful, but outside the scope of the book.

Page 7: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

MATLAB Matrix Multiplication

• Use the operator *:b = A * x

>> A=[3 4; 4 3];>> b=[7 7]b = 7 7>> inv(A) * b??? Error using ==> mtimesInner matrix dimensions must agree.>> b = reshape(b, 2,1)b = 7 7>> inv(A) * bans = 1 1>>

Page 8: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

MATLAB Matrix Division

• If A is square and nonsingular, then, without roundoff error, X = inv(A)*B is theoretically the same as X = A\B and Y = B*inv(A) is theoretically the same as Y = B/A. But the computations involving the backslash and slash operators are preferable because they require less computer time, less memory, and have better error-detection properties.

Page 9: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

MATLAB Matrix Division -2• >> inv(A) * b

• ans =

• 1• 1

• >> A\b

• ans =

• 1.0000• 1.0000

• >>

Besides the multiplication (by the inverse) on the left that is illustrated on the left, there is also multiplication on the right, shown below:

c = 1 2 2 3 3 4>> c * inv(A)ans = 0.7143 -0.2857 0.8571 -0.1429 1.0000 -0.0000>> c / Aans = 0.7143 -0.2857 0.8571 -0.1429 1.0000 0

Page 10: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

An Application of Matrix Multiplication

• Rotate Coordinates in 2-D, about the origin:– xNew, yNew = rotationMatrix * xOld,yOld

• Rotate coordinates in 2-D, about some other point:– Translate point of rotation to origin– Rotate– Translate back

Page 11: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

3D Rotationfunction newCoords = rotate3D(theta, phi, psi, oldCoords)%perform a rotation, centered on origin, about axes%rotations are performed in order about z, about y, about x%to use other orders, call function multiple times, employ 0-angle rotationszRotation = [cos(theta) -sin(theta) 0;... sin(theta) cos(theta) 0;... 0 0 1];yRotation = [cos(phi) 0 sin(phi);... 0 1 0 ;... sin(phi) 0 cos(phi)];xRotation = [1 0 0;... 0 cos(psi) -sin(psi);... 0 sin(psi) cos(psi)];newCoords = zRotation * yRotation * zRotation * oldCoords;end

Page 12: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Simultaneous Linear Equations

• Soccer ball: – nP = number of pentagons– nH = number of hexagons– nP + nH = number of faces– nE = (5* nP + 6*nH)/2– nV = 2*nE/(how many edges impinge on a vertex?)

• Euler’s formula: If you take ANY solid without holes that's made of flat faces, and F = number of faces, E = number of edges, and V = number of vertices: F - E + V = 2

Page 13: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

N Independent Linear Equations in M Unknowns

• N < M: we can obtain a subspace, i.e., a restriction of the values that correspond to solutions

• N=M: we can obtain single values for each of the unknowns

• N>M: might be over-specified, i.e., might be no solution

Page 14: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

N Independent Linear Equations in N Unknowns

• Looks like:– a1x1 + b1x2 + …+p1 xN = y1

– a2x1 + b2x2 + …+p2 xN = y2

– …

– aNx1 + bNx2 + …+pN xN = yN

• Converts to, for coefficients a, b, etc.:[a1 b1 …+p1 ;

a2 b2 …+p2 ;

… a2 b2 …+p2 ]

• For x: [x1 x2 …xN ] as a column vector, same for y• Ax = y

Page 15: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Curve Fitting:Polynomial of Order N to N+1 points

• Given N+1 points, (xi, yi), find the polynomial f(x), of order N, that has the smallest difference squared, (yi-f(xi))2, summed over the N+1 points.

• http://mathworld.wolfram.com/LeastSquaresFittingPolynomial.html

• C, the coefficients, are what we want to know• C = A \ B• A, B are given at the link above.

Page 16: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Chapter 13 Outline -1

• 13.1 Nature of an Image• 13.2 Image Types– 13.2.1 True Color Images– 13.2.2 Gray Scale Images– 13.2.3 Color Mapped Images– 13.2.4 Preferred Image Format

• 13.3 Reading, Displaying and Writing Images

Page 17: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Chapter 13 Outline -2

• 13.4 Operating on Images– 13.4.1 Stretching or Shrinking Images– 13.4.2 Color Masking– 13.4.3 Making a Collage– 13.4.4 Creating a Kaleidoscope– 13.4.5 Images on a Surface

Page 18: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Nature of an Image

• Interpreted information -> drawn graph of n-dimensions (representation based on a concept)

• Vs• Uninterpreted data displayed in n-dimensions

(may lack explanation)

Page 19: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

2-D Slice of an Image

• Many images are of more than 2 dimensions, such as magnetic resonance image (MRI), CAT scan, etc.

• Textbook is talking about 2 dimensions, which could be a slice from a higher dimensional object, or could be the whole image.

• 2D array has a number of cells, given by the number of rows x number of columns.

• In the context of what can be displayed, if a cell corresponds to a dot on the screen, it is called a pixel.

• Color pixels contain a degree of red, blue and green (a positive integer for each, uint8).

Page 20: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Reading in Image File

• One function, imread, reads many types of files.• There are three types of target variables– True color (3 uint8’s per pixel)– Grey scale (1 uint8 per pixel)– Color mapped (1 uint8 per pixel, chooses a 3 uint8

color from the color map)• Pic = imread(myPicture.jpg’)• image(Pic)• imwrite(Pic, ‘newPicture.jpg’, ‘jpg’)

Page 21: Lecture 18 Chapter 12 Matrices continued. Outline from Chapter 12 -1 12.2 Matrix Operations 12.2.1 Matrix Multiplication 12.2.2 Matrix Division 12.2.3.

Operating on Images

• Stretching (sampling and resampling the array)• Color Masking