% FIGURE 0.1.2
      function X = LINEQ(A,B,N)
%
%     ARGUMENT DESCRIPTIONS
%
%     A - (INPUT) A IS THE N BY N MATRIX IN THE LINEAR SYSTEM A*X=B.
%     X - (OUTPUT) X IS THE SOLUTION VECTOR OF LENGTH N.
%     B - (INPUT) B IS THE RIGHT HAND SIDE VECTOR OF LENGTH N.
%     N - (INPUT) N IS THE NUMBER OF EQUATIONS AND NUMBER OF UNKNOWNS
%         IN THE LINEAR SYSTEM.
%
%                              BEGIN FORWARD ELIMINATION
      for K=1:N-1
         IBIG = K;
         BIG = abs(A(K,K));
%                              FIND THE LARGEST POTENTIAL PIVOT
         for I=K+1:N
            if (abs(A(I,K)) > BIG)
               BIG = abs(A(I,K));
               IBIG = I;
            end
         end
         if (BIG == 0.0)
            error ('The matrix is singular')
         end
%                              SWITCH ROW K WITH THE ROW (IBIG) 
%                              CONTAINING THE LARGEST POTENTIAL PIVOT
         for J=K:N
            TEMP = A(IBIG,J);
            A(IBIG,J) = A(K,J);
            A(K,J) = TEMP;
         end
         TEMP = B(IBIG);
         B(IBIG) = B(K);
         B(K) = TEMP;
         for I=K+1:N
            AMUL = -A(I,K)/A(K,K);
            if (AMUL ~= 0.0)
%                              ADD AMUL TIMES ROW K TO ROW I
               for J=K:N
                  A(I,J) = A(I,J)+AMUL*A(K,J);
               end
               B(I) = B(I)+AMUL*B(K);
            end
         end
      end
      if (A(N,N) == 0.0)
         error ('The matrix is singular')
      end
%                              BEGIN BACK SUBSTITUTION
      X(N) = B(N)/A(N,N);
      for K=N-1:-1:1
         SUM = 0;
         for J=K+1:N
            SUM = SUM + A(K,J)*X(J);
         end
         X(K) = (B(K)-SUM)/A(K,K);
      end
