0% found this document useful (0 votes)
7 views1 page

Excel Direct Solver with Error Handling

The document describes a VBA function called SolveDirect that solves a system of equations using Excel's matrix functions while incorporating error handling. It creates a new worksheet to input the K matrix and F vector, computes the inverse of the K matrix, and multiplies it by the F vector to find the solution. The function also includes error handling for singular matrices and other potential errors during execution.

Uploaded by

Le Fondateur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Excel Direct Solver with Error Handling

The document describes a VBA function called SolveDirect that solves a system of equations using Excel's matrix functions while incorporating error handling. It creates a new worksheet to input the K matrix and F vector, computes the inverse of the K matrix, and multiplies it by the F vector to find the solution. The function also includes error handling for singular matrices and other potential errors during execution.

Uploaded by

Le Fondateur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LogError [Link], Err.

Description, "PerformAnalysis"
End Sub

Function SolveDirect(K() As Double, F() As Double) As Double()


' Direct solver using Excel's matrix functions with error handling
On Error GoTo SolverError

Dim ws As Worksheet
Set ws = [Link]

' Write K matrix


Dim size As Integer
size = UBound(K, 1)

Dim i As Integer, j As Integer


For i = 1 To size
For j = 1 To size
[Link](i, j).Value = K(i, j)
Next j
Next i

' Write F vector


For i = 1 To size
[Link](i, size + 1).Value = F(i)
Next i

' Solve using MINVERSE and MMULT


Dim K_inv() As Variant
K_inv = [Link]([Link]([Link](1, 1),
[Link](size, size)))

Dim disp_vector As Variant


disp_vector = [Link](K_inv, [Link]([Link](1, size +
1), [Link](size, size + 1)))

' Convert to 1D array


Dim result() As Double
ReDim result(1 To size)
For i = 1 To size
result(i) = disp_vector(i, 1)
Next i

' Clean up
[Link] = False
[Link]
[Link] = True

SolveDirect = result
Exit Function

SolverError:
' Handle singular matrix or other errors
If [Link] = 1004 Then ' Matrix is singular
MsgBox "System matrix is singular. Structure may be unstable or
improperly supported.", vbExclamation
Else
MsgBox "Solver error: " & [Link], vbCritical

P a g e 57 | 62

You might also like