210 likes | 376 Views
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 12.3 MATLAB Implementation 12.3.1 Matrix Multiplication 12.3.2 Matrix Division 12.4 Rotating Coordinates
E N D
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 12.3 MATLAB Implementation 12.3.1 Matrix Multiplication 12.3.2 Matrix Division 12.4 Rotating Coordinates 12.4.1 2-D Rotation 12.4.2 3-D Rotation
Outline from Chapter 12 -2 • 12.5 Solving Simultaneous Linear Equations • 12.5.1 Intersecting Lines • 12.5.2 Curve Fitting
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)
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
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.
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 ==> mtimes Inner matrix dimensions must agree. >> b = reshape(b, 2,1) b = 7 7 >> inv(A) * b ans= 1 1 >>
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.
MATLAB Matrix Division -2 Besides the multiplication (by the inverse) on the left that is illustrated on the left, there is also multiplication on the right, shown below: • >> inv(A) * b • ans = • 1 • 1 • >> A\b • ans = • 1.0000 • 1.0000 • >> c = 1 2 2 3 3 4 >> c * inv(A) ans = 0.7143 -0.2857 0.8571 -0.1429 1.0000 -0.0000 >> c / A ans = 0.7143 -0.2857 0.8571 -0.1429 1.0000 0
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
3D Rotation function 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 rotations zRotation = [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
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
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
N Independent Linear Equations in N Unknowns • Looks like: • a1x1 + b1x2 + …+p1xN= y1 • a2x1+ b2x2+ …+p2xN= y2 • … • aNx1+ bNx2+ …+pNxN= 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
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.
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
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
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)
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).
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’)
Operating on Images • Stretching (sampling and resampling the array) • Color Masking