% FIGURE 0.4.6
      function X = TRI(A,B,C,F,N)
%
%     ARGUMENT DESCRIPTIONS
%
%     A,B,C - (INPUT) A,B,C ARE VECTORS OF LENGTHS N HOLDING THE
%             SUBDIAGONAL, DIAGONAL AND SUPERDIAGONAL OF THE TRIDIAGONAL
%             MATRIX IN THE LINEAR SYSTEM.
%     X -     (OUTPUT) X IS THE SOLUTION VECTOR OF LENGTH N.
%     F -     (INPUT) F IS THE RIGHT HAND SIDE VECTOR OF LENGTH N.
%     N -     (INPUT) N IS THE NUMBER OF EQUATIONS AND NUMBER OF UNKNOWNS.
%
      for I=1:N
         D(I) = 0.0;
      end
%                              BEGIN FORWARD ELIMINATION
      for K=1:N-1
         if (abs(A(K+1)) > abs(B(K)))
%                              SWITCH ROWS K AND K+1
            TEMP = B(K);
            B(K) = A(K+1);
            A(K+1) = TEMP;
            TEMP = C(K);
            C(K) = B(K+1);
            B(K+1) = TEMP;
            TEMP = F(K);
            F(K) = F(K+1);
            F(K+1) = TEMP;
            if (K < N-1)
               TEMP = D(K);
               D(K) = C(K+1);
               C(K+1) = TEMP;
            end
         end
         if (B(K) == 0.0)
            error ('The matrix is singular')
         end
         AMUL = -A(K+1)/B(K);
         B(K+1) = B(K+1)+AMUL*C(K);
         F(K+1) = F(K+1)+AMUL*F(K);
         if (K < N-1)
            C(K+1) = C(K+1)+AMUL*D(K);
         end
      end
%                              BACK SUBSTITUTION
      if (B(N) == 0.0)
         error ('The matrix is singular')
      end
      X(N) = F(N)/B(N);
      X(N-1) = (F(N-1)-C(N-1)*X(N))/B(N-1);
      for K=N-2:-1:1
         X(K) = (F(K)-C(K)*X(K+1)-D(K)*X(K+2))/B(K);
      end
