NAG Library Manual, Mark 28.6
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
!   C05AU_P0W_F Example Program Text
!   Mark 28.6 Release. NAG Copyright 2022.
    Module c05au_p0w_fe_mod

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp), Public, Save :: t
    Contains
      Subroutine f(ad_handle,x,z,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Real (Kind=nag_wp), Intent (In) :: x
        Real (Kind=nag_wp), Intent (Out) :: z
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Intrinsic Procedures ..
        Intrinsic                      :: exp
!       .. Executable Statements ..
        z = x - exp(-t*x)

        Return

      End Subroutine f
    End Module c05au_p0w_fe_mod

    Program c05au_p0w_fe

!     C05AU_P0W_F Example Main Program

!     .. Use Statements ..
      Use c05au_p0w_fe_mod, Only: f, nout, t
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: c05au_p0w_f
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: a, b, eps, eta, h, x
      Integer                          :: ifail
!     .. Local Arrays ..
      Real (Kind=nag_wp)               :: ruser(1)
      Integer                          :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'C05AU_P0W_F Example Program Results'

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

      ifail = -1
      Call c05au_p0w_f(ad_handle,x,h,eps,eta,f,a,b,iuser,ruser,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
      Case Default
        Write (nout,99997) 'The routine has failed with ifail = ', ifail
        Go To 100
      End Select

100   Continue


99999 Format (1X,A,F13.5)
99998 Format (1X,A,2(F8.5,A))
99997 Format (1X,A,I0)

    End Program c05au_p0w_fe