|
|
◆ Chkder()
| Sub Chkder |
( |
M As |
Long, |
|
|
N As |
Long, |
|
|
X() As |
Double, |
|
|
Fvec() As |
Double, |
|
|
Fjac() As |
Double, |
|
|
Xp() As |
Double, |
|
|
Fvecp() As |
Double, |
|
|
Mode As |
Long, |
|
|
Err() As |
Double, |
|
|
Info As |
Long |
|
) |
| |
Checks the computed gradients
- Purpose
- This routine checks the gradients of M nonlinear functions in N variables, evaluated at a point X, for consistency with the functions themselves. It can be used to check the user's subroutine for Hybrj, Hybrj1, and others.
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.
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.
- Parameters
-
| [in] | M | Number of functions. (M > 0) |
| [in] | N | Number of variables. (N > 0) |
| [in] | X() | Array X(LX - 1) (LX >= N)
The point of evaluation. |
| [in] | Fvec() | Array Fvec(LFvec - 1) (LFvec >= M)
The function values evaluated at X (when Mode = 2). |
| [in] | Fjac() | Array Fjac(LFjac1 - 1, LFjac2 - 1) (LFjac1 >= M, LFjac2 >= N)
The Jacobian (∂fi/∂xj) values (i = 0 to M-1, j = 0 to N-1) evaluated at X (when Mode = 2). |
| [out] | Xp() | Array Xp(LXp - 1) (LXp >= N)
A neighboring point of X returned by the routine (when Mode = 1). |
| [in] | Fvecp() | Array Fvecp(LFvecp - 1) (LFvecp >= M)
The function values evaluated at Xp (when Mode = 2). |
| [in] | Mode | Control variable. (Mode = 1 or 2)
The user must call this routine twice as below.
- First, after setting M, N, and X(), call with Mode = 1, then Xp() is returned.
- Second, after setting Fvec(), Fjac(), and Fvecp(), call with Mode = 2, then Err() contains measures of correctness of the respective gradients.
|
| [out] | Err() | Array Err(LErr - 1) (LErr >= M)
Measures of correctness of the respective gradients (when Mode = 2). (0 <= Err(i) <= 1)
1 shows the gradients is correct, and 0 shows it is incorrect. For values between 0 and 1, 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. |
| [out] | Info | = 0: Successful exit.
= -1: The argument M had an illegal value. (M <= 0)
= -2: The argument N had an illegal value. (N <= 0)
= -3: The argument X() is invalid.
= -4: The argument Fvec() is invalid.
= -5: The argument Fjac() is invalid.
= -6: The argument Xp() is invalid.
= -7: The argument Fvecp() is invalid.
= -8: The argument Mode had an illegal value. (Mode <> 1 and Mode <> 2)
= -9: The argument Err() is invalid. |
- Reference
- netlib/minpack
- Example Program
- Check the derivative values computed by the subroutine used to solve the following nonlinear equations.
x1^2 - x2 - 1 = 0
(x1 - 2)^2 + (x2 - 0.5)^2 - 1 = 0
To be checked at the point (x1, x2) = (1, 1). Sub FHybrj(N As Long, X() As Double, Fvec() As Double, Fjac() As Double, IFlag As Long)
If IFlag = 1 Then
Fvec(0) = X(0) ^ 2 - X(1) - 1
Fvec(1) = (X(0) - 2) ^ 2 + (X(1) - 0.5) ^ 2 - 1
ElseIf IFlag = 2 Then
Fjac(0, 0) = 2 * X(0)
Fjac(1, 0) = 2 * X(0) - 4
Fjac(0, 1) = -1
Fjac(1, 1) = 2 * X(1) - 1
End If
End Sub
Sub Ex_Chkder()
Const M = 2, N = 2
Dim X(N - 1) As Double, Fvec(N - 1) As Double, Fjac(N - 1, N - 1) As Double
Dim Xp(N - 1) As Double, Fvecp(N - 1) As Double, Err(N - 1) As Double
Dim Mode As Long, Info As Long
X(0) = 1: X(1) = 1
'-- Check 1
Mode = 1
Call Chkder(M, N, X(), Fvec(), Fjac(), Xp(), Fvecp(), Mode, Err(), Info)
'-- Check 2
Mode = 2
Call FHybrj(N, X(), Fvec(), Fjac(), 1)
Call FHybrj(N, X(), Fvec(), Fjac(), 2)
Call FHybrj(N, Xp(), Fvecp(), Fjac(), 1)
Call Chkder(M, N, X(), Fvec(), Fjac(), Xp(), Fvecp(), Mode, Err(), Info)
Debug.Print "Err =", Err(0), Err(1)
Debug.Print "Info =", Info
End Sub
Sub Chkder(M As Long, N As Long, X() As Double, Fvec() As Double, Fjac() As Double, Xp() As Double, Fvecp() As Double, Mode As Long, Err() As Double, Info As Long) Checks the computed gradients
- Example Results
Err = 1 0.923072860548928
Info = 0
|