Example description
!   E04LYF Example Program Text
!   Mark 26.2 Release. NAG Copyright 2017.
    Module e04lyfe_mod

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

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: funct2, hess2
!     .. Parameters ..
      Integer, Parameter, Public       :: n = 4, nout = 6
      Integer, Parameter, Public       :: liw = n + 2
      Integer, Parameter, Public       :: lw = n*(n+7)
    Contains
      Subroutine funct2(n,xc,fc,gc,iuser,ruser)
!       Routine to evaluate objective function and its 1st derivatives.

!       .. 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

      End Subroutine funct2
      Subroutine hess2(n,xc,heslc,lh,hesdc,iuser,ruser)
!       Routine to evaluate 2nd derivatives.

!       .. Scalar Arguments ..
        Integer, Intent (In)           :: lh, n
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Out) :: hesdc(n), heslc(lh)
        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)
        hesdc(1) = 2.0_nag_wp + 120.0_nag_wp*(x1-x4)**2
        hesdc(2) = 200.0_nag_wp + 12.0_nag_wp*(x2-2.0_nag_wp*x3)**2
        hesdc(3) = 10.0_nag_wp + 48.0_nag_wp*(x2-2.0_nag_wp*x3)**2
        hesdc(4) = 10.0_nag_wp + 120.0_nag_wp*(x1-x4)**2
        heslc(1) = 20.0_nag_wp
        heslc(2) = 0.0_nag_wp
        heslc(3) = -24.0_nag_wp*(x2-2.0_nag_wp*x3)**2
        heslc(4) = -120.0_nag_wp*(x1-x4)**2
        heslc(5) = 0.0_nag_wp
        heslc(6) = -10.0_nag_wp

        Return

      End Subroutine hess2
    End Module e04lyfe_mod
    Program e04lyfe

!     E04LYF Example Main Program

!     .. Use Statements ..
      Use e04lyfe_mod, Only: funct2, hess2, liw, lw, n, nout
      Use nag_library, Only: e04lyf, nag_wp
!     .. 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,*) 'E04LYF Example Program Results'
      Flush (nout)

      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/)

!     Set up starting point

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

      ifail = -1
      Call e04lyf(n,ibound,funct2,hess2,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,99999) 'at the point', x(1:n)
        Write (nout,*) 'The corresponding (machine dependent) gradient is'
        Write (nout,99998) g(1:n)
      End Select

99999 Format (1X,A,4F9.4)
99998 Format (13X,4E12.4)
    End Program e04lyfe