!   D02JAF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE d02jafe_mod

!      D02JAF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: cf
       FUNCTION cf(j,x)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: cf
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x
          INTEGER, INTENT (IN)                :: j
!         .. Executable Statements ..
          IF (j==2) THEN
             cf = 0.0E0_nag_wp
          ELSE
             cf = 1.0E0_nag_wp
          END IF
          RETURN
!dec$ attributes offload : mic :: cf
       END FUNCTION cf

!dec$ attributes offload : mic :: bc
       SUBROUTINE bc(i,j,rhs)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: rhs
          INTEGER, INTENT (IN)                :: i
          INTEGER, INTENT (OUT)               :: j
!         .. Executable Statements ..
          rhs = 0.0E0_nag_wp
          IF (i==1) THEN
             j = 1
          ELSE
             j = -1
          END IF
          RETURN
!dec$ attributes offload : mic :: bc
       END SUBROUTINE bc
    END MODULE d02jafe_mod

    PROGRAM d02jafe

!      D02JAF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d02jaf, e02akf, nag_wp
       USE d02jafe_mod, ONLY : bc, cf, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: dx, x, x0, x1, y
       INTEGER                             :: i, ia1, ifail, k1, k1max, kp,    &
                                              kpmax, lw, m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: c(:), w(:)
       INTEGER, ALLOCATABLE                :: iw(:)
!      .. Intrinsic Functions ..
       INTRINSIC                              real
!      .. Executable Statements ..
       WRITE (nout,*) 'D02JAF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
!      n: order of the differential equation
!      k1: number of coefficients to be returned
!      kp: number of collocation points
       READ (nin,*) n, k1max, kpmax
       lw = 2*(kpmax+n)*(k1max+1) + 7*k1max
       ALLOCATE (iw(k1max),c(k1max),w(lw))
!      x0: left-hand boundary, x1: right-hand boundary.
       READ (nin,*) x0, x1
       WRITE (nout,*)
       WRITE (nout,*) ' KP  K1   Chebyshev coefficients'
       DO kp = 10, kpmax, 5
          DO k1 = 4, k1max, 2

!            ifail: behaviour on error exit   
!                   =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
             ifail = 0
             CALL d02jaf(n,cf,bc,x0,x1,k1,kp,c,w,lw,iw,ifail)

             WRITE (nout,99999) kp, k1, c(1:k1)
          END DO
       END DO
       k1 = 8
       m = 9
       ia1 = 1
       WRITE (nout,*)
       WRITE (nout,99998) 'Last computed solution evaluated at', m, &
          ' equally spaced points'
       WRITE (nout,*)
       WRITE (nout,*) '      X         Y'
       dx = (x1-x0)/real(m-1,kind=nag_wp)
       x = x0
       DO i = 1, m
          ifail = 0
          CALL e02akf(k1,x0,x1,c,ia1,k1max,x,y,ifail)

          WRITE (nout,99997) x, y
          x = x + dx
       END DO

99999  FORMAT (1X,2(I3,1X),8F8.4)
99998  FORMAT (1X,A,I5,A)
99997  FORMAT (1X,2F10.4)
    END PROGRAM d02jafe
