    MODULE nlp_ex02_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 = 3.0_wp*x(1) + 1.0E-6_wp*x(1)**3 + 2.0_wp*x(2) + &
         (2.0E-6_wp/3.0_wp)*x(2)**3

        IF (PRESENT(obj_grad)) THEN
          obj_grad(1) = 3.0_wp + 3.0E-6_wp*x(1)**2
          obj_grad(2) = 2.0_wp + 2.0E-6_wp*x(2)**2
          IF (first_call) obj_grad(3:4) = 0.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
        ! .. Intrinsic Functions ..
        INTRINSIC COS, SIN
        ! .. 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) = 1000.0_wp*(SIN(-x(3)-0.25_wp)+SIN(-x(4)- &
         0.25_wp)) - x(1)

        IF (needc(2)>0) con_f(2) = 1000.0_wp*(SIN(x(3)-0.25_wp)+SIN(x(3)-x(4)- &
         0.25_wp)) - x(2)
        IF (needc(3)>0) con_f(3) = 1000.0_wp*(SIN(x(4)-0.25_wp)+SIN(x(4)-x(3)- &
         0.25_wp))
        IF (PRESENT(con_jac)) THEN
          IF (needc(1)>0) THEN
            IF (first_call) THEN
              con_jac(1,1) = -1.0_wp
              con_jac(1,2) = 0.0_wp
            END IF
            con_jac(1,3) = -1000.0_wp*COS(-x(3)-0.25_wp)
            con_jac(1,4) = -1000.0_wp*COS(-x(4)-0.25_wp)
          END IF
          IF (needc(2)>0) THEN
            IF (first_call) THEN
              con_jac(2,1) = 0.0_wp
              con_jac(2,2) = -1.0_wp
            END IF
            con_jac(2,4) = -1000.0_wp*COS(x(3)-x(4)-0.25_wp)
            con_jac(2,3) = 1000.0_wp*COS(x(3)-0.25_wp) - con_jac(2,4)
          END IF
          IF (needc(3)>0) THEN
            IF (first_call) con_jac(3,1:2) = 0.0_wp
            con_jac(3,3) = -1000.0_wp*COS(x(4)-x(3)-0.25_wp)
            con_jac(3,4) = 1000.0_wp*COS(x(4)-0.25_wp) - con_jac(3,3)
          END IF
        END IF
      END SUBROUTINE con_fun

    END MODULE nlp_ex02_mod

    PROGRAM nag_nlp_ex02

      ! 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, nag_nlp_cntrl_init, &
       nag_nlp_cntrl_wp => nag_nlp_cntrl_dp
      USE nlp_ex02_mod, ONLY : con_fun, obj_fun, wp
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Parameters ..
      INTEGER, PARAMETER :: m = 2, n = 4, num_nlin_con = 3
      ! .. Local Scalars ..
      INTEGER :: i
      REAL (wp) :: obj_f
      TYPE (nag_nlp_cntrl_wp) :: control
      ! .. Local Arrays ..
      REAL (wp) :: a(m,n), lin_lower(m), nlin_lower(num_nlin_con), &
       nlin_upper(num_nlin_con), x(n), x_lower(n), x_upper(n)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_nlp_ex02'

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

      ! Read in problem data
      READ (nag_std_in,*) (a(i,:),i=1,m)
      READ (nag_std_in,*) lin_lower
      READ (nag_std_in,*) x_lower
      READ (nag_std_in,*) x_upper
      READ (nag_std_in,*) nlin_lower
      READ (nag_std_in,*) nlin_upper
      READ (nag_std_in,*) x

      ! Initialize control structure and set required control parameters

      CALL nag_nlp_cntrl_init(control)

      control%major_iter_lim = 25
      control%minor_iter_lim = 10
      control%step_limit = 5.0_wp
      control%initial_x = .TRUE.

      ! Solve the problem

      CALL nag_nlp_sol(obj_fun,x,obj_f,num_nlin_con=num_nlin_con, &
       con_fun=con_fun,x_lower=x_lower,x_upper=x_upper,nlin_lower=nlin_lower, &
       nlin_upper=nlin_upper,a=a,lin_lower=lin_lower,control=control)

    END PROGRAM nag_nlp_ex02
