Académique Documents
Professionnel Documents
Culture Documents
'
'
'
'
'
iterator = 0
hasEquation = True
Do While hasEquation = True
If Cells(iterator + 2, 1).Value <> "" Then
iterator = iterator + 1
Else
hasEquation = False
End If
Loop
countEquation = iterator
End Function
Next i
' Call the functions for solving the matrix operation using built-in excel
functions
solveSimultaneous
End Sub
indices = CStr(GetIndex(terms(i)))
Else
indices = indices + "," + CStr(GetIndex(terms(i)))
End If
If coefficients = "" Then
coefficients = CStr(GetCoefficient(terms(i)))
Else
coefficients = coefficients + "," + CStr(GetCoefficient(terms(i)))
End If
Next
Dim indexArray() As String
Dim coefficientArray() As String
indexArray = Split(indices, ",")
coefficientArray = Split(coefficients, ",")
Dim numberOfEquations As Integer
numberOfEquations = countEquation()
' Populate the coefficients array by index
Dim orderedCoefficients() As String
Dim ordered As String
Dim pos As Integer
pos = 0
For i = 1 To numberOfEquations
' Find i+1 on the list of indexArray if there is a match
For j = 0 To UBound(terms) - 1
If indexArray(j) = i Then
If ordered = "" Then
ordered = CStr(coefficientArray(j))
Else
ordered = ordered + "," + CStr(coefficientArray(j))
End If
pos = pos + 1
End If
Next j
If pos < i Then
If ordered = "" Then
ordered = "0"
Else
ordered = ordered + ",0"
End If
pos = pos + 1
End If
Next i
ArrangeEquation = ordered
End Function
i = 0
hasValue = True
Do While hasValue = True
If Cells(i + 2, 4).Value <> "" Then
Cells(i + 2, 4).Value = ""
Else
hasValue = False
End If
i = i + 1
Loop
End Sub
' Solve the matrix using excel built-in function MInverse and
'
MMult
Public Sub solveSimultaneous()
Dim matrixA As Variant
Dim vectorB As Variant
Dim ansX As Variant
ReDim matrixA(1 To countEquation(), 1 To countEquation())
ReDim vectorB(1 To countEquation(), 1 To 1)
Dim i, j, size As Integer
size = countEquation()
' Populate the vector
For i = 1 To size
vectorB(i, 1) = Cells(i + 1, 4)
Next i
' Populate the matrix
For i = 1 To size
For j = 1 To size
matrixA(i, j) = Cells(i + 1, j + 4)
Next j
Next i
' Calculate for vector X
ansX = WorksheetFunction.MMult(WorksheetFunction.MInverse(matrixA), vectorB)
' Show the answer
For i = 1 To size
Cells(i + size + 2, 4) = ansX(i, 1)
Next i
i = 1
Cells(i + size + 1, 4) = "ANSWERS"
End Sub