      function X = TRIPER(A,B,C,F,N)
%
%         SOLVE LINEAR SYSTEM WITH A PERIODIC, TRI-DIAGONAL MATRIX:
%
%                B(1)  C(1)                        A(1)
%                A(2)  B(2)  C(2)
%                      A(3)  B(3)  C(3)
%                             .     .     .
%
%                C(N)                        A(N)  B(N)    
%
%                              A = SUBDIAGONAL OF COEFFICIENT MATRIX
%                              B = MAIN DIAGONAL
%                              C = SUPERDIAGONAL                           
%                              D = LAST ROW
%                              E = LAST COLUMN
%                              F = RIGHT HAND SIDE VECTOR
%                              X = SOLUTION 
%
%                              COPY F ONTO X                            
      X = F;
      D = zeros(N,1);
      E = zeros(N,1);
      D(1) = C(N);
      E(1) = A(1);
%                              BEGIN FORWARD ELIMINATION
      for K=1:N-2
         if (B(K)==0.0) 
            error (' Zero pivot encountered')
            return
         end
         AMULA = -A(K+1)/B(K);
         AMULD = -D(K)/B(K);
         B(K+1) = B(K+1) + AMULA*C(K);
         B(N) = B(N) + AMULD*E(K);
         if (K < N-2) 
            E(K+1) = E(K+1) + AMULA*E(K);
            D(K+1) = D(K+1) + AMULD*C(K);
         else 
            C(K+1) = C(K+1) + AMULA*E(K);
            A(N) = A(N) + AMULD*C(K);
         end 
         X(K+1) = X(K+1) + AMULA*X(K);
         X(N) = X(N) + AMULD*X(K);
      end
      if (B(N-1) == 0.0) 
         error (' Zero pivot encountered')
         return
      end
      AMULA = -A(N)/B(N-1);
      B(N) = B(N) + AMULA*C(N-1);
      X(N) = X(N) + AMULA*X(N-1);
      if (B(N) == 0.0) 
         error (' Zero pivot encountered')
         return
      end
%                              BACK SUBSTITUTION                        
      X(N) = X(N)/B(N);                                                 
      X(N-1) = (X(N-1)-C(N-1)*X(N))/B(N-1);                              
      for K=N-2:-1:1                                                  
         X(K) = (X(K)-C(K)*X(K+1)-E(K)*X(N))/B(K);
      end
