10.34, Numerical Methods Applied to Chemical Engineering Professor William H. Green Lecture #3: Matrix Factorization. Modularization. v v U⋅x = v ⎛• ⎜ ⎜0 ⎜0 ⎜ ⎜0 ⎝ • • 0 0 • • • 0 • ⎞⎛ x1 ⎞ ⎛ v1 ⎞ ⎟⎜ ⎟ ⎜ ⎟ • ⎟⎜ x 2 ⎟ ⎜ v 2 ⎟ = • ⎟⎜ x3 ⎟ ⎜ v3 ⎟ ⎟⎜ ⎟ ⎜ ⎟ • ⎟⎠⎜⎝ x 4 ⎟⎠ ⎜⎝ v 4 ⎟⎠ ⎛•⎞ ⎛•⎞ ⎛•⎞ ⎛•⎞ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜•⎟ v ⎜•⎟ ⎜•⎟ ⎜ 0⎟ x1 ⎜ ⎟ + x 2 ⎜ ⎟ + x3 ⎜ ⎟ + x 4 ⎜ ⎟ = (v ) 0 0 • • ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜•⎟ ⎜ 0⎟ ⎜ 0⎟ ⎜ 0⎟ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ function x = backsub(U,v) N = length(v); % dimension of matrix for i = 1:(N-1) m = N+1-i; % backwards from bottom matrix x(m) = v(m)/U(m,m); % solve equation with one unknown v = v – x(m)*U(:,m); % move terms involving known x(m) to r.h.s. end x(1) = v(1)/U(1,1); % tested using: U = [1 2 3; 0 4 5; 0 0 6], v = [0 1 3] % x = [-.75 -.375 .5] To comment a block of text: Ctrl-R Gaussian Elimination ⎛ 1 2 3⎞ ⎛ 0⎞ ⎜ ⎟ v ⎜ ⎟ ⎜ 4 5 6⎟ * x = ⎜1⎟ ⎜ 7 8 0⎟ ⎜ 3⎟ ⎝ ⎠ ⎝ ⎠ ⎛ 1 2 3 0⎞ ⎜ ⎟ add - 4 *1st row Augment : ⎜ 4 5 6 1 ⎟ nd ⎜ 7 8 0 3 ⎟ to 2 row ⎝ ⎠ ⎛1 2 3 0⎞ ⎜ ⎟add 2 * 2 nd row ⎜0 - 3 - 6 1⎟ rd ⎜ 0 - 6 - 21 3 ⎟ to 3 row ⎝ ⎠ ⎛1 2 3 0⎞ ⎜ ⎟ ⎜ 0 - 3 - 6 1 ⎟ ⇒ Upper Triangular solution ⎜0 0 - 9 1⎟ ⎝ ⎠ *gauss.m* function [U,v] = gauss(A,b) % uses Gaussian elimination to convert Ax=b to Ux=v % where U is upper triangular and A is NxN matrix and b is Nx1 matrix M = [A b]; N = length(b); for i = 1:(N-1) for j = (i+1):N lambda = -M(j,i)/M(i,i); M(j,i) = M(j,i)+lambda*M(i,i); end end U = M(i,1:N) unpacks augmented solution v = M(i,N+1) Gaussian Elimination has N3 operations. Not good when N is large (106). Backsub requires N2 operations. Cite as: William Green, Jr., course materials for 10.34 Numerical Methods Applied to Chemical Engineering, Fall 2006. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY]. v a can span entire 2D vector space (linear combination) v b Figure 1. 2 vectors with a common origin and different directions. r v v x = c1a + c2b r v v v x = c1 a + c 2 b + c3 d can span entire 3D vector space (linear combination) v v r *Not linearly independent if: d = c1 a + c 2 b same direction - not independent v a v b Figure 2. 2 vectors with a common origin and direction. rank (m) = # of linearly independent column vectors = dimension of space spanned by column vectors Existence A*x=b solution exists if: rank(A) == rank ([A b]) ⎛ col ⎞ ⎛ col ⎞ ⎛ col ⎞ v x1 ⎜⎜ ⎟⎟ + x2 ⎜⎜ ⎟⎟ + L + x N ⎜⎜ ⎟⎟ = (b ) ⎝ 1 ⎠ ⎝ 2 ⎠ ⎝N⎠ r if rank(A) = N and A is NxN: does not matter what b is; always a solution Ay = 0 If null space of A does not include any Ax = b vector beyond 0, then solution unique A(x+y) = The null space is the set of all vectors b of such that x = 0. Run *gauss.m* => Error! NaN: error – not a number (divided by 0) Need to pivot rows to avoid 0 in diagonal * see “gausselim_pivot.m” online * A*x1 = b1 A*x2 = b2 Gauss elimination has nothing to do with b, only A L-1 * A * x1 = L-1 * b1 U LUx = b Lv = b L: lower triangular Alternative method to Gaussian elimination v For more info: help lu Ux=v MATLAB: [L,U,P] = lu(A) A = P’*L*U (factorizing A) PAx = Pb LU LUx = Pb Lv = P*b (forward sub) Ux = v (backward sub) 10.34, Numerical Methods Applied to Chemical Engineering Prof. William Green Lecture 1 Page 2 of 2 Cite as: William Green, Jr., course materials for 10.34 Numerical Methods Applied to Chemical Engineering, Fall 2006. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].