Vous êtes sur la page 1sur 8

'

'
'
'
'
'

----------------------------Theory of Plates and Shells


----------------------------by:
ALEXIUS S. ACADEMIA
e-mail:
alexius.academia@gmail.com
web:
http://milesofinfo.net

' Programming assignment: Parse an equation to rearrange coefficient in matrix form


' Description: This module parses a list of equations and convert its coefficients
into
'
matrix form then solves the unknowns. This assignment was formulated
'
with the problem of rearranging coefficients of equations on the
solution
'
of finite difference method on plates with curved boundaries.
'
------------------------------------------------------------------------------------' USAGE:
'
1. Make sure there is only one worksheet on the excel workbook.
'
2. Put the list of equations on the first column starting from the 2nd row
(A2)
'
3. Put the variable identifier on cell "C2". This will be the variable
letter
'
The equation format 3x1 + 2x4 - 5x2 = 9 has a variable identifier 'x'.
'
Variable identifier is case sensitive.
'
4. Go to macro on the developer tab and run the 'Parse Click' macro.
'
5. You will see the vector B on column "D" followed by the matrix "A" on
'
the next column. The answers will be on column "D" following the vector
B.
'
A working copy of excel file can be downloaded at
'
http://www.milesofinfo.net/wp-content/uploads/2016/11/EquationParser.xlsm
'
-------------------------------------------------------------------------------------' ///////////////////////////
' Start of source code
' //////////////////////////
Public numberOfEquations As Integer
equations in the yellow cells
' Function to count the number of equations
Function countEquation() As Integer
Dim hasEquation As Boolean
Dim iterator As Integer

' Hold the number of

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

' Code for parsing all of the equations


'
and displaying the left and right side of the
'
equation in matrix and vector form respectively
Public Sub Parse_Click()
' First clear the outputs
clearVectorB
clearMatrixA
' Count the number of equation
Dim equations As Integer
numberOfEquations = countEquation()
' Variable to hold the array of coefficients
Dim coefficients() As String
' Iterate to every equation
Dim i, j As Integer
Dim equation As String
For i = 0 To numberOfEquations - 1
' Get equation one at a time
equation = Cells(i + 2, 1).Value
' Parse the equation and get the coefficients on the left side in order
coefficients = Split(ArrangeEquation(equation), ",")
For j = 0 To numberOfEquations - 1
' Display the coefficients in matrix form
Cells(i + 2, j + 5).Value = CStr(coefficients(j))
Next j
' Generate vector B
Cells(i + 2, 4).Value = Split(equation, "=")(1)

Next i
' Call the functions for solving the matrix operation using built-in excel
functions
solveSimultaneous
End Sub

' Function to count the terms of an equation


' This excludes the terms on the right side of the equation
Function countTerms(equation As String) As Integer
Dim rawEq As String
Dim eqTerms() As String
' Put comma delimiter on the equation term by term
rawEq = Replace(equation, "+", ",+")
rawEq = Replace(rawEq, " ", "")
rawEq = Replace(rawEq, "'", "")
rawEq = Replace(rawEq, "-", ",-")
rawEq = Replace(rawEq, "=", ",")
' Replace equal sign with comma to
separate the right side
eqTerms = Split(rawEq, ",")
countTerms = UBound(eqTerms) - 1
End Function

' List the terms


' Count the items on the list

' Arrange the terms accordingly based on the variable index


'
(e.g. x1 x2 x3 etc.)
Function ArrangeEquation(equation As String) As String
Dim pair As String
' Temporary placeholder of a pair
Dim pairs As String
' Placeholder for list of pairs
Dim eq As String
Dim terms() As String
eq = Replace(equation, "+", ",+")
eq = Replace(eq, "'", "")
eq = Replace(eq, "-", ",-")
eq = Replace(eq, "=", ",")
eq = Replace(eq, " ", "")
terms = Split(eq, ",")
' Loop through the terms
Dim i, j As Integer
Dim indices As String
Dim coefficients As String
For i = 0 To UBound(terms) - 1
If indices = "" Then

' List of indices in string (unordered)


' List of coefficients (unordered)

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

' Function to get the coefficient of a term


Function GetCoefficient(term As String) As String
On Error GoTo ErrorHandler
term = Replace(term, " ", "")
' Get the identifier
Dim var As String
var = Cells(2, 3)
Dim arr() As String
arr = Split(term, var)
If arr(0) = "+" Then
GetCoefficient = "1"
ElseIf arr(0) = "-" Then
GetCoefficient = "-1"
Else
GetCoefficient = Replace(arr(0), "+", "")
End If
If GetCoefficient = "" Then
GetCoefficient = "1"
End If
ErrorHandler:
Exit Function
End Function

' Get the index of the term


' The number after the variable
Function GetIndex(term As String) As Integer
On Error GoTo ErrorHandler
term = Replace(term, " ", "")
' Get the identifier
Dim var As String
var = Cells(2, 3)
Dim arr() As String
arr = Split(term, var)
GetIndex = arr(1)
ErrorHandler:
Exit Function
End Function

' Clear the equations (inputs)


Public Sub clearEquations()
Dim hasEquation As Boolean
hasEquation = True
Dim i As Integer
Do While hasEquation = True
If Cells(i + 2, 1).Value = "" Then
hasValue = False
Else
Cells(i + 2, 1).Value = ""
End If
i = i + 1
Loop
End Sub

' Clear Matrix A


Public Sub clearMatrixA()
Dim hasValue As Boolean
Dim iterator As Integer
iterator = 0
hasValue = True
Do While hasValue = True
If Cells(iterator + 2, 1).Value <> "" Then
iterator = iterator + 1
Else
hasValue = False
End If
Loop
Dim i, j As Integer
For i = 0 To iterator
For j = 0 To iterator
Cells(i + 2, j + 5).Value = ""
Next j
Next i
End Sub

' Function to clear vector B


Public Sub clearVectorB()
Dim hasValue As Boolean
Dim i As Integer

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

Vous aimerez peut-être aussi