NAG Library Manual, Mark 30
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
!   E04KZF Example Program Text
!   Mark 30.0 Release. NAG Copyright 2024.
    Module e04kzfe_mod

!     E04KZF 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
!     .. 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
    End Module e04kzfe_mod
    Program e04kzfe

!     E04KZF Example Main Program

!     .. Use Statements ..
      Use e04kzfe_mod, Only: funct2, liw, lw, n, nout
      Use nag_library, Only: e04kzf, 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,*) 'E04KZF Example Program Results'
      Flush (nout)

      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 e04kzf(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,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,4F12.4)
99998 Format (13X,4E12.4)
    End Program e04kzfe