2

I'm currently trying to plot a bifurcation diagram from a 1D logistic map.

Here is my code:

def logistic_map(x0, r, n):
    """
    This function is returning values for a given logistic map
    after n iterations with an initial state x0 and a parameter r.
    """
    x = [x0]
    for iteration in range(n):
        x.append(r * x[-1] * (1-x[-1]))
    return np.array(x)

def bifurcation_plotter(x0, r_min, r_max, r_step, n, k):
    """
    This function is plotting a bifurcation diagram with an initial step x0,
    with a parameter ranging from r_min to r_max with a step r_step, for n
    iterations. The first k numbers are removed to remove the transient phase.
    """
    r_values = np.arange(r_min, r_max, r_step)
    bifurcations = {}

    for r in r_values:
        log_map = logistic_map(x0, r, n)[k:]
        bifurcations[r] = np.unique(log_map)

    plt.figure()
    plt.plot(bifurcations.keys(), bifurcations.values(), "b,", markersize=1)
    plt.xlabel("$r$")
    plt.ylabel("bifurcations")

When I try this code with parameters such as 0.1, 2.4, 4, 0.01, n, 5 and a value of n=25, the code is working and I'm getting the good plot as you can see here:

bifurcation_plotter(0.1, 2.4, 4, 0.01, 25, 5)

Plot

But as soon as I try to increase n, let's say to n=50, I'm getting a blank plot with "ValueError: setting an array element with a sequence".

bifurcation_plotter(0.1, 2.4, 4, 0.01, 50, 5)

I'm quite new to Python so I don't really understand why it's not working.

Can you help me please? Thank you!

0 Answers0