达到精度了,打印出来就知道了import numpy as np
def mifa(A):
v0 = u0 = np.ones((3, 1))
m0 = np.max(v0)
v = A * u0
m = np.max(v)
u = v / m
i = 1
while abs(m - m0) >= 0.00001:
u0 = u
m0 = m
# print(m0)
v = A * u0
m = np.max(v)
print("{: >.10f} {: >.10f}".format(m0, m))
u = v / m
i += 1
print("在第" + str(i) + "次迭代后获得最大特征值:" + str(m))
A = np.matrix([[12, 6, -6], [6, 16, 2], [-6, 2, 16]])
mifa(A)
F:\Python\Test>python test.py
24.0000000000 20.0000000000
20.0000000000 19.4000000000
19.4000000000 19.3814432990
19.3814432990 19.4957446809
19.4957446809 19.6437847866
19.6437847866 19.8007289051
19.8007289051 19.9588019479
19.9588019479 20.1142734590
20.1142734590 20.2645338386
20.2645338386 20.4075077890
20.4075077890 20.5415810119
20.5415810119 20.6656091315
20.6656091315 21.2401486149
21.2401486149 21.5440037396
21.5440037396 21.5440037441
在第16次迭代后获得最大特征值:21.544003744145076
F:\Python\Test>
|