ccccccccccccccccccccccccc     Program 2.1     cccccccccccccccccccccccccc
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c                                                                      c
c Please Note:                                                         c
c                                                                      c
c (1) This computer program is part of the book, "An Introduction to   c
c     Computational Physics," written by Tao Pang and published and    c
c     copyrighted by Cambridge University Press in 1997.               c
c                                                                      c
c (2) No warranties, express or implied, are made for this program.    c
c                                                                      c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
      PROGRAM INTERPOLATION
C
C Main program for the Lagrange interpolation with the
C Aitken method.
C
      PARAMETER (N=5)
      DIMENSION XI(N),FI(N)
      DATA XI/0.0,0.5,1.0,1.5,2.0/,
     *     FI/1.0,0.938470,0.765198,0.511828,0.223891/
C
      X = 0.9
      CALL AITKEN (N,XI,FI,X,F,DF)
      WRITE (6, 999) X,F,DF
      STOP
  999 FORMAT (3F16.8)
      END
C
      SUBROUTINE AITKEN (N,XI,FI,X,F,DF)
C
C Subroutine performing the Lagrange interpolation with the
C Aitken method. F: interpolated value. DF: error estimated.
C
      PARAMETER (NMAX=21) 
      DIMENSION XI(N),FI(N),FT(NMAX)
C
      IF (N.GT.NMAX) STOP 'Dimension too large'
      DO     100 I = 1, N
        FT(I) = FI(I)
  100 CONTINUE
C
      DO     200 I = 1, N-1  
        DO   150 J = 1, N-I
          X1 = XI(J)
          X2 = XI(J+I)
          F1 = FT(J)
          F2 = FT(J+1)
          FT(J) = (X-X1)/(X2-X1)*F2+(X-X2)/(X1-X2)*F1
  150   CONTINUE
  200 CONTINUE
      F = FT(1) 
      DF = (ABS(F-F1)+ABS(F-F2))/2.0
      RETURN
      END
