Gauss Elimination in Visual Basic-VBForums
Results 1 to 5 of 5

Thread: Gauss Elimination in Visual Basic

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    2

    Gauss Elimination in Visual Basic

    Well, I have some code here. My professor wants us to write this same program in Excel (same result) but do so without the "b" variable. Not sure 100% where to begin.
    Code:
    Option Explicit
    Private a(20, 20) As Double, b(20) As Double
    Private i As Integer, j As Integer, n As Integer
    Private k As Integer
    Private factor As Double
    Private x(20) As Double, sum As Double
    
    Sub gaussturkoglu()
    
    n = 3 'number of equations
    
    'Reading A and b
    For i = 1 To n
    For j = 1 To n
    a(i, j) = Cells(i, j)
    Next j
    b(i) = Cells(i, n + 1)
    Next i
    
    'Forward Elimination
    For k = 1 To n - 1
        For i = k + 1 To n Step 1
            factor = a(i, k) / a(k, k)
            For j = k + 1 To n
                a(i, j) = a(i, j) - factor * a(k, j)
            Next j
            b(i) = b(i) - factor * b(k)
        Next i
    Next k
    
    'Outputing A and b after forward elimination
    For i = 1 To n
    For j = 1 To n
    Cells(n + 1 + i, j).Value = a(i, j)
    Next j
    Cells(n + 1 + i, n + 1) = b(i)
    Next i
    
    'Back Substitution
    x(n) = b(n) / a(n, n)
    For i = n - 1 To 1 Step -1
        sum = 0
        For j = n To i + 1 Step -1 'or j=i+1 to n
            sum = sum + a(i, j) * x(j)
        Next j
        x(i) = (b(i) - sum) / a(i, j)
    Next i
    
    'Output x
    For i = 1 To n
        Cells(i, n + 3) = x(i)
    Next i
    End Sub
    Last edited by Hack; Oct 11th, 2007 at 05:33 AM. Reason: Added Code Tags

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Gauss Elimination in Visual Basic

    Moved to Office Development
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    589

    Re: Gauss Elimination in Visual Basic

    Perhaps your professor means using the augmented matrix technique?

    http://www.math.csusb.edu/math110/sr...s/sol-sys.html

  4. #4
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Re: Gauss Elimination in Visual Basic

    Quote Originally Posted by hed0nisticpat View Post
    Well, I have some code here. My professor wants us to write this same program in Excel (same result) but do so without the "b" variable. Not sure 100% where to begin.
    Code:
    Option Explicit
    Private a(20, 20) As Double, b(20) As Double
    Private i As Integer, j As Integer, n As Integer
    Private k As Integer
    Private factor As Double
    Private x(20) As Double, sum As Double
    
    Sub gaussturkoglu()
    
    n = 3 'number of equations
    
    'Reading A and b
    For i = 1 To n
    For j = 1 To n
    a(i, j) = Cells(i, j)
    Next j
    b(i) = Cells(i, n + 1)
    Next i
    
    'Forward Elimination
    For k = 1 To n - 1
        For i = k + 1 To n Step 1
            factor = a(i, k) / a(k, k)
            For j = k + 1 To n
                a(i, j) = a(i, j) - factor * a(k, j)
            Next j
            b(i) = b(i) - factor * b(k)
        Next i
    Next k
    
    'Outputing A and b after forward elimination
    For i = 1 To n
    For j = 1 To n
    Cells(n + 1 + i, j).Value = a(i, j)
    Next j
    Cells(n + 1 + i, n + 1) = b(i)
    Next i
    
    'Back Substitution
    x(n) = b(n) / a(n, n)
    For i = n - 1 To 1 Step -1
        sum = 0
        For j = n To i + 1 Step -1 'or j=i+1 to n
            sum = sum + a(i, j) * x(j)
        Next j
        x(i) = (b(i) - sum) / a(i, j)
    Next i
    
    'Output x
    For i = 1 To n
        Cells(i, n + 3) = x(i)
    Next i
    End Sub


    I applied your code and it seemed it helped me. But what I cant understand clearly is why is it important to read the matrix and the vector (refering to the portion "'Reading A and b"). How does it work?

    Thank you.

  5. #5
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Re: Gauss Elimination in Visual Basic

    I applied your code and it seemed it helped me. But what I cant understand clearly is why is it important to read the matrix and the vector (refering to the portion "'Reading A and b"). How does it work?

    Thank you.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.