this subroutine checks the gradients of m nonlinear functions in n variables, evaluated at a point x, for consistency with the functions themselves.
the subroutine does not perform reliably if cancellation or rounding errors cause a severe loss of significance in the evaluation of a function. therefore, none of the components of x should be unusually small (in particular, zero) or any other value which may cause loss of significance.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | m | a positive integer input variable set to the number of functions. |
||
integer, | intent(in) | :: | n | a positive integer input variable set to the number of variables. |
||
real(kind=wp), | intent(in) | :: | x(n) | input array |
||
real(kind=wp), | intent(in) | :: | Fvec(m) | an array of length m. on input when mode = 2, fvec must contain the functions evaluated at x. |
||
real(kind=wp), | intent(in) | :: | Fjac(Ldfjac,n) | an m by n array. on input when mode = 2, the rows of fjac must contain the gradients of the respective functions evaluated at x. |
||
integer, | intent(in) | :: | Ldfjac | a positive integer input parameter not less than m which specifies the leading dimension of the array fjac. |
||
real(kind=wp), | intent(out) | :: | Xp(n) | an array of length n. on output when mode = 1, xp is set to a neighboring point of x. |
||
real(kind=wp), | intent(in) | :: | Fvecp(m) | an array of length m. on input when mode = 2, fvecp must contain the functions evaluated at xp. |
||
integer, | intent(in) | :: | Mode | an integer input variable set to 1 on the first call and 2 on the second. other values of mode are equivalent to mode = 1. the user must call chkder twice, first with mode = 1 and then with mode = 2.
|
||
real(kind=wp), | intent(out) | :: | Err(m) | an array of length m. on output when mode = 2, err contains measures of correctness of the respective gradients. if there is no severe loss of significance, then if err(i) is 1.0 the i-th gradient is correct, while if err(i) is 0.0 the i-th gradient is incorrect. for values of err between 0.0 and 1.0, the categorization is less certain. in general, a value of err(i) greater than 0.5 indicates that the i-th gradient is probably correct, while a value of err(i) less than 0.5 indicates that the i-th gradient is probably incorrect. |
subroutine chkder(m, n, x, Fvec, Fjac, Ldfjac, Xp, Fvecp, Mode, Err)
implicit none
integer, intent(in) :: m !! a positive integer input variable set to the number
!! of functions.
integer, intent(in) :: n !! a positive integer input variable set to the number
!! of variables.
integer, intent(in) :: Ldfjac !! a positive integer input parameter not less than m
!! which specifies the leading dimension of the array fjac.
integer, intent(in) :: Mode !! an integer input variable set to 1 on the first call
!! and 2 on the second. other values of mode are equivalent
!! to mode = 1.
!!
!! the user must call chkder twice,
!! first with mode = 1 and then with mode = 2.
!!
!! * mode = 1. **on input**, x must contain the point of evaluation.
!! **on output**, xp is set to a neighboring point.
!!
!! * mode = 2. **on input**, fvec must contain the functions and the
!! rows of fjac must contain the gradients
!! of the respective functions each evaluated
!! at x, and fvecp must contain the functions
!! evaluated at xp.
!! **on output**, err contains measures of correctness of
!! the respective gradients.
real(wp), intent(in) :: x(n) !! input array
real(wp), intent(in) :: Fvec(m) !! an array of length m. on input when mode = 2,
!! fvec must contain the functions evaluated at x.
real(wp), intent(in) :: Fjac(Ldfjac, n) !! an m by n array. on input when mode = 2,
!! the rows of fjac must contain the gradients of
!! the respective functions evaluated at x.
real(wp), intent(out) :: Xp(n) !! an array of length n. on output when mode = 1,
!! xp is set to a neighboring point of x.
real(wp), intent(in) :: Fvecp(m) !! an array of length m. on input when mode = 2,
!! fvecp must contain the functions evaluated at xp.
real(wp), intent(out) :: Err(m) !! an array of length m. on output when mode = 2,
!! err contains measures of correctness of the respective
!! gradients. if there is no severe loss of significance,
!! then if err(i) is 1.0 the i-th gradient is correct,
!! while if err(i) is 0.0 the i-th gradient is incorrect.
!! for values of err between 0.0 and 1.0, the categorization
!! is less certain. in general, a value of err(i) greater
!! than 0.5 indicates that the i-th gradient is probably
!! correct, while a value of err(i) less than 0.5 indicates
!! that the i-th gradient is probably incorrect.
integer :: i, j
real(wp) :: temp
real(wp), parameter :: eps = sqrt(epsmch)
real(wp), parameter :: factor = 100.0_wp
real(wp), parameter :: epsf = factor*epsmch
real(wp), parameter :: epslog = log10(eps)
select case (Mode)
case (2)
Err = zero
do j = 1, n
temp = abs(x(j))
if (temp == zero) temp = one
do i = 1, m
Err(i) = Err(i) + temp*Fjac(i, j)
end do
end do
do i = 1, m
temp = one
if (Fvec(i) /= zero .and. Fvecp(i) /= zero .and. abs(Fvecp(i) - Fvec(i)) >= epsf*abs(Fvec(i))) &
temp = eps*abs((Fvecp(i) - Fvec(i))/eps - Err(i))/(abs(Fvec(i)) + abs(Fvecp(i)))
Err(i) = one
if (temp > epsmch .and. temp < eps) Err(i) = (log10(temp) - epslog)/epslog
if (temp >= eps) Err(i) = zero
end do
case (1)
do j = 1, n
temp = eps*abs(x(j))
if (temp == zero) temp = eps
Xp(j) = x(j) + temp
end do
case default
error stop 'invalid mode in chkder'
end select
end subroutine chkder