3

hw9-1

Embed Size (px)

DESCRIPTION

Numerical analysis

Citation preview

Page 1: hw9-1

Homework Set #9, Math 475APart I: This part is the analytical part.1. Find the Doolittle, Crout, and Cholesky factorizations of the following matrices if the factor-ization exists:(a) 264 7 8 914 46 5128 82 163 375(b) 264 4 12 112 1716 141 14 3364 375Part II: This part should be done using MATLAB.1. The following is a MATLAB implementation of Doolittle's factorization algorithm at the bottomof page 164, the forward and back substitution algorithm on page 160 (equations (2) and (3)).These algorithms combined together has been used to solve a linear system of equations A�x = b.% Doolittle.m% Doolittle's LU factorization to solve the linear system of equations% A * x = bclear;format short;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Step 0: Assign the matrix A and the vector b%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%n = 3;A = [ 7, 8, 9; 14, 46, 51; 28, 82, 163 ];b = [ 24; 111; 273 ];%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Step 1: Doolittle's LU factorization: A = L * U% where L is unit lower triangular and U is upper triangular.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%L = zeros(n,n); % initialize L and U to be n by nU = zeros(n,n); % matrices with all zero entries.for k = 1:nL(k,k) = 1;for j = k:nU(k,j) = A(k,j);for s = 1:(k-1)U(k,j) = U(k,j) - L(k,s)*U(s,j);1

Page 2: hw9-1

endendfor i = (k+1):nL(i,k) = A(i,k);for s = 1:(k-1)L(i,k) = L(i,k) - L(i,s)*U(s,k);endL(i,k) = L(i,k) / U(k,k);endend% output L and U:LU%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Step 2: Forward substitution to solve L * y = b%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%y = zeros(n,1); % Initialize y to be a column vector% of length n with zeros entries.y(1) = b(1);for i = 2:ny(i) = b(i);for j = 1:(i-1)y(i) = y(i) - L(i,j)*y(j);endend% output y:y%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Step 3: Back substitution to solve U * x = y%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%x = zeros(n,1); % Initialize x to be a column vector% of length n with zeros entries.x(n) = y(n) / U(n,n);for i = (n-1):-1:1x(i) = y(i);for j = (i+1):nx(i) = x(i) - U(i,j)*x(j);endx(i) = x(i) / U(i,i);end% output x:x 2

Page 3: hw9-1

Modify the code above to use Cholesky factorization on pages 167{168 to solve the system8>>><>>>: 0:05x1 + 0:07x2 + 0:06x3 + 0:05x4 = 0:230:07x1 + 0:10x2 + 0:08x3 + 0:07x4 = 0:320:06x1 + 0:08x2 + 0:10x3 + 0:09x4 = 0:330:05x1 + 0:07x2 + 0:09x3 + 0:10x4 = 0:31 (1)Hand in the MATLAB code and the output.

3