You are on page 1of 7

ChemE 109 - Numerical and Mathematical Methods in Chemical and Biological Engineering Solution to Homework Set 2 1.

(10 points) From the denition of a determinant, one can show the following properties: Property 1: A determinant changes in sign but not in magnitude with an interchange of a pair of rows or a pair of columns. Property 2: A determinant remains unchanged by additions of multiples of one row (column) to another row (column). Using these properties, show that if A is the upper triangular matrix formed during the solution of a system of equations Ax = c via Gauss elimination with partial pivoting, then: det A = (1)I det A = (1)I where I is the number of row interchanges. Proof: In Gauss elimination, the matrix A is undergoing a series of elementary transformations as follows: A A1 A2 A3 A The transformations can be either interchange of rows(columns) or additions of multiples of one row (column) to another row (column). The As are the intermediate matrices during the transformations. From Properties 1 and 2, the absolute values of the determinants of these As are equal. Since only an interchange changes the sign of the determinant and there are totally I times of interchanges, the relationship between the determinants of A and A is det A = (1)I det A The determinant of A is the multiplication of its diagonal elements due to the upper triangular structure: n det A =
i=1 n

aii
i=1

aii

Therefore the proof is completed. 2. (10 points) (a) Create a program using MATLAB which performs LU decomposition with partial pivoting. Sample program: function [x] = LUSolver(A, b) n=size(A,1); %find the size of A U=A; %Start the U matrix as A
1

L=eye(size(A)); %Start the L matrix as identity P=eye(n); %Start the permutation matrix as identity y=zeros(size(b)); x=zeros(size(b)); for k=1:n-1 %For diagonal elements, k, eliminate in the column below %Partial pivoting [val m]=max(U(k:n,k)); %Find max value from k through n to pivot to m=m+k-1; %Correct the index value U([k m],:)=U([m k],:); %Swap the kth row with the max row b([k m])=b([m k]); %Swap the elements of b %Forward elimination for i=k+1:n L(i,k)=U(i,k)/U(k,k); %Find the elimination factor U(i,:)=U(i,:)-U(k,:)*L(i,k); %row operation end end %Substitution for Ly=b for i=1:n y(i)=b(i)-L(i,1:(i-1))*y(1:(i-1)); end %Substitution for Ux=y for i=n:-1:1 x(i)=(y(i)-U(i,(i+1):n)*x((i+1):n))/U(i,i); end (b) Test your program using the system: 0x1 0x1 1x1 1x1 + + + 0 x2 1 x2 4x2 1 x2 + + + 1 x3 4x3 1 x3 0 x3

+ + +

1x4 1 x4 0 x4 0 x4

= 1 = 9 = 9 = 1

Check your answers by substituting them into the original equations. Solution:

x=

1 2 2 1

(c) Turn in a hardcopy listing of the program and printouts of the results. MATLAB output: >> A=[0 0 1 -1;0 1 -4 1;1 -4 1 0;-1 1 0 0];; >> b=[-1 9 -9 1]; >> LUSolver(A,b)

ans = 1 2 -2 -1 3. (10 points) Use a) Jacobi method, and b) Gauss-Seidel method, with initial guess x(0) = [0 0 0]T , to solve the following system of equations: 5x1 0.3x2 + 0.1x3 = 1 0.2x1 2x2 x3 = 2 0.3x1 0.1x2 4x3 = 0
+1 xm xm i i |100% < 0.5, for all xm i i = 1, 2, 3. Compare the speed of convergence between these two methods. Solution:

Show clearly all steps and terminate the algorithms for | i | = |

5 0.3 0.1 1 2 1 b = 2 A = 0.2 0.3 0.1 4 0 a) Jacobi method: The iteration formulas of Jacobi method for this problem are listed as follows: 1 +1 n xn = (1 + 0.3xn 1 2 0.1x3 ) 5
+1 xn = 2

1 n (2 0.2xn 1 + x3 ) 2 1 n (0.3xn 1 + 0.1x2 ) 4

+1 xn = 3

The initial guess is x(0) = [0 0 0]T . The results of the iterations including the solutions and errors are listed as follows: Iterations 0 1 2 3 4 5 6 x1 0.0000 0.2000 0.2600 0.2614 0.2620 0.2619 0.2619 x2 0.0000 1.0000 1.0200 1.0310 1.0291 1.0293 1.0292
3

x3 0.0000 0.0000 -0.0100 -0.0060 -0.0062 -0.0061 -0.0061

err1 N/A Inf 0.3000 0.0054 0.0022 0.0004 0.0000

err2 N/A Inf 0.0200 0.0108 0.0018 0.0001 0.0001

err3 N/A NaN Inf 0.4000 0.0283 0.0146 0.0019

From the above table, Jacobi method converges after 6 iterations with the maximal nal relative error less than 0.5%. b) Gauss-Seidel method: The iteration formulas of Gauss-Seidel method for this problem are listed as follows: 1 +1 n xn = (1 + 0.3xn 1 2 0.1x3 ) 5
+1 xn = 2

1 +1 (2 0.2xn + xn 1 3) 2 1 +1 +1 (0.3xn + 0.1xn ) 1 2 4

+1 xn = 3

The initial guess is x(0) = [0 0 0]T . The results of the iterations including the solutions and errors are listed as follows: Iterations 0 1 2 3 4 x1 0.0000 0.2000 0.2614 0.2620 0.2619 x2 0.0000 1.0200 1.0314 1.0293 1.0292 x3 0.0000 -0.0105 -0.0062 -0.0061 -0.0061 err1 N/A Inf 0.3071 0.0023 0.0005 err2 N/A Inf 0.0112 0.0020 0.0001 err3 N/A Inf 0.4115 0.0157 0.0013

From the above table, Gauss-Seidel method converges after 4 iterations with the maximal nal relative error less than 0.5%. 4. (20 points) (a) Create a program using MATLAB which implements a) Jacobi method, and b) GaussSeidel method. Terminate the algorithms for (m+1) (m) x xi | i| = | i |100% < 0.5, for all i = 1, 2, 3, 4. (m) xi Sample program: a) Jacobi method: clear all A=[5,.1,.3,-1;0.5,3,-.4,.1;.1,-.4,5,0;-.2,.1,0,5]; b=[-2,0,1,3]; n=4; eps0=5e-3; x(1,:)=zeros(1,n); ep(1,:)=NaN*ones(1,n); m=1;
4

termin=0; while ~termin m=m+1; % Jacobi Iteration for i=1:n x(m,i)=b(i); for j=1:n if j~=i x(m,i)=x(m,i)-A(i,j)*x(m-1,j); end end x(m,i)=x(m,i)/A(i,i); end % Calculate the error for i=1:n ep(m,i)=abs((x(m,i)-x(m-1,i))/x(m-1,i)); end % Termination judgement termin=1; for i=1:n termin=termin&(ep(m,i)<eps0); end; end x,ep Jacobi_loop_times=m-1 b) Gauss-Seidel method: clear all A=[5,.1,.3,-1;0.5,3,-.4,.1;.1,-.4,5,0;-.2,.1,0,5]; b=[-2,0,1,3]; n=4; eps0=5e-3; x(1,:)=zeros(1,n); ep(1,:)=NaN*ones(1,n); m=1; termin=0; while ~termin m=m+1; % Gauss-Seidel Iteration for i=1:n x(m,i)=b(i);
5

for j=1:n if j<i x(m,i)=x(m,i)-A(i,j)*x(m,j); elseif j>i x(m,i)=x(m,i)-A(i,j)*x(m-1,j); end end x(m,i)=x(m,i)/A(i,i); end % Calculate the error for i=1:n ep(m,i)=abs((x(m,i)-x(m-1,i))/x(m-1,i)); end % Termination judgement termin=1; for i=1:n termin=termin&(ep(m,i)<eps0); end; end x,ep GS_loop_times=m-1 (b) Test your program using the system: 5.0x1 0.5x1 0.1x1 0.2x1 + + + 0.1x2 3.0x2 0.4x2 0.1x2 + + + 0.3x3 0.4x3 5.0x3 0.0x3 + + + 1.0x4 0.1x4 0.0x4 5.0x4 = 2 = 0 = 1 = 3

Use as initial guess in both cases x(0) = [0 0 0 0]T . Solution: a) Jacobi method after 5 iterations (when the result meets the criterion of the error):

x=

0.2964 0.0579 0.2106 0.5870

b) Gauss-Seidel method after 4 iterations (when the result meets the criterion of the error):

x=

0.2964 0.0579 0.2106 0.5870

(c) Turn in a hardcopy listing of the program and printouts of the results and discuss the speed of convergence between these two methods.
6

MATLAB output: a) Jacobi method: x = 0 -0.4000 -0.2920 -0.2971 -0.2965 -0.2964 ep = NaN Inf 0.2700 0.0176 0.0023 0.0002 NaN NaN Inf 0.2236 0.0221 0.0048 NaN Inf 0.0400 0.0178 0.0057 0.0004 NaN Inf 0.0267 0.0049 0.0002 0.0000 0 0 0.0733 0.0569 0.0582 0.0579 0 0.2000 0.2080 0.2117 0.2105 0.2106 0 0.6000 0.5840 0.5869 0.5870 0.5870

Jacobi_loop_times = 5 b) Gauss-Seidel method: x = 0 -0.4000 -0.2976 -0.2964 -0.2964 ep = NaN Inf 0.2560 0.0039 0.0001 NaN Inf 0.1207 0.0119 0.0003 NaN Inf 0.0126 0.0004 0.0000 NaN Inf 0.0073 0.0001 0.0000 0 0.0667 0.0586 0.0579 0.0579 0 0.2133 0.2106 0.2106 0.2106 0 0.5827 0.5869 0.5870 0.5870

GS_loop_times = 4

You might also like