Performs Singular Value Decomposition (SVD) of the current matrix object. Matrix decomposes to the product of UxSxVT.
MatrixObject.SVD U, S, V
If any of U, S or V parameters is not set or omitted, this parameter is not calculated.
The SVD method is supported only in Advanced and Advanced II editions of ShortName .
Error 1316 will be returned if SVD method fails to complete.
This example demonstrates the use of SVD method. It finds a least-squares solution to an over-determined set of linear equations. The following equations are used:
A x X = B
X = A-1 x B
A = U x S x VT (Singular value decomposition of A)
A-1 = V x S-1 x UT (Generalized inverse or pseudoinverse of A)
X = V x S-1 x UT x B
Private Sub MatrixSVD()
Dim A As New Matrix, U As New Matrix, S As New Matrix, V As New Matrix
Dim B As New Matrix, X As Matrix
Dim i As Long
A.Size 6, 3
A(0, 0) = 6: A(0, 1) = 4: A(0, 2) = 3
A(1, 0) = 2: A(1, 1) = 3: A(1, 2) = 7
A(2, 0) = 1: A(2, 1) = 7: A(2, 2) = 2
A(3, 0) = 6: A(3, 1) = 1: A(3, 2) = 4
A(4, 0) = 8: A(4, 1) = 7: A(4, 2) = 3
A(5, 0) = 3: A(5, 1) = 2: A(5, 2) = 9
B.Size 6, 1
B(0, 0) = 55
B(1, 0) = 37
B(2, 0) = 40
B(3, 0) = 33
B(4, 0) = 60
B(5, 0) = 75
Debug.Print "Over determined set of equations"
Debug.Print "================================"
For i = 0 To 5
Debug.Print " " & A(i, 0) & "*x + " & A(i, 1) _
& "*y + " & A(i, 2) & "*z = " & B(i, 0)
Next
Debug.Print "================================"
A.SVD U, S, V
'Matrix S is diagonal - to form its inverse we need only
'find the reciprocals of its diagonal elements
For i = 0 To 2
'in real code you will need to check
'for zero values here
S(i, i) = 1 / S(i, i)
Next
Set X = V.Times(S).Times(U.Transpose).Times(B)
Debug.Print "Least square fit solution"
Debug.Print "x ="; X(0, 0)
Debug.Print "y ="; X(1, 0)
Debug.Print "z ="; X(2, 0)
End Sub
Over determined set of equations
================================
6*x + 4*y + 3*z = 55
2*x + 3*y + 7*z = 37
1*x + 7*y + 2*z = 40
6*x + 1*y + 4*z = 33
8*x + 7*y + 3*z = 60
3*x + 2*y + 9*z = 75
================================
Least square fit solution
x = 2.63293929474354
y = 3.66652382290048
z = 5.20251471911851