
Oct 11th, 2007, 03:01 AM
#1
Thread Starter
New Member
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

Oct 11th, 2007, 05:34 AM
#2
Re: Gauss Elimination in Visual Basic
Moved to Office Development

Oct 16th, 2007, 11:32 AM
#3
Fanatic Member
Re: Gauss Elimination in Visual Basic
Perhaps your professor means using the augmented matrix technique?
http://www.math.csusb.edu/math110/sr...s/solsys.html

Jul 22nd, 2011, 03:08 PM
#4
New Member
Re: Gauss Elimination in Visual Basic
Originally Posted by hed0nisticpat
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.

Jul 22nd, 2011, 03:08 PM
#5
New Member
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

Forum Rules

Click Here to Expand Forum to Full Width
Survey posted by VBForums.
