|
|
◆ Hybrj()
| Sub Hybrj |
( |
F As |
LongPtr, |
|
|
N As |
Long, |
|
|
X() As |
Double, |
|
|
Fvec() As |
Double, |
|
|
XTol As |
Double, |
|
|
Diag() As |
Double, |
|
|
Mode As |
Long, |
|
|
Info As |
Long, |
|
|
Optional Maxfev As |
Long = 0, |
|
|
Optional Factor As |
Double = 0, |
|
|
Optional Nprint As |
Long = 0, |
|
|
Optional Nfev As |
Long, |
|
|
Optional Njev As |
Long |
|
) |
| |
Solution of a system of nonlinear equations by Powell hybrid method
- Purpose
- This routine finds a zero of a system of n nonlinear functions in n variables
fi(x1, x2, ..., xn) = 0 (i = 1 to n)
by a modification of the Powell hybrid method.
The user must provide a subroutine which calculates the functions and the Jacobian.
- Parameters
-
| [in] | F | User supplied subroutine which calculates the functions fi(x) and the Jacobian defined as follows. Sub F(N As Long, X() As Double, Fvec() As Double, Fjac() As Double, IFlag As Long)
If IFlag = 1:
Calculate the function values fi(x) at X() and return in Fvec() (i = 0 to n-1). Other variables should not be changed.
If IFlag = 2:
Calculate the Jacobian matrix (dfi/dxj) and return in Fjac() (i = 0 to n-1, j = 0 to n-1). Other variables should not be changed.
If IFlag = 0:
If Nprint is set to positive value, F is called every Nprint iterations with IFlag = 0 for outputting intermediate result X() and Fvec(). Any variables should not be changed.
End Sub
The value of Iflag should not be changed unless the user wants to terminate the execution. In this case, set Iflag to a negative integer. |
| [in] | N | Number of functions and variables. (N > 0) |
| [in,out] | X() | Array X(LX - 1) (LX >= N)
[in] An initial estimate of the solution vector.
[out] The obtained solution vector. |
| [out] | Fvec() | Array Fvec(LFvec - 1) (LFvec >= N)
The function values evaluated at the solution vector X(). |
| [in] | XTol | Target relative tolerance. Termination occurs when the relative error between two consecutive iterations is at most XTol. (XTol >= 0) |
| [in,out] | Diag() | Array Diag(LDiag - 1) (LDiag >= N)
[in] If Mode = 2, Diag() must contain positive entries that serve as multiplicative scale factors for the variables. (Diag(i) > 0)
[out] If Mode = 1, Diag() is set by the subroutine. |
| [in] | Mode | = 1: The variables will be automatically scaled by the subroutine.
= 2: The scaling is specified by the input Diag().
(For other values, Mode = 1 is assumed.) |
| [out] | Info | = 0: Successful exit. (Relative error between two consecutive iterates is at most XTol)
= -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 XTol had an illegal value. (XTol < 0)
= -6: The argument Diag() is invalid.
= 1: Number of calls to F with IFlag = 1 has reached Maxfev.
= 2: XTol is too small. No further improvement in the approximate solution X is possible.
= 3: Iteration is not making good progress, as measured by the improvement from the last five Jacobian evaluations.
= 4: Iteration is not making good progress, as measured by the improvement from the last ten iterations.
= 5: User imposed termination (returned from F with IFlag < 0). |
| [in] | Maxfev | (Optional)
Termination occurs when the number of calls to F with IFlag = 1 or 2 has reached this value. (default = 100*(N + 1)) (if Maxfev <= 0, the default value will be used) |
| [in] | Factor | (Optional)
Used in determining the initial step bound. This bound is set to the product of Factor and the Euclidean norm of Diag*X if nonzero, or else to factor itself. In most cases factor should lie in the interval (0.1, 100). 100 is a generally recommended value. (default = 100) (if Factor <= 0, the default value will be used) |
| [in] | Nprint | (Optional)
Enables controlled printing of iterates. (default = 0)
> 0: F is called with Iflag = 0 at the beginning of the first iteration and every nprint iterations thereafter and at the end of last iteration for printing intermediate result.
<= 0: No special calls of F for printing intermediate results are made. |
| [out] | Nfev | (Optional)
Number of function evaluations (number of calls to F with IFlag = 1). |
| [out] | Njev | (Optional)
Number of Jacobian evaluations (number of calls to F with IFlag = 2). |
- Reference
- netlib/minpack
- Example Program
- Solve the following system of nonlinear equations.
x1^2 - x2 - 1 = 0
(x1 - 2)^2 + (x2 - 0.5)^2 - 1 = 0
The initial approximation (x1, x2) = (0, 0) is used. 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_Hybrj()
Const N = 2
Dim X(N - 1) As Double, Fvec(N - 1) As Double, XTol As Double
Dim Diag(N - 1) As Double, Mode As Long, Info As Long
X(0) = 0: X(1) = 0
XTol = 0.0000000001 '1.0e-10
Mode = 1
Call Hybrj(AddressOf FHybrj, N, X(), Fvec(), XTol, Diag(), Mode, Info)
Debug.Print "X1, X2 =", X(0), X(1)
Debug.Print "Info =", Info
End Sub
Sub Hybrj(F As LongPtr, N As Long, X() As Double, Fvec() As Double, XTol As Double, Diag() As Double, Mode As Long, Info As Long, Optional Maxfev As Long=0, Optional Factor As Double=0, Optional Nprint As Long=0, Optional Nfev As Long, Optional Njev As Long) Solution of a system of nonlinear equations by Powell hybrid method
- Example Results
X1, X2 = 1.06734608580669 0.139227666886862
Info = 0
|