Results 1 to 9 of 9

Thread: Gauss-Jordan (Partially Solved)

  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

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    I love a good math challenge, but we need more information.

    How are you declaring all of the variables you use?
    How are you calling these functions?
    Where are you recieving errors or what function is returning incorrect results.

    "It doesn't work" isn't enough detail.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  3. #3

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

    Talking Sorry...

    First... Thanks for replying!
    Second, you are right. I am not providing a lot of information.

    It is I had comments but most of them where in spanish. It took me a while to translate some of the functions and I forgot to add what I was trying to do. Alright, let me explain myself.

    As you know, I am trying to use Gauss-Jordan to solve a Matrix Problem. I have already programmed Crammer's rule in a Module called "MatrixEs" which is the module where I am working now. Basicly you give a square matrix to the program, the results matrix and you fuse them to create the Gauss-Jordan matrix.

    Lets say we are calling the following Matrix using the Command6:

    x1 + 4x2 + 3x3= 2600 (1) 1 4 3 2600
    2x1 + 5x2 + 4x3= 3500 (2) 2 5 4 3500
    x1 + 3x2 + 2x3= 2000 (3) 1 3 2 2000



    VB Code:
    1. Private Sub Command6_Click()
    2. Dim A() As Long
    3. Dim B() As Long
    4. Dim X As Variant
    5. Dim i As Long
    6. Dim j As Long
    7.  
    8. ReDim A(2, 2)
    9. ReDim B(0, 2)
    10.  
    11. A(0, 0) = 1
    12. A(1, 0) = 4
    13. A(2, 0) = 3
    14. A(0, 1) = 2
    15. A(1, 1) = 5
    16. A(2, 1) = 4
    17. A(0, 2) = 1
    18. A(1, 2) = 3
    19. A(2, 2) = 2
    20.  
    21. B(0, 0) = 2600
    22. B(0, 1) = 3500
    23. B(0, 2) = 2000
    24.  
    25. X = MatResolve(A, B, vbGauss)
    26.  
    27. For i = LBound(X, 1) To UBound(X, 1)
    28.  For j = LBound(X, 2) To UBound(X, 2)
    29.   MsgBox "X(" & i & ", " & j & ") = " & X(i, j)
    30.  Next j
    31. Next i
    32. End Sub

    I will keep adding info, but this is it for this first reply.
    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

  4. #4

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

    Going on...

    Alright, as you could see I called a function "MatResolve" which will receive the data of the Matrices and will select the method to use to solve them. In this case VBGauss is for Gauss-Jordan.

    Let me include here that procedure and the proper enumeration:


    VB Code:
    1. Public Enum MatMethods
    2.  vbCramer = 0
    3.  vbGauss = 1
    4.  vbMontante = 2
    5. End Enum
    6.  
    7.  
    8. Public Function MatResolve(A As Variant, B As Variant, Optional ByVal Method As MatMethods = vbCramer) As Variant
    9. Select Case Method
    10. Case vbCramer:
    11.  MatResolve = Cramer(A, B)
    12. Case vbGauss:
    13.  MatResolve = GaussJordan(A, B)
    14. Case vbMontante:
    15.  MatResolve = Montante(A, B)
    16. Case Else
    17.  MatResolve = Cramer(A, B)
    18. End Select
    19. End Function

    I also read in most of the pages I have reviewed that I would need to add basic operations, and that is what I did. Some of the functions above are the basic operations. I will add more info in next reply.
    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

  5. #5
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Are you getting errors or unexpected result?

    (Man, mathmatical matrixes was years ago, I hope I can restart that section of my brain!)
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  6. #6

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

    Arrow Still going on...

    As you know... Gauss Jordan will get the following Matrix:

    1 0 0 300
    0 1 0 500
    0 0 1 100

    Which means that the answer to the first equation is 300, the answer to the second one is 500 and the answer to the third one is 100. So you can see the last thing I added to the function of Gauss-Jordan was to divide each row over the number that is left in the main diagonal, if it is different from 1.

    Now, after the first I get the following:

    1 4 3 2600
    0 -3 -2 -1700
    0 -1 -1 -600

    And so far is doing fine... In the next iteration it should be:

    1 0 -1 2600
    0 -3 -2 -1700
    0 0 1 100

    But my code gives different values to this... So I don't know what is wrong, this is the 15th time I redo it and I am tired of trying to solve it.

    I have to also explain that it shows the matrix from up to down... So in the first iteration you will get as messages this:

    message 1: 1
    message 2: 0
    message 3: 0

    Instead of:

    message 1: 1
    message 2: 4
    message 3: 3
    message 4: 2600

    I am sure the functions above work properly but I don't know how I should use them to make the algorithm work properly!
    Last edited by Tec-Nico; Oct 27th, 2002 at 09:54 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

  7. #7

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

    Arrow I am getting an unexpected result

    I know, I know... But I am an University student. Do you want me to tell my professor "Man, this was years ago, please get updated!"?

    I just can't... Besides I like the challenge programming these algorithms make me feel. The problem is that I cannot find a proper algorithm anywhere to check how this should be applied.

    In pages I have gone to they say that there are 3 elemental operations... Which are:

    1.- Switching Rows
    2.- Multiply a Row times a Number (Not a Matrix)
    3.- Fj = Fj + n*Fi

    These basic operations have been defined by me in these procedures:


    VB Code:
    1. ''-- Basic Operations --''
    2. '1.- MatInterChng GJ, 0, 1
    3. '2.- MatOperarateRow GJ, 1, vbMultiplication, 2
    4. '3.- MatSubstitutePlusK GJ, 2, 2, 0
    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

  8. #8

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

    Lightbulb Partial Solution

    Here, you go... This is the partial solution to the Gauss-Jordan Method. I have not added the code to switch rows in case you find a zero in the main diagonal, but otherwise it works perfectly.

    So if you can help me getting the switching row code, then this post will be completely solved. Here you have the code if you ever need it:


    VB Code:
    1. Public Function GaussJordan(A As Variant, B As Variant) As Variant
    2. Dim GJ() As Double
    3. Dim i As Long
    4. Dim j As Long
    5. Dim n As Long
    6. Dim Cons As Double
    7.  
    8. ReDim GJ(UBound(A, 1) + 1, UBound(A, 2))
    9.  
    10. For i = LBound(GJ, 1) To UBound(GJ, 1)
    11.  For j = LBound(GJ, 2) To UBound(GJ, 2)
    12.   If i = UBound(GJ, 1) Then
    13.    GJ(i, j) = B(LBound(B, 1), j)
    14.   Else
    15.    GJ(i, j) = A(i, j)
    16.   End If
    17.  Next j
    18. Next i
    19.  
    20. For i = LBound(GJ, 2) To UBound(GJ, 2)
    21.  Cons = GJ(i, i)
    22.  
    23.  For n = LBound(GJ, 1) To UBound(GJ, 1)
    24.   GJ(n, i) = GJ(n, i) / Cons
    25.  Next n
    26.  
    27.  For j = LBound(GJ, 2) To UBound(GJ, 2)
    28.   If j <> i Then
    29.    Cons = (-GJ(i, j))
    30.    For n = LBound(GJ, 1) To UBound(GJ, 1)
    31.     GJ(n, j) = GJ(n, j) + GJ(n, i) * Cons
    32.    Next n
    33.   End If
    34.  Next j
    35.  
    36. Next i
    37.  
    38. GaussJordan = GJ
    39. End Function
    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

  9. #9
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    VB Code:
    1. Private Sub GaussianElimination(oMatrix As CMatrix)
    2.  
    3.     Dim i As Long
    4.     Dim j As Long
    5.     Dim k As Long
    6.     Dim Max As Single
    7.     Dim Pos As Long
    8.     Dim Temp As Single
    9.     Dim Val As Single
    10.    
    11.     With oMatrix
    12.    
    13.         For i = 1 To .Rows
    14.        
    15.             Max = 0
    16.             Pos = 1
    17.            
    18.             For j = i To .Rows
    19.                 If Abs(.Element(j, i) > Max) Then
    20.                     Max = Abs(.Element(j, i))
    21.                     Pos = j
    22.                 End If
    23.             Next
    24.            
    25.             Max = .Element(Pos, i)
    26.            
    27.             For j = 1 To .Cols
    28.                 Temp = .Element(Pos, j)
    29.                 .Element(Pos, j) = .Element(i, j)
    30.                 .Element(i, j) = Temp / Max
    31.             Next
    32.            
    33.             For k = 1 To .Rows
    34.                 If Not (k = i) Then
    35.                     Val = .Element(k, i)
    36.                     For j = i To .Cols
    37.                         .Element(k, j) = .Element(k, j) - Val * (.Element(i, j))
    38.                     Next
    39.                 End If
    40.             Next
    41.            
    42.         Next
    43.        
    44.     End With
    45.    
    46. End Sub

    I do not know who the original author is - but it's not me: I converted it from C++

    You'll also need this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private mArr() As Single
    4.  
    5. Private Sub Class_Initialize()
    6. '
    7. End Sub
    8.  
    9. Private Sub Class_Terminate()
    10. '
    11. End Sub
    12.  
    13. Public Sub Create(i As Long, j As Long)
    14.     ReDim mArr(i - 1, j - 1)
    15. End Sub
    16.  
    17. Public Sub Destroy()
    18.     Erase mArr
    19. End Sub
    20.  
    21. Public Property Get Rows() As Long
    22.     Rows = UBound(mArr, 1) + 1
    23. End Property
    24.  
    25. Public Property Get Cols() As Long
    26.     Cols = UBound(mArr, 2) + 1
    27. End Property
    28.  
    29. Public Property Get Element(i As Long, j As Long) As Single
    30.     Element = mArr(i - 1, j - 1)
    31. End Property
    32.  
    33. Public Property Let Element(i As Long, j As Long, RHS As Single)
    34.     mArr(i - 1, j - 1) = RHS
    35. End Property

    Hope this helps.

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