Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am told to write a code for gaussian elimination in Excel VBA using a data mat

ID: 3803263 • Letter: I

Question

I am told to write a code for gaussian elimination in Excel VBA using a data matrix from an excel worksheet.

I was given most of the code for it in class and am expected to write the elimination and back substitution parts to complete the code.

I have very little understanding of arrays (Both 1D and multidimensional) so I am finding this nearly impossible to figure out.

In bold I have my attempt at writing the elimination portion of the code.

Any help with understanding how to write the multi dimensional arrays required is much appreciated!

Option Explicit
Option Base 1

Sub gauss()
Dim A As Variant, b As Variant, MyRange As Variant
Dim AugMatrix() As Double, x() As Double, f As Double
Dim i As Integer, j As Integer, k As Integer, n As Integer, m As Integer

A = Application.InputBox("Please select coefficient matrix", Type:=64)
b = Application.InputBox("Please select constant vector", Type:=64)

'check if solution exists
If Application.WorksheetFunction.MDeterm(A) = 0 Then
MsgBox ("Solution does not exist")
Exit Sub
End If

'Define DIM for AugMatrix
'Count how many rows for coefficient matrix A
n = UBound(A, 1)

'now define dimensions for augmented matrix and x vector
ReDim AugMatrix(n, n + 1), x(n, 1)

For i = 1 To n
For j = 1 To n + 1
If j = n + 1 Then
AugMatrix(i, j) = b(i, 1)
Else
AugMatrix(i, j) = A(i, j)
End If
Next j
Next i
  
'Following checks previous code

Set MyRange = Application.InputBox("Please select cells to output augmented matrix before elimination", Type:=8)
MyRange.Value = AugMatrix

'Saves aik value
Dim pre_aik As Double

'===========Do elimination Here=========
For k = 1 To n - 1
For i = k + 1 To n
For j = k + 1 To n + 1
pre_aik = x(i, n + 1) - (x(i, n - 1) / x(k, n - 1)) * x(k, n + 1)
Next j
Next i
Next k

'Check if elimination is correct

Set MyRange = Application.InputBox("Please select cells to output augmented matrix after elimination", Type:=8)
MyRange.Value = AugMatrix

Dim sum_known As Double

'======Do Back substitution here========

'=======================================

Set MyRange = Application.InputBox("Please select cells to output result vector", Type:=8)
MyRange.Value = x


End Sub

Explanation / Answer

Write below code

For k = 1 To n - 1 do
For i = k + 1 To n do

mij = aij/ajj
For j = k + 1 To n + 1
// pre_aik = x(i, n + 1) - (x(i, n - 1) / x(k, n - 1)) * x(k, n + 1)

  pre_aik =aik - mij*ajk

end

bi=bi-mijbj
Next j
Next i
Next k

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote