!   C05NCF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE c05ncfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: epsfcn = 0.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: factor = 100.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: one = 1.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: three = 3.0_nag_wp
       REAL (KIND=nag_wp), PARAMETER       :: two = 2.0_nag_wp
       INTEGER, PARAMETER                  :: maxfev = 2000, ml = 1, mode = 2, &
                                              mu = 1, n = 9, nout = 6,         &
                                              nprint = 0
       INTEGER, PARAMETER                  :: nr = n*(n+1)/2
       INTEGER, PARAMETER                  :: ldfjac = n
    CONTAINS
!dec$ attributes offload : mic :: fcn
       SUBROUTINE fcn(n,x,fvec,iflag)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (INOUT)             :: iflag
          INTEGER, INTENT (IN)                :: n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (INOUT)  :: fvec(n)
          REAL (KIND=nag_wp), INTENT (IN)     :: x(n)
!         .. Executable Statements ..
          IF (iflag==0) THEN

!            Insert print statements here when NPRINT is positive.

             CONTINUE
          ELSE
             fvec(1:n) = three*x(1:n) - two*x(1:n)*x(1:n) + one
             fvec(2:n) = fvec(2:n) - x(1:(n-1))
             fvec(1:(n-1)) = fvec(1:(n-1)) - two*x(2:n)
          END IF

          RETURN

!dec$ attributes offload : mic :: fcn
       END SUBROUTINE fcn
    END MODULE c05ncfe_mod
    PROGRAM c05ncfe

!      C05NCF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : c05ncf, dnrm2, nag_wp, x02ajf
       USE c05ncfe_mod, ONLY : epsfcn, factor, fcn, ldfjac, maxfev, ml, mode,  &
                               mu, n, nout, nprint, nr, one
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: fnorm, xtol
       INTEGER                             :: ifail, j, lr, nfev
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: diag(:), fjac(:,:), fvec(:),     &
                                              qtf(:), r(:), x(:)
       REAL (KIND=nag_wp)                  :: w(1,1)
!      .. Intrinsic Functions ..
       INTRINSIC                              sqrt
!      .. Executable Statements ..
       WRITE (nout,*) 'C05NCF Example Program Results'

       ALLOCATE (diag(n),fjac(ldfjac,n),fvec(n),qtf(n),r(nr),x(n))

!      The following starting values provide a rough solution.

       x(1:n) = -one
       xtol = sqrt(x02ajf())
       diag(1:n) = one

       ifail = -1
       CALL c05ncf(fcn,n,x,fvec,xtol,maxfev,ml,mu,epsfcn,diag,mode,factor, &
          nprint,nfev,fjac,ldfjac,r,lr,qtf,w,ifail)

       SELECT CASE (ifail)
       CASE (0)
!         The NAG name equivalent of dnrm2 is f06ejf
          fnorm = dnrm2(n,fvec,1)
          WRITE (nout,*)
          WRITE (nout,99999) 'Final 2-norm of the residuals =', fnorm
          WRITE (nout,*)
          WRITE (nout,99998) 'Number of function evaluations =', nfev
          WRITE (nout,*)
          WRITE (nout,*) 'Final approximate solution'
          WRITE (nout,*)
          WRITE (nout,99997) (x(j),j=1,n)
       CASE (2:)
          WRITE (nout,*)
          WRITE (nout,*) 'Approximate solution'
          WRITE (nout,*)
          WRITE (nout,99997) (x(j),j=1,n)
       END SELECT

99999  FORMAT (1X,A,E12.4)
99998  FORMAT (1X,A,I10)
99997  FORMAT (1X,3F12.4)
    END PROGRAM c05ncfe
