      SUBROUTINE SOL (N, NDIM, A, B, IP)
C-----------------------------------------------------------------------
C SOLUTION OF LINEAR SYSTEM, A*X = B .
C INPUT..
C   N = ORDER OF MATRIX.
C   NDIM = DECLARED DIMENSION OF ARRAY  A .
C   A = TRIANGULARIZED MATRIX OBTAINED FROM DEC.
C   B = RIGHT HAND SIDE VECTOR.
C   IP = PIVOT VECTOR OBTAINED FROM DEC.
C DO NOT USE IF DEC HAS SET IER .NE. 0.
C OUTPUT..
C   B = SOLUTION VECTOR, X .
C-----------------------------------------------------------------------
      INTEGER IP, N, NDIM
      INTEGER I, K, KB, KM1, KP1, M, NM1
      REAL A, B
      REAL T
      DIMENSION A(NDIM, N), B(N), IP(N)
C
      IF (N .EQ. 1) GO TO 50
      NM1 = N - 1
C  APPLY ROW PERMUTATIONS AND MULTIPLIERS TO B. ------------------------
      DO 20 K = 1,NM1
        KP1 = K + 1
        M = IP(K)
        T = B(M)
        B(M) = B(K)
        B(K) = T
        DO 10 I = KP1,N
 10       B(I) = B(I) + A(I,K)*T
 20     CONTINUE
C  BACK SOLVE. ---------------------------------------------------------
      DO 40 KB = 1,NM1
        KM1 = N - KB
        K = KM1 + 1
        B(K) = B(K)/A(K,K)
        T = -B(K)
        DO 30 I = 1,KM1
 30       B(I) = B(I) + A(I,K)*T
 40     CONTINUE
 50   B(1) = B(1)/A(1,1)
      RETURN
C-----------------------  END OF SUBROUTINE SOL  -----------------------
      END
