VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Perform Gaussian Elimination method to solve a system of n-variables of n-linear system of equation

by Angsuman Banerji (23 Submissions)
Category: Math/Dates
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Fri 4th January 2008
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Perform Gaussian Elimination method to solve a system of n-variables of n-linear system of equations.

API Declarations


Private mat() As Single, NoOfVariableEquations As Integer


Rate Perform Gaussian Elimination method to solve a system of n-variables of n-linear system of equation




Dim mBound As Integer, i As Integer, j As Integer
NoOfVariableEquations = Val(InputBox("Number of variables", "Equations", 3))
    If NoOfVariableEquations <= 0 Then Exit Sub '   nothing2do
mBound = NoOfVariableEquations - 1
If mBound > 1 Then ReDim mat(mBound, mBound + 1)
For i = 0 To mBound
    For j = 0 To mBound
        mat(i, j) = InputBox("Enter Element co-eff value" & vbNewLine & _
                "Element [" & i & "," & j & "]", "Input Data Element")
    Next j
        mat(i, mBound + 1) = InputBox("Enter Element Value" & vbNewLine & _
                "Value [" & i & "," & mBound + 1 & "]", "Input Equation Value")
Next i
    Print "Matrix representation of the linear Equations"
    Call DisplayMatrixData
    Call UpperTriangular
    Print "converted to Upper Triangular form"
    Call DisplayMatrixData
    Call BkSubst

End Sub

Private Sub DisplayMatrixData()

Dim i As Integer, j As Integer, res As String
For i = 0 To NoOfVariableEquations - 1
    For j = 0 To NoOfVariableEquations
        res = res & vbTab & mat(i, j)
    Next j
        res = res & vbNewLine
Next i
        MsgBox res: Print res & vbNewLine

End Sub

Private Sub UpperTriangular()       ' Matrix to Echelon form

Dim i As Integer, j As Integer, k As Integer, mBound As Integer
Dim df As Single
mBound = NoOfVariableEquations - 1
For k = 0 To mBound - 1
  For i = k + 1 To mBound
        If Abs(mat(k, k)) < Abs(mat(i, k)) Then Call swapRowMat(i, k)
        df = mat(i, k) / mat(k, k)
    For j = k To 3
      mat(i, j) = FormatNumber(mat(i, j) - mat(k, j) * df, 4)
    Next j
  Next i
Next k

End Sub

Private Sub swapRowMat(ByVal r1 As Integer, ByVal r2 As Integer)

Dim i As Integer, mB As Integer, dVal As Single
mB = NoOfVariableEquations
For i = 0 To mB
    dVal = mat(r1, i):        mat(r1, i) = mat(r2, i):        mat(r2, i) = dVal
Next i

End Sub

Private Sub BkSubst()       '   BackSubstitution

Dim i As Integer, j As Integer, mBound As Integer, sSum As Single
mBound = NoOfVariableEquations - 1
ReDim reslt(mBound) As Single
reslt(mBound) = mat(mBound, mBound + 1) / mat(mBound, mBound)
  For i = mBound - 1 To 0 Step -1
    sSum = 0
    For j = i + 1 To mBound
    sSum = sSum + mat(i, j) * reslt(j)
    Next j
    reslt(i) = FormatNumber((mat(i, mBound + 1) - sSum) / mat(i, i), 5)
  Next i
Dim msgStr As String
  For i = 0 To mBound
    msgStr = msgStr + vbNewLine & "X" & i + 1 & vbTab & reslt(i)
  Next i
        MsgBox msgStr, , "Result"
        Print "Solution of Equations" & msgStr

End Sub

Download this snippet    Add to My Saved Code

Perform Gaussian Elimination method to solve a system of n-variables of n-linear system of equation Comments

No comments have been posted about Perform Gaussian Elimination method to solve a system of n-variables of n-linear system of equation. Why not be the first to post a comment about Perform Gaussian Elimination method to solve a system of n-variables of n-linear system of equation.

Post your comment

Subject:
Message:
0/1000 characters