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

NAG AD Library Introduction
Example description
!   C05QB_P0W_F Example Program Text
!   Mark 29.1 Release. NAG Copyright 2023.
    Module c05qb_p0w_fe_mod

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: fcn
!     .. Parameters ..
      Integer, Parameter, Public       :: n = 7, nout = 6
    Contains
      Subroutine fcn(ad_handle,n,x,fvec,iuser,ruser,iflag)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Integer, Intent (Inout)        :: iflag
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Out) :: fvec(n)
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (In) :: x(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Integer                        :: i
!       .. Executable Statements ..
        Do i = 1, n
          fvec(i) = (ruser(2)+ruser(3)*x(i))*x(i) - ruser(5)
        End Do
        Do i = 2, n
          fvec(i) = fvec(i) + ruser(1)*x(i-1)
        End Do
        Do i = 1, n - 1
          fvec(i) = fvec(i) + ruser(4)*x(i+1)
        End Do
!       Set iflag negative to terminate execution for any reason.
        iflag = 0
        Return
      End Subroutine fcn
    End Module c05qb_p0w_fe_mod
    Program c05qb_p0w_fe
!     C05QB_P0W_F Example Main Program

!     .. Use Statements ..
      Use c05qb_p0w_fe_mod, Only: fcn, n, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: c05qb_p0w_f
      Use nag_library, Only: nag_wp, x02ajf
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: xtol
      Integer                          :: i, ifail
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: fvec(:), x(:)
      Real (Kind=nag_wp)               :: ruser(5)
      Integer                          :: iuser(1)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: sqrt
!     .. Executable Statements ..

      Write (nout,*) 'C05QB_P0W_F Example Program Results'

      Allocate (fvec(n),x(n))

!     The following starting values provide a rough solution.
      x(1:n) = -1.0_nag_wp

      ruser(1) = -1.0_nag_wp
      ruser(2) = 3.0_nag_wp
      ruser(3) = -2.0_nag_wp
      ruser(4) = -2.0_nag_wp
      ruser(5) = -1.0_nag_wp

      xtol = sqrt(x02ajf())

      ifail = 0
      Call c05qb_p0w_f(ad_handle,fcn,n,x,fvec,xtol,iuser,ruser,ifail)

      Write (nout,*) 'Final approximate solution'
      Write (nout,99999)(x(i),i=1,n)
99999 Format (1X,3F12.4)

    End Program c05qb_p0w_fe