Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How I can implement a matrix inversion using Gauss-Seidel Method?

Status
Not open for further replies.

Pegado

Computer
Oct 2, 2001
3
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 !!!
 
Replies continue below

Recommended for you

Simply put, you can't.

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.
 
Thank's for you explaination ! I wanna implement the Gauss-Seidel Linear solve system. Forget the matrix inversion, I need knows only how a get this in VB 5.0. Can you help me ?
 
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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor