Results 1 to 9 of 9

Thread: Gauss-Jordan (Partially Solved)

Threaded View

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Exclamation Gauss-Jordan (Partially Solved)

    Hello there... Here I am posting a doubt again. I haven't checked the one of the INI file because I have been very busy working on Algorithms provided by my professor.

    Anyway... Here is my question:
    What am I doing wrong? I am trying to implement the Gauss-Jordan algorithm but I just can't. After the first iteration it won't work... What is wrong with my code? I also tried translating other codes I have seen into VB Code but it won't work... Here you have my code:


    VB Code:
    1. Public Enum MatOperator
    2.  vbSum = 0
    3.  vbSubstraction = 1
    4.  vbMultiplication = 2
    5.  vbDivision = 3
    6.  vbPower = 4
    7.  vbRoot = 5
    8. End Enum
    9.  
    10. Public Sub MatInterchange(M As Variant, i1 As Long, j1 As Long, Optional i2 As Long = -1, Optional j2 As Long = -1)
    11. Dim x As Double
    12.  
    13. If i2 = -1 Then i2 = i1
    14. If j2 = -1 Then j2 = j1
    15.  
    16. x = M(i1, j1)
    17. M(i1, j1) = M(i2, j2)
    18. M(i2, j2) = x
    19. End Sub
    20.  
    21. Public Sub MatInterChng(M As Variant, j1 As Long, j2 As Long)
    22. Dim i As Long
    23.  
    24. For i = LBound(M, 1) To UBound(M, 1)
    25.  MatInterchange M, i, j1, i, j2
    26. Next i
    27. End Sub
    28.  
    29. Public Sub MatOperateRow(M As Variant, j As Long, Operator As MatOperator, ToOperate As Long)
    30. Dim i As Long
    31.  
    32. For i = LBound(M, 1) To UBound(M, 1)
    33.  MatApOperation M, i, j, Operator, ToOperate
    34. Next i
    35. End Sub
    36.  
    37. Public Sub MatSubstitutePlusK(M As Variant, K As Long, j1 As Long, j2 As Long)
    38. Dim i As Long
    39.  
    40. For i = LBound(M, 1) To UBound(M, 1)
    41.  MatApOperation M, i, j1, vbSum, M(i, j2) * K
    42. Next i
    43. End Sub
    44.  
    45. Public Sub MatApOperation(M As Variant, i As Long, j As Long, Operator As MatOperator, ToOperate As Long)
    46. Select Case Operator
    47. Case 0:
    48.  M(i, j) = M(i, j) + ToOperate
    49. Case 1:
    50.  M(i, j) = M(i, j) - ToOperate
    51. Case 2:
    52.  M(i, j) = M(i, j) * ToOperate
    53. Case 3:
    54.  M(i, j) = M(i, j) / ToOperate
    55. Case 4:
    56.  M(i, j) = M(i, j) ^ ToOperate
    57. Case 5:
    58.  M(i, j) = M(i, j) ^ -ToOperate
    59. End Select
    60. End Sub
    61.  
    62. Public Function GaussJordan(A As Variant, B As Variant) As Variant
    63. Dim GJ() As Double
    64. Dim i As Long
    65. Dim j As Long
    66. Dim n As Long
    67. Dim Cons As Double
    68.  
    69. LnFd = ChR$(13) & ChR$(10)
    70. DLnFd = LnFd & LnFd
    71.  
    72. ReDim GJ(UBound(A, 1) + 1, UBound(A, 2))
    73.  
    74. For i = LBound(GJ, 1) To UBound(GJ, 1)
    75.  For j = LBound(GJ, 2) To UBound(GJ, 2)
    76.   If i = UBound(GJ, 1) Then
    77.    GJ(i, j) = B(LBound(B, 1), j)
    78.   Else
    79.    GJ(i, j) = A(i, j)
    80.   End If
    81.  Next j
    82. Next i
    83.  
    84. For i = LBound(GJ, 2) To UBound(GJ, 2)
    85.  For n = LBound(GJ, 2) To UBound(GJ, 2)
    86.   If n <> i Then
    87.    MatSubstitutePlusK GJ, -GJ(i, n), n, i
    88.    MsgBox " i = " & i & LnFd & "GJ(" & i & ", " & n & ") = " & GJ(i, n) & " n = " & n & LnFd & "GJ(" & i & ", " & i & ") = " & GJ(i, i), vbExclamation, "Iteration " & i
    89.   End If
    90.  Next n
    91. Next i
    92.  
    93. 'Turn the main diagonal into 1
    94. For j = LBound(GJ, 2) To UBound(GJ, 2)
    95.  If GJ(j, j) <> 1 Then
    96.   MatOperateRow GJ, j, vbDivision, CLng(GJ(j, j))
    97.  End If
    98. Next j
    99.  
    100. GaussJordan = GJ
    101. End Function
    Last edited by Tec-Nico; Oct 28th, 2002 at 07:40 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width