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

◆ Sor_s()

Sub Sor_s ( N As  Long,
Matvec As  LongPtr,
Matsol As  LongPtr,
ChkConv As  LongPtr,
B() As  Double,
X() As  Double,
Optional Info As  Long,
Optional Iter As  Long,
Optional Res As  Double,
Optional MaxIter As  Long = 500 
)

Solution of linear system Ax = b using Successive over-relaxation (SOR) method (Subroutine version)

Purpose
This routine solves the linear system Ax = b using successive over-relaxation (SOR) iterative method.
Parameters
[in]NDimension of the matrix A. (N >= 0) (if N = 0, returns without computation)
[in]MatvecUser supplied subroutine which calculates the product of matrix A and vector x as follows.
Sub Matvec(N As Long, X() As Double, Y() As Double)
Compute A*x, and return in Y().
End Sub
[in]MatsolUser supplied subroutine which computes (D/ω + L)^(-1)*b using diagonal and lower tridiagonal parts of matrix A as follows.
Sub Matsol(N As Long, B() As Double, X() As Double)
Compute x = (D/ω + L)^(-1)*b (or solve (D/ω + L)*x = b) and return solution x in X().
End Sub
[in]ChkConvUser supplied subroutine which is called on every iteration for the convergence test as follows, where X() is the current approximate solution, Res is the current residual norm norm(b - A*x), and Iter is the current number of iterations. This routine can also be used to output the intermediate results.
Sub ChkConv(N As Long, X() As Double, Res As Double, Iter As Long, IChk As Long)
Set IChk = 1 if converged. Otherwise, set IChk = 0.
End Sub
[in]B()Array B(LB - 1) (LB >= N)
Right hand side vector b.
[in,out]X()Array X(LX - 1) (LX >= N)
[in] Initial guess of solution.
[out] Obtained approximate solution.
[out]Info(Optional)
= 0: Successful exit
= i < 0: The (-i)-th argument is invalid.
= 11: Maximum number of iterations exceeded.
= 12: Matrix is singular (zero diagonal element).
[out]Iter(Optional)
Actual number of iterations performed for convergence.
[out]Res(Optional)
Final residual norm value norm(b - A*x).
[in]MaxIter(Optional)
Maximum number of iterations. (MaxIter > 0) (default = 500)
Note
To calculate (D/ω + L)^(-1)*b, CscDussv or CsrDussv can be used.
Example Program
Solve the system of linear equations Ax = B, where
( 0.2 -0.11 -0.93 ) ( -0.3727 )
A = ( -0.32 0.81 0.37 ), B = ( 0.4319 )
( -0.8 -0.92 -0.29 ) ( -1.4247 )
Const N = 3, Nnz = N * N, Omega = 0.4, Tol = 0.0000000001
Dim A(Nnz - 1) As Double, Ia(N) As Long, Ja(Nnz - 1) As Long
Sub Matvec(N As Long, X() As Double, Y() As Double)
Call CsrDusmv("N", N, N, 1, A(), Ia(), Ja(), X(), 0, Y())
End Sub
Sub Matsol(N As Long, B() As Double, X() As Double)
Dim I As Long
For I = 0 To N - 1
X(I) = B(I)
Next
Call CsrDussv("L", "N", "N", N, A(), Ia(), Ja(), X(), Omega:=Omega)
End Sub
Sub ChkConv(N As Long, X() As Double, Res As Double, Iter As Long, IChk As Long)
If Res < Tol Then
IChk = 1
Else
IChk = 0
End If
End Sub
Sub Ex_Sor_s()
Dim B(N - 1) As Double, X(N - 1) As Double
Dim Iter As Long, Res As Double, Info As Long
A(0) = 0.2: A(1) = -0.11: A(2) = -0.93: A(3) = -0.32: A(4) = 0.81: A(5) = 0.37: A(6) = -0.8: A(7) = -0.92: A(8) = -0.29
Ia(0) = 0: Ia(1) = 3: Ia(2) = 6: Ia(3) = 9
Ja(0) = 0: Ja(1) = 1: Ja(2) = 2: Ja(3) = 0: Ja(4) = 1: Ja(5) = 2: Ja(6) = 0: Ja(7) = 1: Ja(8) = 2
B(0) = -0.3727: B(1) = 0.4319: B(2) = -1.4247
Call Sor_s(N, AddressOf Matvec, AddressOf Matsol, AddressOf ChkConv, B(), X(), Info, Iter, Res)
Debug.Print "X =", X(0), X(1), X(2)
Debug.Print "Iter = " + CStr(Iter) + ", Res = " + CStr(Res) + ", Info = " + CStr(Info)
End Sub
Sub CsrDussv(Uplo As String, Trans As String, Diag As String, N As Long, Val() As Double, Rowptr() As Long, Colind() As Long, X() As Double, Optional Info As Long, Optional Base As Long=-1, Optional IncX As Long=1, Optional Omega As Double=1)
Solution of Ax = b or ATx = b (triangular matrices) (CSR)
Sub CsrDusmv(Trans As String, M As Long, N As Long, Alpha As Double, Val() As Double, Rowptr() As Long, Colind() As Long, X() As Double, Beta As Double, Y() As Double, Optional Info As Long, Optional Base As Long=-1, Optional IncX As Long=1, Optional IncY As Long=1)
y <- αAx + βy or y <- αATx + βy (CSR)
Sub Sor_s(N As Long, Matvec As LongPtr, Matsol As LongPtr, ChkConv As LongPtr, B() As Double, X() As Double, Optional Info As Long, Optional Iter As Long, Optional Res As Double, Optional MaxIter As Long=500)
Solution of linear system Ax = b using Successive over-relaxation (SOR) method (Subroutine version)
Example Results
X = 0.859999999965658 0.640000000003731 0.509999999994652
Iter = 57, Res = 2.83730635879748E-11, Info = 0