Page 95 - Applied Numerical Methods Using MATLAB
P. 95

84    SYSTEM OF LINEAR EQUATIONS
                                                             
                  1   0  1 1        1    011           101      1
                  1   1  1 3    →   0    102       →   010      2       (2.2.17)
                                                             
                  1  −111           0   −100           000      2
           This ended up with an all-zero row except the nonzero RHS element, corre-
           sponding to the case of inconsistency. So we must declare the case of ‘no exact
           solution’ for this problem.
              The following MATLAB routine “gauss()” implements the Gauss elimination
           algorithm, and the program “do_gauss” is designed to solve Eq. (2.2.8) by using
           “gauss()”. Note that at every pivoting operation in the routine “gauss()”, the
           pivot row is divided by the pivot element so that every diagonal element becomes
           one and that we don’t need to perform any computation for the kth column at
           the kth stage, since the column is supposed to be all zeros but the kth element
             (k)
           a   = 1.
            kk

            function  x = gauss(A,B)
            %The sizes of matrices A,B are supposed to be NA x NA and NA x NB.
            %This function solves Ax=Bby Gauss elimination algorithm.
            NA = size(A,2);  [NB1,NB] = size(B);
            if NB1 ~= NA, error(’A and B must have compatible dimensions’); end
            N = NA + NB; AB = [A(1:NA,1:NA) B(1:NA,1:NB)]; % Augmented matrix
            epss = eps*ones(NA,1);
            for k = 1:NA
               %Scaled Partial Pivoting at AB(k,k) by Eq.(2.2.20)
               [akx,kx] = max(abs(AB(k:NA,k))./ ...
                         max(abs([AB(k:NA,k + 1:NA) epss(1:NA - k + 1)]’))’);
               if akx < eps, error(’Singular matrix and No unique solution’); end
               mx=k+kx - 1;
               if kx>1% Row change if necessary
                  tmp_row = AB(k,k:N);
                  AB(k,k:N) = AB(mx,k:N);
                  AB(mx,k:N) = tmp_row;
               end
               % Gauss forward elimination
               AB(k,k + 1:N) = AB(k,k+1:N)/AB(k,k);
               AB(k,k) = 1;  %make each diagonal element one
               form=k+ 1: NA
                  AB(m,k+1:N) = AB(m,k+1:N) - AB(m,k)*AB(k,k+1:N); %Eq.(2.2.5)
                  AB(m,k) = 0;
               end
            end
            %backward substitution for a upper-triangular matrix eqation
            % having all the diagonal elements equal to one
            x(NA,:) = AB(NA,NA+1:N);
            for m = NA-1: -1:1
               x(m,:) = AB(m,NA + 1:N)-AB(m,m + 1:NA)*x(m + 1:NA,:); %Eq.(2.2.7)
            end
            %do_gauss
            A=[011;2 -1 -1;11-1];b=[20 1]’; %Eq.(2.2.8)
            x = gauss(A,b)
            x1=A\b %for comparison with the result of backslash operation
   90   91   92   93   94   95   96   97   98   99   100