Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Gauss Elimination on N-variables
Author:
Dipankar Basu
Website:
http://www.geocities.com/basudip_in/vb6code/
Submitted:
12/23/2006
Version:
VB6
Compatibility:
VB6
Category:
Mathematics
Views:
16964
This is an update to my previous program. Perform Gaussian Elimination method to solve a system of n-variables of n-linear system of equations. I hope the program will ne some help to students on the subject.
Declarations:
Rem PLACE A COMMAND_BUTTON ON THE FORM ' Solve a system of linear equations by GaussElimination ' Accuracy upto six-decimal dizits of N linear equations on N variables ' Programmed by Dipankar Bose on November 30, 2006.
Code:
Option Explicit Private mat() As Single, NoOfVariableEquations As Integer Private Sub Command1_Click() ' Input Matrix data 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
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement