使用 Python 3 的不需要的无限 While 循环
Unwanted Infinite While Loop with Python 3
v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt
while v > 0:
print(v,t)
v0 = v
t = t+dt
我的意图是,在第一次检查 v>0 之后,我们在重新执行检查之前让 v0 等于 v。
相反,我看到了第一个计算出的无限流 v = 0.9
您需要在每次循环中使用更新后的参数重新计算 v
:
import numpy as np
v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt
while v > 0:
print(v,t)
v0 = v
t = t+dt
v = v0 - alpha*np.sqrt(v0)*dt
输出:
0.9 0
0.805131670195 0.1
0.715402540577 0.2
0.630821068577 0.30000000000000004
0.551396823627 0.4
0.477140724644 0.5
0.408065350983 0.6
0.34418535795 0.7
0.28551804525 0.7999999999999999
0.232084156635 0.8999999999999999
0.183909043077 0.9999999999999999
0.141024425429 1.0999999999999999
0.103471206469 1.2
0.0713042659134 1.3
0.0446014072812 1.4000000000000001
0.0234823620184 1.5000000000000002
0.00815840625677 1.6000000000000003
v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt
while v > 0:
print(v,t)
v0 = v
t = t+dt
v = v0 - alpha*np.sqrt(v0)*dt
v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt
while v > 0:
print(v,t)
v0 = v
t = t+dt
我的意图是,在第一次检查 v>0 之后,我们在重新执行检查之前让 v0 等于 v。 相反,我看到了第一个计算出的无限流 v = 0.9
您需要在每次循环中使用更新后的参数重新计算 v
:
import numpy as np
v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt
while v > 0:
print(v,t)
v0 = v
t = t+dt
v = v0 - alpha*np.sqrt(v0)*dt
输出:
0.9 0
0.805131670195 0.1
0.715402540577 0.2
0.630821068577 0.30000000000000004
0.551396823627 0.4
0.477140724644 0.5
0.408065350983 0.6
0.34418535795 0.7
0.28551804525 0.7999999999999999
0.232084156635 0.8999999999999999
0.183909043077 0.9999999999999999
0.141024425429 1.0999999999999999
0.103471206469 1.2
0.0713042659134 1.3
0.0446014072812 1.4000000000000001
0.0234823620184 1.5000000000000002
0.00815840625677 1.6000000000000003
v0=1
alpha=1
t=0
dt=0.1
v = v0 - alpha*np.sqrt(v0)*dt
while v > 0:
print(v,t)
v0 = v
t = t+dt
v = v0 - alpha*np.sqrt(v0)*dt