    PROGRAM c05axfe

!      C05AXF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : c05axf, nag_wp, x02ajf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: fx, scal, tol, x
       INTEGER                         :: i, ifail, ind, ir
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: c(26)
!      .. Intrinsic Functions ..
       INTRINSIC                          exp, sqrt
!      .. Executable Statements ..
       WRITE (nout,*) 'C05AXF Example Program Results'

       scal = sqrt(x02ajf())
       ir = 0

LOOP:  DO i = 3, 4
          tol = 10.0E0_nag_wp**(-i)
          WRITE (nout,*)
          WRITE (nout,99999) 'TOL =', tol
          WRITE (nout,*)
          x = 1.0E0_nag_wp
          ind = 1
          ifail = -1

REVCOMM:  DO
             CALL c05axf(x,fx,tol,ir,scal,c,ind,ifail)

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

             fx = x - exp(-x)
          END DO REVCOMM

          SELECT CASE (ifail)
          CASE (:-1)
             EXIT LOOP
          CASE (4,6)
             WRITE (nout,99998) 'Final value = ', x, ' THETA = ', c(5)
          CASE (0)
             WRITE (nout,99998) 'Root is ', x
          END SELECT

       END DO LOOP

99999  FORMAT (1X,A,E11.4)
99998  FORMAT (1X,A,F14.5,A,F10.2)
    END PROGRAM c05axfe
