    PROGRAM e02cbfe

!      E02CBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e02cbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: xmax, xmin, y, ymax, ymin
       INTEGER                         :: i, ifail, j, k, l, m, m1, m2,        &
                                          mfirst, mlast, na, nwork
       LOGICAL                         :: plot
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), ff(:), work(:), x(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          min, real
!      .. Executable Statements ..
!      Skip heading in data file
       READ (nin,*)

       READ (nin,*) plot

       IF ( .NOT. plot) THEN
          WRITE (nout,*) 'E02CBF Example Program Results'
       END IF

       READ (nin,*) k, l, m

       IF (plot) THEN
          m1 = 1
          m2 = m
          mlast = m
       ELSE
          m1 = (2*m+3)/7
          m2 = (6*m+3)/7 + 1
          mlast = min(5,m)
       END IF

       na = (k+1)*(l+1)
       nwork = k + 1
       ALLOCATE (x(mlast),ff(mlast),a(na),work(nwork))

       READ (nin,*) a(1:na)
       READ (nin,*) ymin, ymax, xmin, xmax

       x(1:mlast) = xmin + (xmax-xmin)*real((/(j-1,j=1,mlast)/),kind=nag_wp)/ &
          real(mlast-1,kind=nag_wp)
       mfirst = 1

       DO i = m1, m2, m1
          y = ymin + ((ymax-ymin)*real(i-1,kind=nag_wp))/real(m-1,kind=nag_wp)

          ifail = 0
          CALL e02cbf(mfirst,mlast,k,l,x,xmin,xmax,y,ymin,ymax,ff,a,na,work, &
             nwork,ifail)

          IF (plot) THEN

             DO j = 1, mlast
                WRITE (nout,99998) y, x(j), ff(j)
             END DO

             WRITE (nout,*)
          ELSE
             WRITE (nout,*)
             WRITE (nout,99999) 'Y = ', y
             WRITE (nout,*)
             WRITE (nout,*) '  I     X(I)      Poly(X(I),Y)'

             DO j = 1, mlast
                WRITE (nout,99997) j, x(j), ff(j)
             END DO

          END IF

       END DO

99999  FORMAT (1X,A,E13.4)
99998  FORMAT (1X,1P,2E13.4,1P,2E13.4)
99997  FORMAT (1X,I3,1P,2E13.4)
    END PROGRAM e02cbfe
