Below is some code that inverses a 3 x 3 matrix. Note that the
matrices' are 1-based in my example below.
(Mind the word wraps)
Option Explicit
' This function returns the determinant of a 2 x 2 matrix
' a b
' c d
Private Function Det2D(a As Double, b As Double, c As Double, d As
Double) As Double
Det2D = a * d - b * c
End Function
' This function returns the determinant of a 3 x 3 matrix
Private Function Determinant(M() As Double) As Double
Determinant = M(1, 1) * (M(2, 2) * M(3, 3) - M(2, 3) * M(3, 2)) _
- M(1, 2) * (M(2, 1) * M(3, 3) - M(2, 3) * M(3, 1)) _
+ M(1, 3) * (M(2, 1) * M(3, 2) - M(2, 2) * M(3, 1))
End Function
' This function returns the Inverse of a 3 x 3 matrix
Private Function Inverse(M() As Double) As Double()
Dim det As Double
det = Determinant(M)
If det = 0 Then
' Handle the error as you wish ...
MsgBox "Inversion error! Matrix is singular and thus
non-invertible"
Exit Function
End If
Dim I(1 To 3, 1 To 3) As Double
I(1, 1) = Det2D(M(2, 2), M(2, 3), M(3, 2), M(3, 3)) / det
I(1, 2) = -Det2D(M(1, 2), M(1, 3), M(3, 2), M(3, 3)) / det
I(1, 3) = Det2D(M(1, 2), M(1, 3), M(2, 2), M(2, 3)) / det
I(2, 1) = -Det2D(M(2, 1), M(2, 3), M(3, 1), M(3, 3)) / det
I(2, 2) = Det2D(M(1, 1), M(1, 3), M(3, 1), M(3, 3)) / det
I(2, 3) = -Det2D(M(1, 1), M(2, 1), M(1, 3), M(2, 3)) / det
I(3, 1) = Det2D(M(2, 1), M(2, 2), M(3, 1), M(3, 2)) / det
I(3, 2) = -Det2D(M(1, 1), M(1, 2), M(3, 1), M(3, 2)) / det
I(3, 3) = Det2D(M(1, 1), M(1, 2), M(2, 1), M(2, 2)) / det
Inverse = I
End Function
' Invert a matrix when the form loads and display the results
' in the Immediate pane
Private Sub Form_Load()
Dim M(1 To 3, 1 To 3) As Double
Dim I() As Double
M(1, 1) = 1
M(1, 2) = 3
M(1, 3) = 3
M(2, 1) = 1
M(2, 2) = 4
M(2, 3) = 3
M(3, 1) = 1
M(3, 2) = 3
M(3, 3) = 4
I = Inverse(M)
Debug.Print I(1, 1), I(1, 2), I(1, 3)
Debug.Print I(2, 1), I(2, 2), I(2, 3)
Debug.Print I(3, 1), I(3, 2), I(3, 3)
End Sub
With the inversion done, it is a simple problem of multiplying the
inverse matrix with another matrix to get the result. The
multiplication part is left as an exercise to the reader ... ;-)
Hope this helps.
Suresh