!   D04AAF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE d04aafe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: h_init = 0.5_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: h_reduce = 0.1_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: xval = 0.5_nag_wp
       INTEGER, PARAMETER                  :: nder = -7, nout = 6
!   nder: abs(nder) is largest order derivative required;
!          nder < 0 means only odd or even derivatives.
!   h_init:   initial step size.
!   h_reduce: reduction factor applied to successive step sizes.
!   xval:     derivatives evaluated at x=xval.
    CONTAINS
!dec$ attributes offload : mic :: fun
       FUNCTION fun(x)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: fun
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x
!         .. Intrinsic Functions ..
          INTRINSIC                              exp
!         .. Executable Statements ..
          fun = 0.5_nag_wp*exp(2.0_nag_wp*x-1.0_nag_wp)

          RETURN

!dec$ attributes offload : mic :: fun
       END FUNCTION fun
    END MODULE d04aafe_mod
    PROGRAM d04aafe

!      D04AAF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d04aaf, nag_wp
       USE d04aafe_mod, ONLY : fun, h_init, h_reduce, nder, nout, xval
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: hbase
       INTEGER                             :: i, ifail, j, k, l
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: der(14), erest(14)
!      .. Intrinsic Functions ..
       INTRINSIC                              abs
!      .. Executable Statements ..
       WRITE (nout,*) 'D04AAF Example Program Results'

       WRITE (nout,*)
       WRITE (nout,*) 'Four separate runs to calculate the first &
          &four odd order derivatives of'
       WRITE (nout,*) '   FUN(X) = 0.5*exp(2.0*X-1.0) at X = 0.5.'
       WRITE (nout,*) 'The exact results are 1, 4, 16 and 64'
       WRITE (nout,*)
       WRITE (nout,*) 'Input parameters common to all four runs'
       WRITE (nout,99999) '  XVAL = ', xval, '    NDER = ', nder, &
          '    IFAIL = 0'
       WRITE (nout,*)

       hbase = h_init
       l = abs(nder)

       IF (nder>=0) THEN
          j = 1
       ELSE
          j = 2
       END IF

       DO k = 1, 4

          ifail = 0
          CALL d04aaf(xval,nder,hbase,der,erest,fun,ifail)

          WRITE (nout,*)
          WRITE (nout,99998) 'with step length', hbase, '  the results are'
          WRITE (nout,*) 'Order        Derivative       Error estimate'

          DO i = 1, l, j
             WRITE (nout,99997) i, der(i), erest(i)
          END DO

          hbase = hbase*h_reduce
       END DO

99999  FORMAT (1X,A,F4.1,A,I2,A)
99998  FORMAT (1X,A,F9.4,A)
99997  FORMAT (1X,I2,2E21.4)
    END PROGRAM d04aafe
