Here is an implementation of Gauss-Seidel in Visual Basic. Before you use it though, there are a few words of warning: the Gauss-Seidel method works best on diagonally dominant systems of equations (ie the elements on the diagonal are larger than the other elements in the array). I have included a function to test for this condition (the GaussSeidelConverge function) if this function returns "true", the system is guaranteed to converge; if it does not return "true" it is not guaranteed (but that does not necessarily mean it will diverge). One of the optional arguments to the main routine is "RelaxCoeff", this is a relaxation coefficient that may help with the solving process. This coefficient can take on values from 0 to 2 (1 is the default, it is the same as not using relaxation). If the system is guaranteed to converge, you can use a value between 1 and 2 to make in converge faster; if the system is not guaranteed to converge setting it to a value between 0 and 1 may help keep things on track.
My implementation is by no means "bulletproof" but it should get you headed in the right direction. Hope it helps you.
PS if you have any questions about it please ask, and if you make any improvements to it I'd like to see them!
OK here's the code:
'10/15/2001
'Module to perform Gauss-Seidel algorithm
'to solve a system of linear equations
'as is, these functions expect 0 based arrays
Option Explicit
Public Function NormalizeSystem(Coefficients() As Double, Constants() As Double)
As Boolean
Dim dblDummy As Double
Dim i As Integer
Dim j As Integer
On Error GoTo ErrorNormalizeSystem
For i = 0 To UBound(Coefficients(), 1)
dblDummy = Coefficients(i, i)
For j = 0 To UBound(Coefficients(), 2)
Coefficients(i, j) = Coefficients(i, j) / dblDummy
Next j
Constants(i) = Constants(i) / dblDummy
Next i
NormalizeSystem = True
Exit Function
ErrorNormalizeSystem:
NormalizeSystem = False
End Function
Public Function GaussSeidel(Coefficients() As Double, _
Constants() As Double, _
Unknowns() As Double, _
Optional ErrorAllow As Single = 0.001, _
Optional MaxIterations As Integer = 100, _
Optional RelaxCoeff As Single = 1) As Boolean
Dim intIterations As Integer
Dim blnSentinel As Boolean
Dim dblError As Double
Dim dblOld As Double
Dim dblSum As Double
Dim i As Integer, j As Integer
NormalizeSystem(Coefficients(), Constants())
If ErrorAllow <= 0 Then ErrorAllow = 0.0001
If RelaxCoeff < 0 Then RelaxCoeff = 0
If RelaxCoeff > 2 Then RelaxCoeff = 2
intIterations = 0
blnSentinel = False
On Error GoTo ErrorGaussSeidel
Do While (intIterations < MaxIterations) And (blnSentinel = False)
'blnSentinel = True
intIterations = intIterations + 1
'loop through the equations
For i = 0 To UBound(Coefficients(), 1)
dblOld = Unknowns(i)
dblSum = Constants(i)
'loop through the coefficients in the equation
For j = 0 To UBound(Coefficients(), 2)
If i <> j Then
dblSum = dblSum - Coefficients(i, j) * Unknowns(j)
End If
Next j
Unknowns(i) = RelaxCoeff * dblSum + (1 - RelaxCoeff) * dblOld
If (blnSentinel = False) And (Unknowns(i) <> 0) Then
dblError = Abs((Unknowns(i) - dblOld) / Unknowns(i)) * 100
End If
Next i
If dblError <= ErrorAllow Then
blnSentinel = True
GaussSeidel = True
End If
Loop
Exit Function
ErrorGaussSeidel:
GaussSeidel = False
End Function
Public Function GaussSeidelConverge(Coefficients() As Double) As Boolean
Dim i As Integer, j As Integer
Dim sum As Double
For i = 0 To UBound(Coefficients(), 1)
sum = 0
For j = 0 To UBound(Coefficients(), 2)
If i <> j Then
sum = sum + Abs(Coefficients(i, j))
End If
Next j
If sum > Abs(Coefficients(i, i)) Then
GaussSeidelConverge = False
Exit Function
Else
GaussSeidelConverge = True
End If
Next i
End Function