Nested while loop distribution calculation

68 Views Asked by At

I got a question about nested while loops. I want to run a calculation of a distribution for multiple values. Here mu and 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

So I got the function running, but it doesn't stop. I know it would be an easy solution, but I don't see the tree for the forest anymore.

1

There are 1 best solutions below

1
On
>>> 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)

Can you see what the problem is here?

mu_steps == 0; The variant mu will always be <= mu_end.