!   E04GZF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04gzfe_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: m = 15, n = 3, nin = 5,          &
                                              nout = 6, nt = 3
       INTEGER, PARAMETER                  :: lw = 8*n + 2*n*n + 2*m*n + 3*m
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: t(m,nt), y(m)
    CONTAINS
!dec$ attributes offload : mic :: lsfun2
       SUBROUTINE lsfun2(m,n,xc,fvec,fjac,ldfjac,iuser,ruser)
!         Routine to evaluate the residuals and their 1st derivatives.

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: ldfjac, m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (INOUT)  :: fjac(ldfjac,n), ruser(*)
          REAL (KIND=nag_wp), INTENT (OUT)    :: fvec(m)
          REAL (KIND=nag_wp), INTENT (IN)     :: xc(n)
          INTEGER, INTENT (INOUT)             :: iuser(*)
!         .. Local Scalars ..
          REAL (KIND=nag_wp)                  :: denom, dummy
          INTEGER                             :: i
!         .. Executable Statements ..
          DO i = 1, m
             denom = xc(2)*t(i,2) + xc(3)*t(i,3)
             fvec(i) = xc(1) + t(i,1)/denom - y(i)
             fjac(i,1) = 1.0E0_nag_wp
             dummy = -1.0E0_nag_wp/(denom*denom)
             fjac(i,2) = t(i,1)*t(i,2)*dummy
             fjac(i,3) = t(i,1)*t(i,3)*dummy
          END DO

          RETURN

!dec$ attributes offload : mic :: lsfun2
       END SUBROUTINE lsfun2
    END MODULE e04gzfe_mod
    PROGRAM e04gzfe

!      E04GZF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04gzf
       USE e04gzfe_mod, ONLY : lsfun2, lw, m, n, nag_wp, nin, nout, nt, t, y
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: fsumsq
       INTEGER                             :: i, ifail
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: ruser(1), w(lw), x(n)
       INTEGER                             :: iuser(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'E04GZF Example Program Results'

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

!      Observations of TJ (J = 1, 2, ..., nt) are held in T(I, J)
!      (I = 1, 2, ..., m)

       DO i = 1, m
          READ (nin,*) y(i), t(i,1:nt)
       END DO

       x(1:nt) = (/ 0.5E0_nag_wp, 1.0E0_nag_wp, 1.5E0_nag_wp/)

       ifail = -1
       CALL e04gzf(m,n,lsfun2,x,fsumsq,w,lw,iuser,ruser,ifail)

       SELECT CASE (ifail)
       CASE (0,2:8,10:)
          WRITE (nout,*)
          WRITE (nout,99999) 'On exit, the sum of squares is', fsumsq
          WRITE (nout,99999) 'at the point', x(1:n)
       END SELECT

99999  FORMAT (1X,A,3F12.4)
    END PROGRAM e04gzfe
