我有一个关于嵌套while循环的问题。
我想运行多个值的分布计算。这里mu
和kappa
。
import numpy as np
import matplotlib.pyplot as pl
import scipy.special as sps
mu = 1/9 # circular mean phase
mu_steps = 2/9
mu_end = 1
kappa = np.pi/8 # circular dispersion
kappa_steps = np.pi/8
kappa_end = np.pi/2
observations = 50
while mu <= mu_end:
while kappa <= kappa_end:
s = np.random.vonmises(mu, kappa, observations)
count, bins, ignored = pl.hist(s, observations, normed=True)
x = np.arange(-np.pi, np.pi, 2*np.pi/observations)
y = -np.exp(kappa*np.cos(x-mu))/(2*np.pi*sps.jn(0,kappa))
pl.plot(x, y/max(y), linewidth=2, color='r')
pl.show(False)
pl.draw()
kappa = kappa + kappa_steps
mu = mu + mu_steps
所以我让函数运行,但它没有停止。我知道这将是一个简单的解决方案,但我不再看到森林的树了。
答案 0 :(得分:1)
>>> import numpy as np
>>> mu = 1/9 # circular mean phase
>>> mu_steps = 2/9
>>> mu_end = 1
>>> kappa = np.pi/8 # circular dispersion
>>> kappa_steps = np.pi/8
>>> kappa_end = np.pi/2
>>> mu, mu_steps, mu_end
(0, 0, 1)
>>> kappa, kappa_steps, kappa_end
(0.39269908169872414, 0.39269908169872414, 1.5707963267948966)
你能看到这里的问题吗?
mu_steps == 0
;变体mu
始终为<= mu_end
。