NAG Library Manual, Mark 28.3
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
!   E04YAF Example Program Text
!   Mark 28.3 Release. NAG Copyright 2022.

    Module e04yafe_mod

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

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: lsqfun
!     .. Parameters ..
      Integer, Parameter, Public       :: liw = 1, mdec = 15, ndec = 3,        &
                                          nin = 5, nout = 6
      Integer, Parameter, Public       :: ldfjac = mdec
      Integer, Parameter, Public       :: lw = 3*ndec + mdec + mdec*ndec
!     .. Local Arrays ..
      Real (Kind=nag_wp), Public, Save :: t(mdec,ndec), y(mdec)
    Contains
      Subroutine lsqfun(iflag,m,n,xc,fvec,fjac,ldfjac,iw,liw,w,lw)

!       Routine to evaluate the residuals and their 1st derivatives

!       .. Scalar Arguments ..
        Integer, Intent (Inout)        :: iflag
        Integer, Intent (In)           :: ldfjac, liw, lw, m, n
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: fjac(ldfjac,n), w(lw)
        Real (Kind=nag_wp), Intent (Out) :: fvec(m)
        Real (Kind=nag_wp), Intent (In) :: xc(n)
        Integer, Intent (Inout)        :: iw(liw)
!       .. 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)

          If (iflag/=1) Then
            fvec(i) = xc(1) + t(i,1)/denom - y(i)
          End If

          If (iflag/=0) Then
            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 If

        End Do

        Return

      End Subroutine lsqfun
    End Module e04yafe_mod
    Program e04yafe

!     E04YAF Example Main Program

!     .. Use Statements ..
      Use e04yafe_mod, Only: ldfjac, liw, lsqfun, lw, mdec, ndec, nin, nout,   &
                             t, y
      Use nag_library, Only: e04yaf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Integer                          :: i, ifail, m, n
!     .. Local Arrays ..
      Real (Kind=nag_wp)               :: fjac(ldfjac,ndec), fvec(mdec),       &
                                          w(lw), x(ndec)
      Integer                          :: iw(liw)
!     .. Executable Statements ..
      Write (nout,*) 'E04YAF Example Program Results'

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

      n = ndec
      m = mdec

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

      Do i = 1, m
        Read (nin,*) y(i), t(i,1:n)
      End Do

!     Set up an arbitrary point at which to check the 1st
!     derivatives

      x(1:n) = (/0.19E0_nag_wp,-1.34E0_nag_wp,0.88E0_nag_wp/)

      Write (nout,*)
      Write (nout,*) 'The test point is'
      Write (nout,99999) x(1:n)

      ifail = -1
      Call e04yaf(m,n,lsqfun,x,fvec,fjac,ldfjac,iw,liw,w,lw,ifail)

      If (ifail>=0 .And. ifail/=1) Then

        Select Case (ifail)
        Case (0)
          Write (nout,*)
          Write (nout,*) '1st derivatives are consistent with residual values'
        Case (2)
          Write (nout,*)
          Write (nout,*) 'Probable error in calculation of 1st derivatives'
        End Select

        Write (nout,*)
        Write (nout,*) 'At the test point, LSQFUN gives'
        Write (nout,*)
        Write (nout,*) '      Residuals                   1st derivatives'
        Write (nout,99998)(fvec(i),fjac(i,1:n),i=1,m)
      End If

99999 Format (1X,4F10.5)
99998 Format (1X,1P,4E15.3)
    End Program e04yafe