! E04LYF Example Program Text ! Mark 23 Release. NAG Copyright 2011. 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 ! .. Parameters .. INTEGER, PARAMETER :: n = 4, nout = 6 INTEGER, PARAMETER :: liw = n + 2 INTEGER, PARAMETER :: lw = n*(n+7) CONTAINS 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 END SUBROUTINE funct2 SUBROUTINE hess2(n,xc,heslc,lh,hesdc,iuser,ruser) ! Routine to evaluate 2nd derivatives. ! .. Implicit None Statement .. IMPLICIT NONE ! .. 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 nag_library, ONLY : e04lyf USE e04lyfe_mod, ONLY : funct2, hess2, 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,*) '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