!   E02GBF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e02gbfe_mod

!      E02GBF 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, nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: monit
       SUBROUTINE monit(n,x,niter,k,el1n)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: el1n
          INTEGER, INTENT (IN)                :: k, n, niter
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x(n)
!         .. Executable Statements ..
          WRITE (nout,*)
          WRITE (nout,99999) 'Results at iteration ', niter
          WRITE (nout,*) 'X-values'
          WRITE (nout,99998) x
          WRITE (nout,99997) 'Norm of residuals =', el1n

          RETURN

99999     FORMAT (1X,A,I5)
99998     FORMAT (1X,4F15.4)
99997     FORMAT (1X,A,E12.5)
!dec$ attributes offload : mic :: monit
       END SUBROUTINE monit
    END MODULE e02gbfe_mod
    PROGRAM e02gbfe

!      E02GBF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e02gbf, nag_wp
       USE e02gbfe_mod, ONLY : monit, n, nin, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: el1n, t
       INTEGER                             :: i, ifail, iprint, iw, k, l, lde, &
                                              m, mpl, mxs
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: e(:,:), f(:), w(:), x(:)
       INTEGER, ALLOCATABLE                :: indx(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'E02GBF Example Program Results'

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

       READ (nin,*) m
       lde = n
       l = m
       mpl = m + l
       iw = 3*mpl + 5*n + n**2 + (n+1)*(n+2)/2
       ALLOCATE (e(lde,mpl),f(mpl),x(n),indx(mpl),w(iw))

       DO i = 1, m
          READ (nin,*) t, f(i)
          e(1:4,i) = (/ 1.0_nag_wp, t, t*t, t*t*t/)
          e(1:4,m+i) = (/ 0.0_nag_wp, 1.0_nag_wp, 2.0_nag_wp*t, &
             3.0_nag_wp*t*t/)
          f(m+i) = 0.0_nag_wp
       END DO

       x(1:n) = 0.0_nag_wp
       mxs = 50

!      * Set IPRINT=1 to obtain output from MONIT at each iteration *
       iprint = 0

       ifail = -1
       CALL e02gbf(m,n,m+l,e,lde,f,x,mxs,monit,iprint,k,el1n,indx,w,iw,ifail)

    END PROGRAM e02gbfe
