    MODULE nlp_ex03_mod
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Default Accessibility ..
      PUBLIC
      ! .. Intrinsic Functions ..
      INTRINSIC KIND, PRESENT
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)

    CONTAINS

      SUBROUTINE obj_fun(first_call,x,finish,obj_f,obj_grad)
        ! .. Implicit None Statement ..
        IMPLICIT NONE
        ! .. Scalar Arguments ..
        REAL (wp), INTENT (OUT) :: obj_f
        LOGICAL, INTENT (INOUT) :: finish
        LOGICAL, INTENT (IN) :: first_call
        ! .. Array Arguments ..
        REAL (wp), OPTIONAL, INTENT (INOUT) :: obj_grad(:)
        REAL (wp), INTENT (IN) :: x(:)
        ! .. Executable Statements ..

        obj_f = (x(1)-10.0_wp)**2 + 5.0_wp*(x(2)-12.0_wp)**2 + x(3)**4 + &
         3.0_wp*(x(4)-11.0_wp)**2 + 10.0_wp*x(5)**6 + 7.0_wp*x(6)**2 + &
         x(7)**4 - 4.0_wp*x(6)*x(7) - 10.0_wp*x(6) - 8.0_wp*x(7)

        IF (PRESENT(obj_grad)) THEN
          obj_grad(1) = 2.0_wp*(x(1)-10.0_wp)
          obj_grad(2) = 10.0_wp*(x(2)-12.0_wp)
          obj_grad(3) = 4.0_wp*x(3)**3
          obj_grad(4) = 6.0_wp*(x(4)-11.0_wp)
          obj_grad(5) = 60.0_wp*x(5)**5
          obj_grad(6) = 14.0_wp*x(6) - 4.0_wp*x(7) - 10.0_wp
          obj_grad(7) = 4.0_wp*x(7)**3 - 4.0_wp*x(6) - 8.0_wp
        END IF
      END SUBROUTINE obj_fun

      SUBROUTINE con_fun(first_call,x,finish,needc,con_f,con_jac)
        ! .. Implicit None Statement ..
        IMPLICIT NONE
        ! .. Scalar Arguments ..
        LOGICAL, INTENT (INOUT) :: finish
        LOGICAL, INTENT (IN) :: first_call
        ! .. Array Arguments ..
        INTEGER, INTENT (IN) :: needc(:)
        REAL (wp), INTENT (INOUT) :: con_f(:)
        REAL (wp), OPTIONAL, INTENT (INOUT) :: con_jac(:,:)
        REAL (wp), INTENT (IN) :: x(:)
        ! .. Executable Statements ..

        IF (needc(1)>0) con_f(1) = 2.0_wp*x(1)**2 + 3.0_wp*x(2)**4 + x(3) + &
         4.0_wp*x(4)**2 + 5.0_wp*x(5)

        IF (needc(2)>0) con_f(2) = 7.0_wp*x(1) + 3.0_wp*x(2) + &
         10.0_wp*x(3)**2 + x(4) - x(5)
        IF (needc(3)>0) con_f(3) = 23.0_wp*x(1) + x(2)**2 + 6.0_wp*x(6)**2 - &
         8.0_wp*x(7)
        IF (needc(4)>0) con_f(4) = -4.0_wp*x(1)**2 - x(2)**2 + &
         3.0_wp*x(1)*x(2) - 2.0_wp*x(3)**2 - 5.0_wp*x(6) + 11.0_wp*x(7)
        IF (PRESENT(con_jac)) THEN
          IF (needc(1)>0) THEN
            IF (first_call) THEN
              con_jac(1,3) = 1.0_wp
              con_jac(1,5) = 5.0_wp
              con_jac(1,6:7) = 0.0_wp
            END IF
            con_jac(1,1) = 4.0_wp*x(1)
            con_jac(1,2) = 12.0_wp*x(2)**3
            con_jac(1,4) = 8.0_wp*x(4)
          END IF
          IF (needc(2)>0) THEN
            IF (first_call) THEN
              con_jac(2,1) = 7.0_wp
              con_jac(2,2) = 3.0_wp
              con_jac(2,4) = 1.0_wp
              con_jac(2,5) = -1.0_wp
              con_jac(2,6:7) = 0.0_wp
            END IF
            con_jac(2,3) = 20.0_wp*x(3)
          END IF
          IF (needc(3)>0) THEN
            IF (first_call) THEN
              con_jac(3,1) = 23.0_wp
              con_jac(3,3:5) = 0.0_wp
              con_jac(3,7) = -8.0_wp
            END IF
            con_jac(3,2) = 2.0_wp*x(2)
            con_jac(3,6) = 12.0_wp*x(6)
          END IF
          IF (needc(4)>0) THEN
            IF (first_call) THEN
              con_jac(4,4:5) = 0.0_wp
              con_jac(4,6) = -5.0_wp
              con_jac(4,7) = 11.0_wp
            END IF
            con_jac(4,1) = -8.0_wp*x(1) + 3.0_wp*x(2)
            con_jac(4,2) = -2.0_wp*x(2) + 3.0_wp*x(1)
            con_jac(4,3) = -4.0_wp*x(3)
          END IF
        END IF
      END SUBROUTINE con_fun

    END MODULE nlp_ex03_mod

    PROGRAM nag_nlp_ex03

      ! Example Program Text for nag_nlp
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_nlp, ONLY : nag_nlp_sol
      USE nlp_ex03_mod, ONLY : con_fun, obj_fun, wp
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Parameters ..
      INTEGER, PARAMETER :: n = 7, num_nlin_con = 4
      ! .. Local Scalars ..
      REAL (wp) :: obj_f
      ! .. Local Arrays ..
      REAL (wp) :: nlin_lower(num_nlin_con), nlin_upper(num_nlin_con), x(n)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_nlp_ex03'

      READ (nag_std_in,*)          ! Skip heading in data file

      ! Read in problem data
      READ (nag_std_in,*) nlin_lower
      READ (nag_std_in,*) nlin_upper
      READ (nag_std_in,*) x

      ! Solve the problem

      CALL nag_nlp_sol(obj_fun,x,obj_f,num_nlin_con=num_nlin_con, &
       con_fun=con_fun,nlin_lower=nlin_lower,nlin_upper=nlin_upper)

    END PROGRAM nag_nlp_ex03
