1. (20 points) Write a VBA code that can find the determinant of any size matrix
ID: 671313 • Letter: 1
Question
1. (20 points) Write a VBA code that can find the determinant of any size matrix that is given by user.
3. (30 points) Develop a VBA program that can solve a problem like question 2 using LU decomposition with the input values read from the spreadsheet by your program.
Questions 2:
Solve the following linear system of equations:
2x1- 6x2 - x3 = -38
-3x1- x2 +7x3 = -34
-8x1 + x2 -2x3 = -20
I've solved the linear system but i have the problem with the VBA part. I don't really know what should i write in the VBA in excel, Hope you can help, thank you very much.
Explanation / Answer
Public Function Det(ByRef a(,) As Double, ByVal n As Integer) As Double
If Not Math.Sqrt(a.Length) - 1 = n Then
Throw New Exception("Error, size is not correct")
Exit Function
End If
Dim Result As Double = 0
If n = 1 Then Return a(1, 1)
If n = 2 Then Return a(1, 1) * a(2, 2) - a(2, 1) * a(1, 2)
Dim b(n - 1, n - 1) As Double
For i As Integer = 1 To n
For j As Integer = 1 To n - 1
For k As Integer = 1 To n - 1
If k >= i Then
b(j, k) = a(j + 1, k + 1)
Else
b(j, k) = a(j + 1, k)
End If
Next
Next
Result += ((-1) ^ (i + 1)) * a(1, i) * Det(b, n - 1)
Next
Return Result
End Function
Here is an example how to use this function:
Dim Size As Integer = 3
Dim a(Size, Size) As Double
a(1, 1) = 1
a(1, 2) = 2
a(1, 3) = 3
a(2, 1) = 4
a(2, 2) = -5
a(2, 3) = 6
a(3, 1) = 7
a(3, 2) = 8
a(3, 3) = 9
Dim R As Double = Det(a, 3)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.