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

◆ Sos_r()

Sub Sos_r ( N As  Long,
X() As  Double,
RTolx As  Double,
ATolx As  Double,
Tolf As  Double,
Info As  Long,
K As  Long,
XX() As  Double,
YY As  Double,
IRev As  Long,
Optional Info2 As  Long,
Optional Nprint As  Long = 0,
Optional MaxIter As  Long = 0,
Optional Iter As  Long,
Optional RNorm As  Double 
)

Solution of a system of nonlinear equations by Brown's method (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 the Brown's method.

The algorithm is based on an iterative method which is a variation of Newton's method using Gaussian elimination in a manner similar to the Gauss-Seidel process. Convergence is roughly quadratic. The convergence behavior of this code is affected by the ordering of the equations, and it is advantageous to place linear and mildly nonlinear equations first in the ordering.

The user must provide the calculated function values according to IRev. All partial derivatives required by the algorithm are approximated by first difference quotients.
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] The obtained solution vector.
[in]RTolxRelative error tolerance used in the convergence criteria. Each solution component X(I) is checked by an accuracy test of the form Abs(X(I) - Xold(I)) <= RTolx*Abs(X(I)) + ATolx, where Xold(I) represents the previous iteration value. (RTolx >= 0)
[in]ATolxAbsolute error tolerance used in the convergence criteria. If the user suspects some solution component may be zero, he should set ATolx to an appropriate (depends on the scale of the remaining variables) positive value for better efficiency. (ATolx >= 0)
[in]TolfResidual error tolerance used in the convergence criteria. Convergence will be indicated if all residuals (values of the functions or equations) are not bigger than Tolf in magnitude. Note that extreme care must be given in assigning an appropriate value for Tolf because this convergence test is dependent on the scaling of the equations. An inappropriate value can cause premature termination of the iteration process. (Tolf >= 0)
[out]Info= 0: Successful exit. (See sub-code in Info2)
= -1: The argument N had an illegal value. (N <= 0)
= -2: The argument X() is invalid.
= -3: The argument RTolx had an illegal value. (RTolx < 0)
= -4: The argument ATolx had an illegal value. (ATolx < 0)
= -5: The argument Tolf had an illegal value. (Tolf < 0)
= 1: Number of iterations has reached maxiter. (Maybe very slow convergence. Check if error tolerances are appropriate)
= 2: Number of iterations has reached maxiter. (A local minimum may have been encountered or there may be limiting precision difficulties)
= 3: Iterative scheme appears to be diverging. (Residual norms have increased over several consecutive iterations)
= 4: A Jacobian-related matrix was singular.
[out]KIRev = 1: The K-th function value should be evaluated and given in the next call.
IRev = 3: Number of iterations so far.
[out]XX()Array XX(LXX - 1) (LXX >= N)
IRev = 1: The abscissa where the function value should be evaluated and given in the next call.
IRev = 3: The last approximate solution.
[in,out]YY[in] IRev = 1: The function value fi(XX()) (i = K) should be given in the next call.
[out] IRev = 3: Residual norm at the last approximate solution.
[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: User should set the K-th function value at XX() in YY. Do not alter any variables other than YY.
= 3: Display the intermediate result (YY contains residual norm at XX() after K-th iteration) (only in the case of Nprint = 1). Do not alter any variables.
[out]Info2(Optional)
Sub-code for Info = 0.
= 1: Each component X(i) satisfies the error tolerance test: abs(X(i) - Xold(i)) <= RTolx*abs(X(i)) + ATolx.
= 2: All residuals are at most Tolf in magnitude: abs(F(X, i)) <= Tolf.
= 3: Both of above are satisfied.
= 4: Possibly converged. (Error tolerance too small or very slow convergence) (Residual norms have remained roughly constant over several consecutive iterations)
[in]Nprint(Optional)
Flag to control printing intermediate results (default = 0)
= 0: Do not return to print out the intermediate solution.
= 1: Return with IRev = 50 to print out the intermediate solution on every iteration.
(For other values, the default value will be used)
[in]MaxIter(Optional)
The maximum number of iterations. (default = 100) (if MaxIter <= 0, the default value will be used)
[out]Iter(Optional)
Number of iterations required to converge.
[out]RNorm(Optional)
A norm of the residual at the final estimate of the solution.
Reference
SLATEC
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_Sos_r()
Const N = 2
Dim X(N - 1) As Double, RTolx As Double, ATolx As Double, Tolf As Double
Dim Info As Long, K As Long, XX(N - 1) As Double, YY As Double, IRev As Long
X(0) = 0: X(1) = 0
RTolx = 0.0000000001 '1.0e-10
ATolx = 0.0000000001 '1.0e-10
Tolf = 0
IRev = 0
Do
Call Sos_r(N, X(), RTolx, ATolx, Tolf, Info, K, XX(), YY, IRev)
If IRev = 1 Then
If K = 1 Then
YY = XX(0) ^ 2 - XX(1) - 1
ElseIf K = 2 Then
YY = (XX(0) - 2) ^ 2 + (XX(1) - 0.5) ^ 2 - 1
End If
End If
Loop While IRev <> 0
Debug.Print "X1, X2 =", X(0), X(1)
Debug.Print "Info =", Info
End Sub
Sub Sos_r(N As Long, X() As Double, RTolx As Double, ATolx As Double, Tolf As Double, Info As Long, K As Long, XX() As Double, YY As Double, IRev As Long, Optional Info2 As Long, Optional Nprint As Long=0, Optional MaxIter As Long=0, Optional Iter As Long, Optional RNorm As Double)
Solution of a system of nonlinear equations by Brown's method (reverse communication version)
Example Results
X1, X2 = 1.06734608580667 0.13922766688682
Info = 0