!   E04FYF Example Program Text
!   Mark 24 Release. NAG Copyright 2012.
    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
      Subroutine lsfun1(m,n,xc,fvec,iuser,ruser)

!       .. 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

      End Subroutine lsfun1
    End Module e04fyfe_mod
    Program e04fyfe

!     E04FYF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: e04fyf, nag_wp
      Use e04fyfe_mod, Only: lsfun1, lw, m, n, 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
