asked on
ASKER
ASKER
Option Explicit
Private MatrixElements() As Double
Private NumberofRows As Integer
Sub matrix2()
Dim MatrixColumn As Integer
Dim MatrixRow As Integer
Dim l As Integer
Dim w As Double
Dim PivotRow As Integer
Dim SearchRow As Integer
Dim PivotValue As Double
NumberofRows = Sheet1.Cells(20, 3)
ReDim MatrixElements(NumberofRows, NumberofRows + 1)
' load an array with Cell values
For MatrixRow = 1 To NumberofRows
For MatrixColumn = 1 To NumberofRows + 1
MatrixElements(MatrixRow, MatrixColumn) = Sheet1.Cells(MatrixRow, MatrixColumn)
Next MatrixColumn
Next MatrixRow
' for each row of the matrix
For MatrixRow = 1 To NumberofRows
' Find the first nonzero element in the equivalent column
PivotRow = 0
For SearchRow = MatrixRow To NumberofRows
If MatrixElements(SearchRow, MatrixRow) <> 0 And PivotRow = 0 Then
PivotRow = SearchRow
End If
Next SearchRow
' If pivotrow <> matrixRow then swap the rows
If PivotRow <> MatrixRow Then
Call SwapRows(PivotRow, MatrixRow)
End If
Call Update
PivotValue = MatrixElements(MatrixRow, MatrixRow)
For MatrixColumn = 1 To NumberofRows + 1
MatrixElements(MatrixRow, MatrixColumn) = MatrixElements(MatrixRow, MatrixColumn) / PivotValue
Next MatrixColumn
Call Update
For l = 1 To NumberofRows
If l <> MatrixRow Then
w = MatrixElements(l, MatrixRow)
For MatrixColumn = 1 To NumberofRows + 1
MatrixElements(l, MatrixColumn) = MatrixElements(l, MatrixColumn) - MatrixElements(MatrixRow, MatrixColumn) * w
Next MatrixColumn
End If
Next l
Call Update
Next MatrixRow
End Sub
Sub SwapRows(Row1 As Integer, Row2 As Integer)
Dim Column As Integer
Dim tempvalue As Double
For Column = 1 To NumberofRows + 1
tempvalue = MatrixElements(Row1, Column)
MatrixElements(Row1, Column) = MatrixElements(Row2, Column)
MatrixElements(Row2, Column) = tempvalue
Next Column
End Sub
Sub Update()
Dim MatrixColumn As Integer
Dim MatrixRow As Integer
For MatrixRow = 1 To NumberofRows
For MatrixColumn = 1 To NumberofRows + 1
Sheet1.Cells(MatrixRow + 8, MatrixColumn) = MatrixElements(MatrixRow, MatrixColumn)
Next MatrixColumn
Next MatrixRow
End Sub
ASKER
ASKER
ASKER
Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.
TRUSTED BY
zero situation, which of course is arithmetically impossible.
I will not pretend to understand your method, but you need to either prevent the divisor from ever being
zero, or test for the possibility of the quotient being zero and then bypassing the division if needed.
For example, a simple rule like "if a(j, j) = 0 then make the result 0" can be implemented by replacing
If j <> i Then a(j, i) = a(j, i) / a(j, j)
with
If a(j, j) <> 0 Then
If j <> i Then a(j, i) = a(j, i) / a(j, j)
Else
a(j, i) = 0
End If