!   D01GBF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.

    Module d01gbfe_mod

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

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter                   :: lenwrk = 500, maxcls = 20000,    &
                                              ndim = 4, nout = 6
    Contains
      Function functn(ndim,x)

!       .. Function Return Value ..
        Real (Kind=nag_wp)                   :: functn
!       .. Scalar Arguments ..
        Integer, Intent (In)                 :: ndim
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (In)      :: x(ndim)
!       .. Intrinsic Procedures ..
        Intrinsic                            :: exp
!       .. Executable Statements ..
        functn = 4.0E0_nag_wp*x(1)*x(3)**2*exp(2.0E0_nag_wp*x(1)*x(3))/ &
          (1.0E0_nag_wp+x(2)+x(4))**2

        Return

      End Function functn
    End Module d01gbfe_mod
    Program d01gbfe

!     D01GBF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: d01gbf, nag_wp
      Use d01gbfe_mod, Only: functn, lenwrk, maxcls, ndim, nout
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)                   :: acc, eps, finest
      Integer                              :: ifail, mincls
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable      :: a(:), b(:), wrkstr(:)
!     .. Executable Statements ..
      Write (nout,*) 'D01GBF 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.01E0_nag_wp
      mincls = 1000

      ifail = -1
      Call d01gbf(ndim,a,b,mincls,maxcls,functn,eps,acc,lenwrk,wrkstr,finest, &
        ifail)

      Select Case (ifail)
      Case (0,2)
        Write (nout,*)
        Write (nout,99998) 'Requested accuracy    = ', eps
        Write (nout,99997) 'Estimated value       = ', finest
        Write (nout,99998) 'Estimated accuracy    = ', acc
        Write (nout,99999) 'Number of evaluations = ', mincls
      End Select

99999 Format (1X,A,I5)
99998 Format (1X,A,E13.2)
99997 Format (1X,A,F13.5)
    End Program d01gbfe
