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

    MODULE d01fcfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: ndim = 4, nout = 6
       INTEGER, PARAMETER                  :: maxpts = 1000*ndim
       INTEGER, PARAMETER                  ::                                  &
                    lenwrk = (ndim+2)*(1+maxpts/(2**ndim+2*ndim*ndim+2*ndim+1))
    CONTAINS
!dec$ attributes offload : mic :: functn
       FUNCTION functn(ndim,z)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: functn
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: ndim
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: z(ndim)
!         .. Intrinsic Functions ..
          INTRINSIC                              exp
!         .. Executable Statements ..
          functn = 4.0E0_nag_wp*z(1)*z(3)*z(3)*exp(2.0E0_nag_wp*z(1)*z(3))/ &
             (1.0E0_nag_wp+z(2)+z(4))**2

          RETURN

!dec$ attributes offload : mic :: functn
       END FUNCTION functn
    END MODULE d01fcfe_mod
    PROGRAM d01fcfe

!      D01FCF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : d01fcf, nag_wp
       USE d01fcfe_mod, ONLY : functn, lenwrk, maxpts, ndim, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: acc, eps, finval
       INTEGER                             :: ifail, minpts
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: a(:), b(:), wrkstr(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'D01FCF Example Program Results'

       ALLOCATE (a(ndim),b(ndim),wrkstr(lenwrk))

       a(1:ndim) = 0.0E0_nag_wp
       b(1:ndim) = 1.0E0_nag_wp
       eps = 0.0001E0_nag_wp
       minpts = 0

       ifail = -1
       CALL d01fcf(ndim,a,b,minpts,maxpts,functn,eps,acc,lenwrk,wrkstr,finval, &
          ifail)

       SELECT CASE (ifail)
       CASE (0,2:)
          WRITE (nout,*)
          WRITE (nout,99999) 'Requested accuracy = ', eps
          WRITE (nout,99998) 'Estimated value    = ', finval
          WRITE (nout,99999) 'Estimated accuracy = ', acc
       END SELECT

99999  FORMAT (1X,A,E12.2)
99998  FORMAT (1X,A,F12.4)
    END PROGRAM d01fcfe
