!   C05PBF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.

    Module c05pbfe_mod

!     C05PBF 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        :: 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                   :: n = 9, nout = 6
      Integer, Parameter                   :: ldfjac = n
    Contains
      Subroutine fcn(n,x,fvec,fjac,ldfjac,iflag)

!       .. Scalar Arguments ..
        Integer, Intent (Inout)              :: iflag
        Integer, Intent (In)                 :: ldfjac, n
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout)   :: fjac(ldfjac,n), fvec(n)
        Real (Kind=nag_wp), Intent (In)      :: x(n)
!       .. Local Scalars ..
        Integer                              :: k
!       .. Executable Statements ..
        If (iflag/=2) Then
          fvec(1:n) = (three-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)
        Else
          fjac(1:n,1:n) = 0.0_nag_wp

          fjac(1,1) = three - two*two*x(1)
          fjac(1,2) = -two
          Do k = 2, n - 1
            fjac(k,k) = three - two*two*x(k)
            fjac(k,k-1) = -one
            fjac(k,k+1) = -two
          End Do
          fjac(n,n-1) = -one
          fjac(n,n) = three - two*two*x(n)

        End If

        Return

      End Subroutine fcn
    End Module c05pbfe_mod
    Program c05pbfe

!     C05PBF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: c05pbf, dnrm2, nag_wp, x02ajf
      Use c05pbfe_mod, Only: fcn, ldfjac, n, nout, one
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)                   :: fnorm, xtol
      Integer                              :: ifail, j, lwa
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable      :: fjac(:,:), fvec(:), x(:)
      Real (Kind=nag_wp)                   :: wa(1)
!     .. Intrinsic Procedures ..
      Intrinsic                            :: sqrt
!     .. Executable Statements ..
      Write (nout,*) 'C05PBF Example Program Results'

      Allocate (fjac(ldfjac,n),fvec(n),x(n))

!     The following starting values provide a rough solution.

      x(1:n) = -one

      xtol = sqrt(x02ajf())

      ifail = -1
      Call c05pbf(fcn,n,x,fvec,fjac,ldfjac,xtol,wa,lwa,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,*) 'Final approximate solution'
        Write (nout,*)
        Write (nout,99998)(x(j),j=1,n)
      Case (2:)
        Write (nout,*)
        Write (nout,*) 'Approximate solution'
        Write (nout,*)
        Write (nout,99998)(x(j),j=1,n)
      End Select

99999 Format (1X,A,E12.4)
99998 Format (1X,3F12.4)
    End Program c05pbfe
