急求:用vb编写的矩阵求逆的算法,谢谢!

急求:用vb编写的矩阵求逆的算法,谢谢!,第1张

Function Rect_yu(A() As Double, L As Long, C() As Double) As Double 矩阵求逆磨逗橘 Dim T0 As Double Dim T1 As Double Dim T2 As Double Dim T3 As Double Dim B() As Double Dim Num As Double Dim Chay As Long Dim Chax As Long Chay = 0 Chax = 0 ReDim B(L - 1, L - 1) Num = 0 Dim add As Double add = 1 /指拿 Rect(A(), L) For T0 = 0 To L For T3 = 0 To L For T1 = 0 To L - 1 If T1 <T0 Then Chax = 0 Else Chax = 1 End If For T2 = 0 To L - 1 If T2 <T3 Then Chay = 0 Else Chay = 1 End If B(T1, T2) = A(T1 + Chax, T2 + Chay) Next T2 Next T1 Rect(B(), L - 1) 调用求行列瞎团式值 C(T3, T0) = Rect(B(), L - 1) * add * ((-1) ^ (T0 + T3)) Next T3 Next T0End Function

用下面这个函数求逆,你须先把矩阵保存到二维数组中,并以参数Matrix传给函数;另外须用一个空二维数组装载所求得的逆,它传给参数Reversal。n为矩阵维数。Matrix与Reversal都须是n维的。代码如下。

Public Function Reverse( _

ByVal Matrix(,) As Double, _

ByVal n As Integer, _

ByRef Reversal(,) As Double _

) As Boolean

Rem 这个函数采用的是高斯消元法

Dim i As Integer, j As Integer, k As Integer

‘ 将Reversal初始化为单位阵(E)

For i = 0 To n - 1

For j = 0 To n - 1

Reversal(i, j) = IIf(i = j, 1, 0)

Next

Next

' 逐行消元求逆(Kernal)

For i = 0 To n - 1

For j = i To n - 1 '往后寻找一个第i列不为0的行,然后把它换到第i行,以备消元

If Matrix(j, i) = 0 Then Return False

Next

Dim tmp As Double

If i <>j Then'交换i, j两行

If j = n Then Return False

For k = 0 To n - 1

tmp = Matrix(i, k) : Matrix(i, k) = Matrix(j, k) : Matrix(j, k) = tmp

tmp = Reversal(i, k) : Reversal(i, k) = Reversal(j, k) : Reversal(j, k) = tmp

Next

End If

For j = 0 To n - 1'消元

If j = i Then Continue For

tmp = Matrix(j, i) / Matrix(i, i)

For k = 0 To n - 1

Matrix(j, k) = Matrix(j, k) - Matrix(i, k) * tmp

Reversal(j, k) = Reversal(j, k) - Reversal(i, k) * tmp

Next

Next

tmp = Matrix(i, i)

For k = 0 To n - 1'将Matrix化为单位阵,得到的Reversal即是它的逆

Reversal(i, k) = Reversal(i, k) / tmp

Matrix(i, k) = Matrix(i, k) / tmp

Next

Next

Return True

End Function

如果Matrix不可逆,那么该函数返回false,Reversal的值无效。

如果可逆,那么该函数返回true,Reversal为其逆阵。

1、求逆的必要条伏谈唤件是N阶方阵A的行列式不等侍盯于0

2、然后求N阶方阵A的行列式的值|A|

3、求N阶方阵A的伴随缺凯矩阵A*

4、N阶方阵A的逆矩阵=A*/|A|


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12452072.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存