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

    MODULE c05agfe_mod

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

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

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: f
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x
!         .. Intrinsic Functions ..
          INTRINSIC                              exp
!         .. Executable Statements ..
          f = x - exp(-x)

          RETURN

!dec$ attributes offload : mic :: f
       END FUNCTION f
    END MODULE c05agfe_mod
    PROGRAM c05agfe

!      C05AGF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : c05agf, nag_wp
       USE c05agfe_mod, ONLY : f, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: a, b, eps, eta, h, x
       INTEGER                             :: ifail
!      .. Executable Statements ..
       WRITE (nout,*) 'C05AGF Example Program Results'

       x = 1.0E0_nag_wp
       h = 0.1E0_nag_wp
       eps = 1.0E-5_nag_wp
       eta = 0.0E0_nag_wp

       ifail = -1
       CALL c05agf(x,h,eps,eta,f,a,b,ifail)

       WRITE (nout,*)

       SELECT CASE (ifail)
       CASE (0)
          WRITE (nout,99999) 'Root is ', x
          WRITE (nout,99998) 'Interval searched is [', a, ',', b, ']'
       CASE (3,4)
          WRITE (nout,99999) 'Final value = ', x
       END SELECT

99999  FORMAT (1X,A,F13.5)
99998  FORMAT (1X,A,2(F8.5,A))
    END PROGRAM c05agfe
