Results 1 to 5 of 5

Thread: Gauss Elimination in Visual Basic

Threaded View

  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

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