XLPack 7.0
XLPack Numerical Library (Excel VBA) Reference Manual
Loading...
Searching...
No Matches

◆ Hybrd_r()

Sub Hybrd_r ( N As  Long,
X() As  Double,
Fvec() As  Double,
XTol As  Double,
Diag() As  Double,
Mode As  Long,
Info As  Long,
XX() As  Double,
YY() As  Double,
IRev As  Long,
Optional Maxfev As  Long = 0,
Optional Ml As  Long = -1,
Optional Mu As  Long = -1,
Optional Epsfcn As  Double = 0,
Optional Factor As  Double = 0,
Optional Nprint As  Long = 0,
Optional Nfev As  Long 
)

Solution of a system of nonlinear equations by Powell hybrid method (Jacobian not required) (reverse communication version)

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 the calculated function values according to IRev. Since the Jacobian is calculated by a forward difference approximation within the routine, the user is not required to provide the Jacobian.
Parameters
[in]NNumber of functions and variables. (N > 0)
[in,out]X()Array X(LX - 1) (LX >= N)
[in] An initial estimate of the solution vector.
[out] IRev = 0: The obtained solution vector.
  IRev = 3: Recent approximation of the solution vector.
[out]Fvec()Array Fvec(LFvec - 1) (LFvec >= N)
IRev = 0: The function values evaluated at the solution vector X()
IRev = 3: The function values evaluated at the recent approximation of the solution vector.
[in]XTolTarget 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)
= -1: The argument N had an illegal value. (N <= 0)
= -2: The argument X() is invalid.
= -3: The argument Fvec() is invalid.
= -4: The argument XTol had an illegal value. (XTol < 0)
= -5: The argument Diag() is invalid or had an illegal value. (Diag(i) < 0 when Mode = 2)
= -8: The argument XX() is invalid.
= -9: The argument YY() is invalid.
= 1: Number of function evaluations (number of returns with IRev = 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.
[out]XX()Array XX(LXX - 1) (LXX >= N)
When returned with IRev = 1 or 2, XX() contains the abscissa where the function value should be evaluated and given in the next call.
[in]YY()Array YY(LYY - 1) (LYY >= N)
When returned with IRev = 1 or 2, the function value fi(XX())(i = 1 to N) should be given in YY() in the next call.
[in,out]IRevControl variable for reverse communication.
[in] Before first call, IRev should be initialized to zero. On succeeding calls, IRev should not be altered.
[out] If IRev is not zero, complete the following process and call this routine again.
= 0: Computation finished. See return code in Info.
= 1 or 2: User should set the function values at XX() in YY(). Do not alter any variables other than YY().
= 3: Display the intermediate result (X(), Fvec(), etc.) (in the case of Nprint > 0). Do not alter any variables.
[in]Maxfev(Optional)
Termination occurs when the number of function evaluations (number of returns with IRev = 1) has reached this value. (default = 200*(N + 1)) (if Maxfev <= 0, the default value will be used)
[in]Ml(Optional)
Number of subdiagonals within the band of the Jacobian matrix (Ml >= 0). If the Jacobian is not banded, set Ml to at least N - 1. (default = N - 1) (if Ml < 0, the default value will be used)
[in]Mu(Optional)
Number of superdiagonals within the band of the Jacobian matrix (Mu >= 0). If the Jacobian is not banded, set Mu to at least N - 1. (default = N - 1) (if Mu < 0, the default value will be used)
[in]Epsfcn(Optional)
Used in determining a suitable step length for the forward-difference approximation. This approximation assumes that the relative errors in the functions are of the order of Epsfcn. If Epsfcn is less than the machine precision, it is assumed that the relative errors in the functions are of the order of the machine precision. (default = 0)
[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: To be returned with IRev = 3 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 returns with IRev = 3 for printing intermediate results are made.
[out]Nfev(Optional)
Number of function evaluations (number of returns with IRev = 1).
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 Ex_Hybrd_r()
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
Dim XX(N - 1) As Double, YY(N - 1) As Double, IRev As Long
X(0) = 0: X(1) = 0
XTol = 0.0000000001 '1.0e-10
Mode = 1
IRev = 0
Do
Call Hybrd_r(N, X(), Fvec(), XTol, Diag(), Mode, Info, XX(), YY(), IRev)
If IRev = 1 Or IRev = 2 Then
YY(0) = XX(0) ^ 2 - XX(1) - 1
YY(1) = (XX(0) - 2) ^ 2 + (XX(1) - 0.5) ^ 2 - 1
End If
Loop While IRev <> 0
Debug.Print "X1, X2 =", X(0), X(1)
Debug.Print "Info =", Info
End Sub
Sub Hybrd_r(N As Long, X() As Double, Fvec() As Double, XTol As Double, Diag() As Double, Mode As Long, Info As Long, XX() As Double, YY() As Double, IRev As Long, Optional Maxfev As Long=0, Optional Ml As Long=-1, Optional Mu As Long=-1, Optional Epsfcn As Double=0, Optional Factor As Double=0, Optional Nprint As Long=0, Optional Nfev As Long)
Solution of a system of nonlinear equations by Powell hybrid method (Jacobian not required) (reverse ...
Example Results
X1, X2 = 1.06734608580669 0.139227666886862
Info = 0