NAG Library Manual, Mark 28.4
```!   C05RB_A1W_F Example Program Text
!   Mark 28.4 Release. NAG Copyright 2022.
Module c05rb_a1w_fe_mod

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

!     .. Use Statements ..
Use iso_c_binding, Only: c_ptr
Assignment (=), Operator (*)
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

!       .. Scalar Arguments ..
Type (c_ptr), Intent (Inout)   :: ad_handle
Integer, Intent (Inout)        :: iflag
Integer, Intent (In)           :: n
!       .. Array Arguments ..
Type (nagad_a1w_w_rtype), Intent (Inout) :: fjac(n,n), fvec(n),        &
ruser(*)
Type (nagad_a1w_w_rtype), Intent (In) :: x(n)
Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
Integer                        :: i, k
!       .. Executable Statements ..

If (iflag/=2) Then
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
Else
fjac(1:n,1:n) = 0.0_nag_wp
fjac(1,1) = ruser(2) + 2.0_nag_wp*ruser(3)*x(1)
fjac(1,2) = ruser(4)
Do k = 2, n - 1
fjac(k,k-1) = ruser(1)
fjac(k,k) = ruser(2) + 2.0_nag_wp*ruser(3)*x(k)
fjac(k,k+1) = ruser(4)
End Do
fjac(n,n-1) = ruser(1)
fjac(n,n) = ruser(2) + 2.0_nag_wp*ruser(3)*x(n)
End If
!       Set iflag negative to terminate execution for any reason.
iflag = 0

Return

End Subroutine fcn

End Module c05rb_a1w_fe_mod

Program c05rb_a1w_fe
!     C05RB_A1W_F Example Main Program

!     .. Use Statements ..
Use c05rb_a1w_fe_mod, Only: fcn, n, nout
Use iso_c_binding, Only: c_ptr
x10aa_a1w_f, x10ab_a1w_f, Assignment (=)
Use nag_library, Only: nag_wp, x02ajf, x04caf
!     .. Implicit None Statement ..
Implicit None
!     .. Local Scalars ..
Integer                          :: i, ifail
!     .. Local Arrays ..
Type (nagad_a1w_w_rtype), Allocatable :: fjac(:,:), fvec(:), x(:)
Real (Kind=nag_wp), Allocatable  :: dr(:,:)
Integer                          :: iuser(1)
!     .. Intrinsic Procedures ..
Intrinsic                        :: sqrt
!     .. Executable Statements ..

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

Allocate (fjac(n,n),fvec(n),x(n),dr(n,5))

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

!     Create AD configuration data object
ifail = 0

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
ruser(1:5)%tapeindex = 0

!     Register variables to differentiate w.r.t.

xtol = sqrt(x02ajf())

ifail = 0

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

!     Setup evaluation of derivatives via adjoints

Write (nout,*)
Write (nout,*) ' Derivatives calculated: First order adjoints'
Write (nout,*) ' Computational mode    : algorithmic'
Write (nout,*)
Write (nout,*) ' Derivatives are of solution w.r.t function params'
Write (nout,*)

Do i = 1, n
ifail = 0

!       Get derivatives