Killian
Killian

Reputation: 83

bifurcation diagram with python

I'm a beginner and I don't speak english very well so sorry about that. I'd like to draw the bifurcation diagram of the sequence : x(n+1)=ux(n)(1-x(n)) with x(0)=0.7 and u between 0.7 and 4.

I am supposed to get something like this :

https://i.sstatic.net/T4gyF.png

So, for each value of u, I'd like to calculate the accumulation points of this sequence. That's why I'd like to code something that could display every points (u;x1001),(u;x1002)...(u;x1050) for each value of u.

I did this :

import matplotlib.pyplot as plt
import numpy as np
P=np.linspace(0.7,4,10000)
m=0.7
Y=[m]
l=np.linspace(1000,1050,51)
for u in P:
    X=[u]
    for n in range(1001):
      m=(u*m)*(1-m)
    break 
    for l in range(1051):
      m=(u*m)*(1-m)
      Y.append(m)
plt.plot(X,Y)
plt.show()

And, I get a blank graphic.

This is the first thing I try to code and I don't know anything yet in Python so I need help please.

Upvotes: 8

Views: 21844

Answers (2)

Soroush
Soroush

Reputation: 465

To save bifurcation diagram in png format, you can try this simple code.

# Bifurcation diagram of the logistic map

from PIL import Image
imgx = 1000
imgy = 500
image = Image.new("RGB", (imgx, imgy))

xa = 2.9
xb = 4.0
maxit = 1000

for i in range(imgx):
    r = xa + (xb - xa) * float(i) / (imgx - 1)
    x = 0.5
    for j in range(maxit):
        x = r * x * (1 - x)
        if j > maxit / 2:
            image.putpixel((i, int(x * imgy)), (255, 255, 255))

image.save("Bifurcation.png", "PNG")

Upvotes: 1

Pierre de Buyl
Pierre de Buyl

Reputation: 7293

There are a few issues in your code. Although the problem you have is a code review problem, generating bifurcation diagrams is a problem of general interest (it might need a relocation on scicomp but I don't know how to request that formally).

import matplotlib.pyplot as plt
import numpy as np
P=np.linspace(0.7,4,10000)
m=0.7
# Initialize your data containers identically
X = []
Y = []
# l is never used, I removed it.
for u in P:
    # Add one value to X instead of resetting it.
    X.append(u)
    # Start with a random value of m instead of remaining stuck
    # on a particular branch of the diagram
    m = np.random.random()
    for n in range(1001):
      m=(u*m)*(1-m)
    # The break is harmful here as it prevents completion of
    # the loop and collection of data in Y 
    for l in range(1051):
      m=(u*m)*(1-m)
    # Collection of data in Y must be done once per value of u
    Y.append(m)
# Remove the line between successive data points, this renders
# the plot illegible. Use a small marker instead.
plt.plot(X, Y, ls='', marker=',')
plt.show()

Also, X is useless here as it contains a copy of P.

Upvotes: 8

Related Questions