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

GRASP Python (9)

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)
3 views1 page

GRASP Python (9)

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
You are on page 1/ 1

LogError Err.Number, 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 = ThisWorkbook.Worksheets.Add

' 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
ws.Cells(i, j).Value = K(i, j)
Next j
Next i

' Write F vector


For i = 1 To size
ws.Cells(i, size + 1).Value = F(i)
Next i

' Solve using MINVERSE and MMULT


Dim K_inv() As Variant
K_inv = WorksheetFunction.MInverse(ws.Range(ws.Cells(1, 1),
ws.Cells(size, size)))

Dim disp_vector As Variant


disp_vector = WorksheetFunction.MMult(K_inv, ws.Range(ws.Cells(1, size +
1), ws.Cells(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
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True

SolveDirect = result
Exit Function

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

P a g e 57 | 62

You might also like