Download 9 Solved Problems on Gaussian Elimination - Assignment | CVEN 302 and more Assignments Civil Engineering in PDF only on Docsity! Solution Set for Assignment 2 Problem 1: Gaussian Elimination function x = GElarge(A,b,ptol) % GElarge Show steps in Gauss elimination and back substitution % For multiple values of b. % % Synopsis: x = GElarge(A,b) % x = GElarge(A,b,ptol) % % Input: A,b = coefficient matrix and set of right hand side vectors % ptol = (optional) tolerance for detection of zero pivot % Default: ptol = 50 * eps % % Output: x = solution vector, if solution exists if nargin<3, ptol = 50*eps; end [m,n] = size(A); if m~=n, error('A matrix needs to be square'); end [p,q] = size(b); if p~=n, error('b matrix is inconsistent with the A matrix'); end nb = n+q; Ab = [A b]; % Augmented system fprintf('\n Begin forward elimination with Augmented system;\n'); disp(Ab); % --- Elimination for i =1:n-1 pivot = Ab(i,i); if abs(pivot)<ptol, error('zero pivot encountered'); end for k=i+1:n Ab(k,i:nb) = Ab(k,i:nb) - (Ab(k,i)/pivot)*Ab(i,i:nb); end fprintf('\n After elimination in column %d with pivot = %f \n\n',i,pivot); disp(Ab); pause; end % --- Back substitution x = zeros(n,q); % Initializing the x matrix to zero for j=1:q nstep = n + j; x(n,j) = Ab(n,nstep) / Ab(n,n); for i= n-1:-1:1 x(i,j) = (Ab(i,nstep) - Ab(i,i+1:n)*x(i+1:n,j))/Ab(i,i); end end Program is enclosed in the problem set. Results for test matrices: (a) >> A = [2 1; 3 4] >> b =[ 3 1 2; 4 -5 -3] >> GElarge(A,b) Begin forward elimination with Augmented system; 2 1 3 1 2 3 4 4 -5 -3 After elimination in column 1 with pivot = 2.000000 2.0000 1.0000 3.0000 1.0000 2.0000 0 2.5000 -0.5000 -6.5000 -6.0000 ans = 1.6000 1.8000 2.2000 -0.2000 -2.6000 -2.4000 (b) >> GElarge(A,b2) Begin forward elimination with Augmented system; 2 1 1 0 3 4 0 1 After elimination in column 1 with pivot = 2.000000 2.0000 1.0000 1.0000 0 0 2.5000 -1.5000 1.0000 ans = 0.8000 -0.2000 -0.6000 0.4000 Mistake in the test set starting values should have been: Begin forward elimination with Augmented system; 2 1 1 0 6 7 0 1 After elimination in column 1 with pivot = 2.000000 2 1 1 0 0 4 -3 1 ans = 0.8750 -0.1250 -0.7500 0.2500 Program is enclosed in the problem set. Results for test matrices: (a) >> GJordan(A,b) Begin forward elimination with Augmented system; 2 1 3 1 2 3 4 4 -5 -3 After elimination in column 1 with pivot = 2.000000 1.0000 0.5000 1.5000 0.5000 1.0000 0 2.5000 -0.5000 -6.5000 -6.0000 After elimination in column 2 with pivot = 2.500000 1.0000 0 1.6000 1.8000 2.2000 0 1.0000 -0.2000 -2.6000 -2.4000 ans = 1.6000 1.8000 2.2000 -0.2000 -2.6000 -2.4000 (b) >> GJordan(A,b2) Begin forward elimination with Augmented system; 2 1 1 0 3 4 0 1 After elimination in column 1 with pivot = 2.000000 1.0000 0.5000 0.5000 0 0 2.5000 -1.5000 1.0000 After elimination in column 2 with pivot = 2.500000 1.0000 0 0.8000 -0.2000 0 1.0000 -0.6000 0.4000 ans = 0.8000 -0.2000 -0.6000 0.4000 ( c ) >> GJordan(A4,b4) Begin forward elimination with Augmented system; 10 3 2 1 2 3 4 8 -1 4 6 5 -3 6 11 5 9 -4 After elimination in column 1 with pivot = 10.000000 1.0000 0.3000 0.2000 0.1000 0.2000 0.3000 0 6.8000 -1.8000 3.6000 5.2000 3.8000 0 6.9000 11.6000 5.3000 9.6000 -3.1000 After elimination in column 2 with pivot = 6.800000 1.0000 0 0.2794 -0.0588 -0.0294 0.1324 0 1.0000 -0.2647 0.5294 0.7647 0.5588 0 0 13.4265 1.6471 4.3235 -6.9559 After elimination in column 3 with pivot = 13.426471 1.0000 0 0 -0.0931 -0.1194 0.2771 0 1.0000 0 0.5619 0.8499 0.4217 0 0 1.0000 0.1227 0.3220 -0.5181 ans = -0.0931 -0.1194 0.2771 0.5619 0.8499 0.4217 0.1227 0.3220 -0.5181 Problem 3: Quintdiagonal Problem function x =demoQuint(c,a,d,b,f,r) % demoQuint Solves A x = b where A is a tridiagonal matrix % % % Synopsis: x = demoQuint(c,a,d,b,f,r) % % Input: a = upper diagonal of Matrix A a(n)=0 % d = diagonal of Matrix A % b = lower diagonal of Matrix A b(1)=0 % f = lower diagonal of Martix A f(1)=0 and f(2)=0 % c = upper diagonal of Martix A c(n-1)=0 and c(n)=0 % r = right-hand side of equation % % Output: x = the results c a d b f r n = length(d); % --- First row modification of terms a(1) = a(1) / d(1); c(1) = c(1) / d(1); r(1) = r(1) / d(1); % --- Second row modification of the method denom = d(2) - b(2)*a(1); a(2) = (a(2) - b(2)*c(1)) / denom; c(2) = c(2) / denom; r(2) = (r(2) - b(2)*r(1)) / denom; % --- Row modification of the method for i=3:n b(i) = b(i) - f(i)*a(i-2); denom = d(i) - b(i)*a(i-1) - f(i)*c(i-2); if (denom == 0) error('zero in denominator'), end % -- prevents a zero division a(i) = ( a(i) - b(i)*c(i-1) ) / denom; c(i) = c(i) / denom; r(i) = ( r(i) - b(i)*r(i-1) - f(i)*r(i-2) ) / denom; end % --- First two answers from the back substitution x(n) = r(n); x(n-1) = r(n-1) - a(n-1)*x(n); % --- Back substitution to get answers for i=n-2:-1:1 x(i) = r(i) - a(i)*x(i+1) - c(i)*x(i+2); end Program is enclosed in the problem set. Results for test matrices: (a) >> Quint(c,a,d,b,f,r) a = -1 -1 -1 -1 0 c = -1 -1 -1 0 0 d = 5 5 5 5 5 b = 0 -1 -1 -1 -1 f = 0 0 -1 -1 -1 r = 0 0 0 0 10 ans = 0.1984 0.3070 0.6849 0.6519 2.2674 (b) >> Quint(c1,a1,d1,b1,f1,r1) a = 9 0 9 0 9 0 9 0 c = 25 25 25 25 25 25 0 0 d = -68 -68 -68 -68 -68 -68 -68 -68 b = 0 9 0 9 0 9 0 9 f = 0 0 25 25 25 25 25 25 r = -25.6667 -8.6933 -8.0000 -1.4400 -9.0000 -3.2400 -34.0000 -30.7600 ans = 0.7064 0.4071 0.7483 0.5052 0.8271 0.6400 0.9111 0.8082 Problem 6: Gauss- Jordan (a ) Problem 3.3 GJordan(A,b) Begin forward elimination with Augmented system; 10 -2 1 9 -2 10 -2 12 -2 -5 10 18 After elimination in column 1 with pivot = 10.000000 1.0000 -0.2000 0.1000 0.9000 The first step : Divide Row 1 by 10 0 9.6000 -1.8000 13.8000 Multiply Row 1 by 2 and add to Row 2 0 -5.4000 10.2000 19.8000 Multiply Row 1 by 2 and add to Row 3 After elimination in column 2 with pivot = 9.600000 1.0000 0 0.0625 1.1875 Multiply Row 2 by 0.2 and add to Row 1 0 1.0000 -0.1875 1.4375 The first step : Divide Row 2 by 9.6. 0 0 9.1875 27.5625 Multiply Row 2 by 5.4 and add to Row 3 After elimination in column 3 with pivot = 9.187500 1.0000 0 0 1.0000 Multiply Row 3 by – 0.0625 and add to Row 1 0 1.0000 0 2.0000 Multiply Row 2 by 0.1875 and add to Row 2 0 0 1.0000 3.0000 The first step: Divide Row 3 by 9.1875 The results are the last column of the matrix [1.0000, 2.0000 , 3.0000] T ( b ) Problem 3.4 GJordan(A2,b2) Begin forward elimination with Augmented system; -1 5 2 -2 2 3 1 7 3 2 1 3 After elimination in column 1 with pivot = -1.000000 1 -5 -2 2 The first step: Divide Row 1 by –1 0 13 5 3 Multiply Row 1 by –2 and add to Row 2 0 17 7 -3 Multiply Row 1 by –3 and add to Row 3 After elimination in column 2 with pivot = 13.000000 1.0000 0 -0.0769 3.1538 Multiply Row 2 by 5 and add to Row 1 0 1.0000 0.3846 0.2308 The first step is divide Row 2 by 13 0 0 0.4615 -6.9231 Multiply Row 2 by –17 and add to Row 3 After elimination in column 3 with pivot = 0.461538 1.0000 0 0 2.0000 Multiply Row 3 by 0.0769 and add to Row 1 0 1.0000 0 6.0000 Multiply Row 3 by - 0.3846 and add to Row 2 0 0 1.0000 -15.0000 The first step is divide Row 3 by 0.4615 The results are the last column of the matrix [ 2, 6, -15] T Problem 7: Gauss- Jordan ( a ) Problem 3.12 >> GEPivotdemo(A,b) Begin forward elimination with Augmented system; 1 2 3 1 2 4 10 -2 3 14 28 -8 First step is to pivot: Swap rows 1 and 3; new pivot = 3 After elimination in column 1 with pivot = 3.000000 3.0000 14.0000 28.0000 -8.0000 0 -5.3333 -8.6667 3.3333 Multiply Row 1 by (– 2 / 3) and add to Row 2 0 -2.6667 -6.3333 3.6667 Multiply Row 1 by (– 1 / 3) and add to Row 3 After elimination in column 2 with pivot = -5.333333 3.0000 14.0000 28.0000 -8.0000 0 -5.3333 -8.6667 3.3333 0 0 -2.0000 2.0000 Multiply Row 2 by ( – 2.6667 / 5.3333) and add to Row 3 ans = 2.0000 1.0000 Back substitute to get the final values. -1.0000 ( b ) Problem 3.13 >> GEPivotdemo(A2,b2) Begin forward elimination with Augmented system; 2 6 10 0 1 3 3 2 3 14 28 -8 Swap rows 1 and 3; new pivot = 3 After elimination in column 1 with pivot = 3.000000 3.0000 14.0000 28.0000 -8.0000 0 -1.6667 -6.3333 4.6667 Multiply Row 1 by ( – 1 /3 ) add to Row 2 0 -3.3333 -8.6667 5.3333 Multiply Row 1 by ( – 2 /3 ) add to Row 3 Swap rows 2 and 3; new pivot = -3.33333 After elimination in column 2 with pivot = -3.333333 3.0000 14.0000 28.0000 -8.0000 0 -3.3333 -8.6667 5.3333 0 0 -2.0000 2.0000 Multiply Row 2 by ( – 1.6667 / 3.3333 ) add to Row 3 ans = 2.0000 1.0000 Back substitute to get the results: -1.0000 Problem 8: Thomas method ( a ) Problem 3.26 a = 2 3 0 d = 1 3 10 b = 0 1 3 r = 10 17 22 Sweep down : Step 1: denom = d(1) = 1 = 1 a(1) = a(1)/denom = 2/1 r(1) = r(1)/denom = 10/1 i = 1 denom = 1.00000000 a(1) = 2.00000000 r(1) = 10.00000000 Step 2: denom = d(2) - b(2)*a(1) = 3 – 2*1 = 1 a(2) = a(2)/denom = 3/1 = 3 r(2) = ( r(2)-b(2)*r(1))/denom = (17-1*10)/1 = 7 i = 2 denom = 1.00000000 a(2) = 3.00000000 r(2) = 7.00000000 Step 3: denom = d(3) - b(3)*a(2) = 10 – 3*3 = 1 a(3) = a(3)/denom = 0/1 = 0 r(3) = ( r(3)-b(3)*r(2))/denom = (22-3*7)/1 = 1 i = 3 denom = 1.00000000 a(3) = 0.00000000 r(3) = 1.00000000 Sweep Back Step 1: x(3) = r(3) = 1 i = 3 r(3) = 1.00000000 a(3) = 0.00000000 x(3) = 1.00000000 Step 2: x(2) = r(2) - a(2)*x(3) = 7 – 3*1 = 4 i = 2 r(2) = 7.00000000 a(2) = 3.00000000 x(2) = 4.00000000 Step 3: x(1) = r(1) - a(1)*x(2) = 10 – 2*4 = 2 i = 1 r(1) = 10.00000000 a(1) = 2.00000000 x(1) = 2.00000000 Final results are x = [2.0000, 4.0000, 1.0000] (2) Problem 3.25 (a)Gauss Elimination Begin forward elimination with Augmented system; 600.0 -220.0 300.0 2000.0 -300.0 100.0 -110.0 - 810.0 -1.0 -3.0 0.9 6.0 After elimination in column 1 with pivot = 600.000000 600.0 -220.0 300.0 2000.0 0 -10.0 40.0 190.0 0 -3.4 1.4 9.3 After elimination in column 2 with pivot = -10.000000 600.0 -220.0 300.0 2000.0 0 -10.0 40.0 190.0 0 0 -12.1 -54.6 ans = 0.74 -0.89 4.53 b)Gauss Pivot Begin forward elimination with Augmented system; 600.0 -220.0 300.0 2000.0 -300.0 100.0 -110.0 - 810.0 -1.0 -3.0 0.9 6.0 After elimination in column 1 with pivot = 600.000000 600.0 -220.0 300.0 2000.0 0 -10.0 40.0 190.0 0 -3.4 1.4 9.3 After elimination in column 2 with pivot = -10.000000 600.0 -220.0 300.0 2000.0 0 -10.0 40.0 190.0 0 0 -12.1 -54.6 ans = 0.74 -0.89 4.53 ( c ) Scaling Begin forward elimination with Augmented system; 1.0000 -0.3700 0.5000 3.3000 1.0000 -0.3300 0.3700 2.7000 0.3300 1.0000 -0.3000 -2.0000 After elimination in column 1 with pivot = 1.00 1.0000 -0.37 0.50 3.30 0 0.04 -0.13 -0.60 0 1.12 -0.47 -3.09 Swap rows 2 and 3; new pivot = 1.12 After elimination in column 2 with pivot = 1.12 1.0000 -0.37 0.50 3.3000 0 1.12 -0.47 -3.09 0 0 - 0.11 -0.49 ans = 0.75 -0.89 4.45 The round-off error kills the solution of the problem.