用vb如何求一个n阶矩阵的特征值和特征向量楼下给出的答案,只能求对称矩阵,我需要的是求一般矩阵方法的代码.同样还是很感谢您
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/15 06:32:19
用vb如何求一个n阶矩阵的特征值和特征向量楼下给出的答案,只能求对称矩阵,我需要的是求一般矩阵方法的代码.同样还是很感谢您
用vb如何求一个n阶矩阵的特征值和特征向量
楼下给出的答案,只能求对称矩阵,我需要的是求一般矩阵方法的代码.同样还是很感谢您
用vb如何求一个n阶矩阵的特征值和特征向量楼下给出的答案,只能求对称矩阵,我需要的是求一般矩阵方法的代码.同样还是很感谢您
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 模块名:MatrixModule.bas
' 函数名:MJacobiEigenv
' 功能:用雅可比法(Jacobi)计算对称矩阵的特征值和特征向量
' 参数:n - Integer型变量,对称矩阵的阶数.
' dblA - Double型二维数组,体积为n x n.存放对称矩阵;返回时,对角线上存放求得的n个特征值.
' dblV - Double型二维数组,体积为n x n.返回n个特征向量,其中第i列为第i个特征值dblA(i,i)对应的特征向量.
' eps - Double型变量.迭代过程中的控制精度参数.
' nMaxItNum - Integer.为求得一个特征值所允许的最大迭代次数.
' 返回值:Boolean型.False,失败无解;True,成功
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function MJacobiEigenv(n As Integer,dblA() As Double,dblV() As Double,eps As Double,nMaxItNum As Integer) As Boolean
' 局部变量
Dim i As Integer,j As Integer,p As Integer,q As Integer,l As Integer
Dim fm As Double,cn As Double,sn As Double,omega As Double,x As Double,y As Double,d As Double
l = 1
For i = 1 To n
dblV(i,i) = 1#
For j = 1 To n
If (i j) Then dblV(i,j) = 0#
Next j
Next i
While (True)
fm = 0#
For i = 2 To n
For j = 1 To i - 1
d = Abs(dblA(i,j))
If ((i j) And (d > fm)) Then
fm = d
p = i
q = j
End If
Next j
Next i
If (fm < eps) Then
MJacobiEigenv = True
Exit Function
End If
If (l > nMaxItNum) Then
MJacobiEigenv = False
Exit Function
End If
l = l + 1
x = -dblA(p,q)
y = (dblA(q,q) - dblA(p,p)) / 2#
omega = x / Sqr(x * x + y * y)
If (y < 0#) Then omega = -omega
sn = 1# + Sqr(1# - omega * omega)
sn = omega / Sqr(2# * sn)
cn = Sqr(1# - sn * sn)
fm = dblA(p,p)
dblA(p,p) = fm * cn * cn + dblA(q,q) * sn * sn + dblA(p,q) * omega
dblA(q,q) = fm * sn * sn + dblA(q,q) * cn * cn - dblA(p,q) * omega
dblA(p,q) = 0#
dblA(q,p) = 0#
For j = 1 To n
If ((j p) And (j q)) Then
fm = dblA(p,j)
dblA(p,j) = fm * cn + dblA(q,j) * sn
dblA(q,j) = -fm * sn + dblA(q,j) * cn
End If
Next j
For i = 1 To n
If ((i p) And (i q)) Then
fm = dblA(i,p)
dblA(i,p) = fm * cn + dblA(i,q) * sn
dblA(i,q) = -fm * sn + dblA(i,q) * cn
End If
Next i
For i = 1 To n
fm = dblV(i,p)
dblV(i,p) = fm * cn + dblV(i,q) * sn
dblV(i,q) = -fm * sn + dblV(i,q) * cn
Next i
Wend
End Function