!   E04KYF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04kyfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: n = 4, nout = 6
       INTEGER, PARAMETER                  :: liw = n + 2
       INTEGER, PARAMETER                  :: lw = 10*n + n*(n-1)/2
    CONTAINS
!dec$ attributes offload : mic :: funct2
       SUBROUTINE funct2(n,xc,fc,gc,iuser,ruser)
!         Routine to evaluate objective function and its 1st derivatives.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: fc
          INTEGER, INTENT (IN)                :: n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: gc(n)
          REAL (KIND=nag_wp), INTENT (INOUT)  :: ruser(*)
          REAL (KIND=nag_wp), INTENT (IN)     :: xc(n)
          INTEGER, INTENT (INOUT)             :: iuser(*)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: x1, x2, x3, x4
!         .. Executable Statements ..
          x1 = xc(1)
          x2 = xc(2)
          x3 = xc(3)
          x4 = xc(4)
          fc = (x1+10.0_nag_wp*x2)**2 + 5.0_nag_wp*(x3-x4)**2 + &
             (x2-2.0_nag_wp*x3)**4 + 10.0_nag_wp*(x1-x4)**4
          gc(1) = 2.0_nag_wp*(x1+10.0_nag_wp*x2) + 40.0_nag_wp*(x1-x4)**3
          gc(2) = 20.0_nag_wp*(x1+10.0_nag_wp*x2) + 4.0_nag_wp*(x2-2.0_nag_wp* &
             x3)**3
          gc(3) = 10.0_nag_wp*(x3-x4) - 8.0_nag_wp*(x2-2.0_nag_wp*x3)**3
          gc(4) = -10.0_nag_wp*(x3-x4) - 40.0_nag_wp*(x1-x4)**3

          RETURN

!dec$ attributes offload : mic :: funct2
       END SUBROUTINE funct2
    END MODULE e04kyfe_mod
    PROGRAM e04kyfe

!      E04KYF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04kyf
       USE e04kyfe_mod, ONLY : funct2, liw, lw, n, nag_wp, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: f
       INTEGER                             :: ibound, ifail
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: bl(n), bu(n), g(n), ruser(1),    &
                                              w(lw), x(n)
       INTEGER                             :: iuser(1), iw(liw)
!      .. Executable Statements ..
       WRITE (nout,*) 'E04KYF Example Program Results'

       x(1:n) = (/ 3.0_nag_wp, -1.0_nag_wp, 0.0_nag_wp, 1.0_nag_wp/)
       ibound = 0

!      X(3) is unconstrained, so we set BL(3) to a large negative
!      number and BU(3) to a large positive number.

       bl(1:n) = (/ 1.0_nag_wp, -2.0_nag_wp, -1.0E6_nag_wp, 1.0_nag_wp/)
       bu(1:n) = (/ 3.0_nag_wp, 0.0_nag_wp, 1.0E6_nag_wp, 3.0_nag_wp/)

       ifail = -1
       CALL e04kyf(n,ibound,funct2,bl,bu,x,f,g,iw,liw,w,lw,iuser,ruser,ifail)

       SELECT CASE (ifail)
       CASE (0,2:)
          WRITE (nout,*)
          WRITE (nout,99999) 'Function value on exit is ', f
          WRITE (nout,99998) 'at the point', x(1:n)
          WRITE (nout,*) 'The corresponding (machine dependent) gradient is'
          WRITE (nout,99997) g(1:n)
       END SELECT

99999  FORMAT (1X,A,F9.4)
99998  FORMAT (1X,A,4F9.4)
99997  FORMAT (13X,4E12.4)
    END PROGRAM e04kyfe
