!   E04FYF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.
    MODULE e04fyfe_mod

!      E04FYF 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 = 7*n + n*n + 2*m*n + 3*m + n*(n-1)/2
!      .. Local Arrays ..
       REAL (KIND=nag_wp)                  :: t(m,nt), y(m)
    CONTAINS
!dec$ attributes offload : mic :: lsfun1
       SUBROUTINE lsfun1(m,n,xc,fvec,iuser,ruser)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (IN)                :: m, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (OUT)    :: fvec(m)
          REAL (KIND=nag_wp), INTENT (INOUT)  :: ruser(*)
          REAL (KIND=nag_wp), INTENT (IN)     :: xc(n)
          INTEGER, INTENT (INOUT)             :: iuser(*)
!         .. Executable Statements ..
          fvec(1:m) = xc(1) + t(1:m,1)/(xc(2)*t(1:m,2)+xc(3)*t(1:m,3)) - &
             y(1:m)

          RETURN

!dec$ attributes offload : mic :: lsfun1
       END SUBROUTINE lsfun1
    END MODULE e04fyfe_mod
    PROGRAM e04fyfe

!      E04FYF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : e04fyf
       USE e04fyfe_mod, ONLY : lsfun1, 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,*) 'E04FYF 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.5_nag_wp, 1.0_nag_wp, 1.5_nag_wp/)

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

       SELECT CASE (ifail)
       CASE (0,2:)
          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 e04fyfe
