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

◆ Sos()

Sub Sos ( F As  LongPtr,
N As  Long,
X() As  Double,
RTolx As  Double,
ATolx As  Double,
Tolf As  Double,
Info 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

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 a subroutine which calculates the function values. All partial derivatives required by the algorithm are approximated by first difference quotients.
Parameters
[in]FUser supplied subroutine which calculates the functions fi(x) defined as follows.
Sub F(N As Long, X() As Double, K As Long, Fval As Double)
If K = 1, ..., N:
Calculate the value of the K-th function Fk(X) and return it in Fval.
If K <= 0:
If Nprint = 1, F is called to print out the intermediate solution on every iteration. The estimated solution in X() and its residual norm in Fval on (-K)-th iteration will be provided.
End Sub
Index of X() will be 0 to N-1. When function value is required, however, be noted that the value of K will be 1 to N. Variables except Fval should not be changed.
[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)
= -2: The argument N had an illegal value. (N <= 0)
= -3: The argument X() is invalid.
= -4: The argument RTolx had an illegal value. (RTolx < 0)
= -5: The argument ATolx had an illegal value. (ATolx < 0)
= -6: 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]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 call F (K <= 0) to print out the intermediate solution.
= 1: Call F (K <= 0) 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 FSos(N As Long, X() As Double, K As Long, Fval As Double)
If K = 1 Then
Fval = X(0) ^ 2 - X(1) - 1
ElseIf K = 2 Then
Fval = (X(0) - 2) ^ 2 + (X(1) - 0.5) ^ 2 - 1
End If
End Sub
Sub Ex_Sos()
Const N = 2
Dim X(N - 1) As Double, RTolx As Double, ATolx As Double, Tolf As Double
Dim Info As Long
X(0) = 0: X(1) = 0
RTolx = 0.0000000001 '1.0e-10
ATolx = 0.0000000001 '1.0e-10
Tolf = 0
Call Sos(AddressOf FSos, N, X(), RTolx, ATolx, Tolf, Info)
Debug.Print "X1, X2 =", X(0), X(1)
Debug.Print "Info =", Info
End Sub
Sub Sos(F As LongPtr, N As Long, X() As Double, RTolx As Double, ATolx As Double, Tolf As Double, Info 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
Example Results
X1, X2 = 1.06734608580667 0.13922766688682
Info = 0