    PROGRAM e01bafe

!      E01BAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e01baf, e02bbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: m = 7, nout = 6
       INTEGER, PARAMETER              :: lck = m + 4
       INTEGER, PARAMETER              :: lwrk = 6*m + 16
       REAL (KIND=nag_wp), PARAMETER   :: x(m) = (/ 0.0E0_nag_wp,              &
                                          0.2E0_nag_wp, 0.4E0_nag_wp,          &
                                          0.6E0_nag_wp, 0.75E0_nag_wp,         &
                                          0.9E0_nag_wp, 1.0E0_nag_wp/)
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: fit, xarg
       INTEGER                         :: ifail, j, r
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: c(lck), lamda(lck), wrk(lwrk), y(m)
!      .. Intrinsic Functions ..
       INTRINSIC                          exp
!      .. Executable Statements ..
       WRITE (nout,*) 'E01BAF Example Program Results'

       y(1:m) = exp(x(1:m))

       ifail = 0
       CALL e01baf(m,x,y,lamda,c,lck,wrk,lwrk,ifail)

       WRITE (nout,*)
       WRITE (nout,*) '   J    Knot LAMDA(J+2)   B-spline coeff C(J)'
       WRITE (nout,*)

       j = 1
       WRITE (nout,99998) j, c(1)

       DO j = 2, m - 1
          WRITE (nout,99999) j, lamda(j+2), c(j)
       END DO

       WRITE (nout,99998) m, c(m)
       WRITE (nout,*)
       WRITE (nout,*) &
          '   R        Abscissa            Ordinate             Spline'
       WRITE (nout,*)

       DO r = 1, m

          ifail = 0
          CALL e02bbf(m+4,lamda,c,x(r),fit,ifail)

          WRITE (nout,99999) r, x(r), y(r), fit

          IF (r<m) THEN
             xarg = 0.5E0_nag_wp*(x(r)+x(r+1))

             ifail = 0
             CALL e02bbf(m+4,lamda,c,xarg,fit,ifail)

             WRITE (nout,99997) xarg, fit
          END IF

       END DO

99999  FORMAT (1X,I4,F15.4,2F20.4)
99998  FORMAT (1X,I4,F35.4)
99997  FORMAT (1X,F19.4,F40.4)
    END PROGRAM e01bafe
