Gauss Elimination in Visual Basic-VBForums

Thread: Gauss Elimination in Visual Basic

1. 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```

2. Re: Gauss Elimination in Visual Basic

Moved to Office Development

3. 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. 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.

5. 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