while为什么没达到判断条件就结束循环了
代码是关于幂法的计算def mifa(A):
import numpy as np
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)
u=v/m
i+=1
print("在第"+str(i)+"次迭代后获得最大特征值:"+str(m))
import numpy as np
A=np.matrix([,,[-6,2,16]])
mifa(A)
可以看出让它在进行循环 达到一定的误差才可以跳出,但是计算结果总是在16次迭代停止并跳出,24.0
20.0
19.4
19.381443298969074
19.495744680851065
19.643784786641927
19.800728905086782
19.95880194789526
20.114273458980524
20.26453383864906
20.407507789000007
20.541581011888532
20.66560913148607
21.24014861493221
21.544003739648907
在第16次迭代后获得最大特征值:21.544003744145076
此时明显未达到精度,但是它的前面的计算都是正确的,为什么到这一步就算不下去了呢 达到精度了,打印出来就知道了
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([, , [-6, 2, 16]])
mifa(A)
F:\Python\Test>python test.py
24.000000000020.0000000000
20.000000000019.4000000000
19.400000000019.3814432990
19.381443299019.4957446809
19.495744680919.6437847866
19.643784786619.8007289051
19.800728905119.9588019479
19.958801947920.1142734590
20.114273459020.2645338386
20.264533838620.4075077890
20.407507789020.5415810119
20.541581011920.6656091315
20.665609131521.2401486149
21.240148614921.5440037396
21.544003739621.5440037441
在第16次迭代后获得最大特征值:21.544003744145076
F:\Python\Test> hrpzcf 发表于 2021-11-14 19:26
达到精度了,打印出来就知道了
还是这样子的 十分感谢
页:
[1]