!   C05AWF Example Program Text
!   Mark 26.1 Release. NAG Copyright 2016.

    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
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: 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 c05awfe_mod, Only: f, nout
      Use nag_library, Only: c05awf, nag_wp
!     .. 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