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

    MODULE d02kafe_mod

!      Data for D02KAF example program

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: two = 2.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: zero = 0.0_nag_wp
       INTEGER, PARAMETER                  :: nin = 5, nout = 6, qq = 5
    CONTAINS
!dec$ attributes offload : mic :: coeffn
       SUBROUTINE coeffn(p,q,dqdl,x,elam,jint)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: dqdl, p, q
          REAL (KIND=nag_wp), INTENT (IN)     :: elam, x
          INTEGER, INTENT (IN)                :: jint
!         .. Intrinsic Functions ..
          INTRINSIC                              cos, real
!         .. Executable Statements ..
          p = one
          dqdl = one
          q = elam - two*real(qq,kind=nag_wp)*cos(two*x)
          RETURN
!dec$ attributes offload : mic :: coeffn
       END SUBROUTINE coeffn
!dec$ attributes offload : mic :: monit
       SUBROUTINE monit(nit,iflag,elam,finfo)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: elam
          INTEGER, INTENT (IN)                :: iflag, nit
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: finfo(15)
!         .. Executable Statements ..
          IF (nit==14) THEN
             WRITE (nout,*)
             WRITE (nout,*) 'Output from MONIT'
          END IF
          WRITE (nout,99999) nit, iflag, elam, finfo(1:4)
          RETURN

99999     FORMAT (1X,2I4,F10.3,2E12.2,2F8.1)
!dec$ attributes offload : mic :: monit
       END SUBROUTINE monit
    END MODULE d02kafe_mod
    PROGRAM d02kafe

!      D02KAF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d02kaf, d02kay, nag_wp
       USE d02kafe_mod, ONLY : coeffn, nin, nout, one, qq, zero
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: delam, delam1, elam, elam1, tol, &
                                              xl, xr
       INTEGER                             :: i, ifail, k
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: bcond(3,2)
!      .. Executable Statements ..
       WRITE (nout,*) 'D02KAF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
!      xl: left-hand end point,  xr: right-hand end point,
!      k: index of the required eigenvalue
!      elam1: initial estimate of the eigenvalue
!      delam1: initial search step
       READ (nin,*) xl, xr
       READ (nin,*) k
       READ (nin,*) elam1, delam1
       bcond(1,1:2) = one
       bcond(2,1:2) = zero
       DO i = 5, 6
          tol = 10.0_nag_wp**(-i)
          elam = elam1
          delam = delam1

!         ifail: behaviour on error exit   
!                =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
          ifail = 0

!         * To obtain monitoring information from the supplied
!         subroutine monit replace the name d02kay by monit in
!         the next statement and USE monit from d02kafe_mod *
          CALL d02kaf(xl,xr,coeffn,bcond,k,tol,elam,delam,d02kay,ifail)

          WRITE (nout,*)
          WRITE (nout,99999) 'Calculation with TOL =', tol
          WRITE (nout,*)
          WRITE (nout,*) ' Final results'
          WRITE (nout,*)
          WRITE (nout,99998) k, qq, elam, delam
          WRITE (nout,99997) bcond(3,1), bcond(3,2)
          WRITE (nout,*)
       END DO

99999  FORMAT (1X,A,E16.4)
99998  FORMAT (1X,' K =',I3,'  QQ =',I3,'  ELAM =',F12.3,'    DELAM =',E12.2)
99997  FORMAT (1X,' BCOND(3,1) =',E12.4,'    BCOND(3,2) =',E12.4)
    END PROGRAM d02kafe
