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

    MODULE c05azfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: tolx = 1.0E-5_nag_wp
       INTEGER, PARAMETER                  :: ir = 0, 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 = exp(-x) - x

          RETURN

!dec$ attributes offload : mic :: f
       END FUNCTION f
    END MODULE c05azfe_mod
    PROGRAM c05azfe

!      C05AZF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : c05azf, nag_wp
       USE c05azfe_mod, ONLY : f, ir, nout, tolx
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: fx, x, y
       INTEGER                             :: ifail, ind
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: c(17)
!      .. Executable Statements ..
       WRITE (nout,*) 'C05AZF Example Program Results'

       WRITE (nout,*)
       WRITE (nout,*) ' Iterations'
       WRITE (nout,*)

!      Initial values, root in [0,1].
       x = 0.0_nag_wp
       y = 1.0_nag_wp
       ind = 1
       ifail = -1

!       Reverse communication loop
REVCOM: DO
          CALL c05azf(x,y,fx,tolx,ir,c,ind,ifail)

          IF (ind==0) THEN
             EXIT REVCOM
          END IF

          fx = f(x)
          WRITE (nout,99999) ' X =', x, '   FX =', fx, '   IND =', ind
       END DO REVCOM

!      Results
       SELECT CASE (ifail)
       CASE (0)
          WRITE (nout,*)
          WRITE (nout,*) ' Solution'
          WRITE (nout,*)
          WRITE (nout,99998) ' X =', x, '   Y =', y
       CASE (4,5)
          WRITE (nout,99998) 'X =', x, '  Y =', y
       END SELECT

99999  FORMAT (1X,A,F8.5,A,E12.4,A,I2)
99998  FORMAT (1X,2(A,F8.5))
    END PROGRAM c05azfe
