How I can implement a matrix inversion using Gauss-Seidel Method?
How I can implement a matrix inversion using Gauss-Seidel Method?
(OP)
Hi guys,I need to implement one VB 5.0 aplication, witch decoy a imagem jammed to the same imagem, but clear now. I will use a calculus with two matrix, calls Matrix_A and Matrix_B, right ? Well, I can't use the matrix invertion, using the Gauss-Seidel's linear system calculation method. How I can implement a matrix inversion using Gauss-Seidel Method in Visual Basic 5.0 ?
Thanks for all !!!
Thanks for all !!!
RE: How I can implement a matrix inversion using Gauss-Seidel Method?
The Gauss-Seidel method is an iterative method to solve a system of equations but it does not use (or return) the matrix inverse. If all you are interested in is the matrix inverse, you will have to find a different algorithm (such as LU decomposition or a modified version of Gauss-Jordan). If all you are interested in is solving a system of linear equations, the Gauss-Seidel method will do just fine.
RE: How I can implement a matrix inversion using Gauss-Seidel Method?
RE: How I can implement a matrix inversion using Gauss-Seidel Method?
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