!   C05AWF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.

    Module c05awfe_mod

!     C05AWF 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
      Function f(x,iuser,ruser)

!       .. Function Return Value ..
        Real (Kind=nag_wp)                   :: f
!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In)      :: x
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout)   :: ruser(*)
        Integer, Intent (Inout)              :: iuser(*)
!       .. Intrinsic Procedures ..
        Intrinsic                            :: exp
!       .. Executable Statements ..
        f = exp(-x) - x

        Return

      End Function f
    End Module c05awfe_mod
    Program c05awfe

!     C05AWF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: c05awf, nag_wp
      Use c05awfe_mod, Only: f, nout
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)                   :: eps, eta, x
      Integer                              :: ifail, k, nfmax
!     .. Local Arrays ..
      Real (Kind=nag_wp)                   :: ruser(1)
      Integer                              :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'C05AWF Example Program Results'

      Write (nout,*)

!     Repeat with tolerance eps set to varying powers of 10:

loop: Do k = 3, 4
        eps = 10.0E0_nag_wp**(-k)
        x = 1.0E0_nag_wp
        eta = 0.0E0_nag_wp
        nfmax = 200

        ifail = -1
        Call c05awf(x,eps,eta,f,nfmax,iuser,ruser,ifail)

        Select Case (ifail)
        Case (0)
          Write (nout,99999) 'With EPS = ', eps, '   root = ', x
        Case (:-1)
          Exit loop
        Case (3,4)
          Write (nout,99999) 'With EPS = ', eps, ' final value = ', x
        End Select

      End Do loop

99999 Format (1X,A,E10.2,A,F14.5)
    End Program c05awfe
