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

NAG AD Library Introduction
Example description
    Program s15ad_t1w_fe

!     S15AD_T1W_F Example Program Text
!     Mark 30.1 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: nagad_t1w_w_rtype, s15ad_t1w_f, x10aa_t1w_f,    &
                               x10ab_t1w_f, Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_t1w_w_rtype)         :: e, x
      Integer                          :: ifail
!     .. Executable Statements ..
      Write (nout,*) 'S15AD_T1W_F Example Program Results'

      x = -1._nag_wp

      Write (nout,*)
      Write (nout,*) '       x         erfc(x)    derfc/dx   Exact erfc'''

!     Create AD configuration data object
      ifail = 0
      Call x10aa_t1w_f(ad_handle,ifail)

      x%tangent = 1._nag_wp

      ifail = 0
      Call s15ad_t1w_f(ad_handle,x,e,ifail)

      Write (nout,99999) x%value, e%value, e%tangent, exact_dedx(x%value)

!     Remove computational data object
      Call x10ab_t1w_f(ad_handle,ifail)

99999 Format (1X,1P,4E12.3)
    Contains
      Function exact_dedx(x_val)
!       .. Use Statements ..
        Use nag_library, Only: x01aaf
!       .. Function Return Value ..
        Real (Kind=nag_wp)             :: exact_dedx
!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In) :: x_val
!       .. Intrinsic Procedures ..
        Intrinsic                      :: exp, sqrt
!       .. Executable Statements ..
        exact_dedx = (-2._nag_wp/sqrt(x01aaf(x_val)))*exp(-x_val**2)
        Return
      End Function exact_dedx
    End Program s15ad_t1w_fe