I was wondering what was wrong with my code as I keep getting a weird but similar result to the actual bifurcation diagram. I am using the iterative equation xn+1 = xn * r(1-xn)
.
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from functools import lru_cache
@lru_cache(maxsize = 1000)
def bifunc():
R_val = []
X_val = []
R = np.linspace(0.5,4,1000)
for r in R:
x = 0.5
for iterations in range(1001):
x = x*r*(1-x)
R_val.append(r)
X_val.append(x)
plt.plot(R_val, X_val, ls = '', marker = ',')
plt.show()
bifunc()
Here is the image that keeps coming up:
Any help would be appreciated. Thank you.
As the first few iterations don't follow the pattern, they can be left out via an if
-test. Best also remove the lru_cache
as it has no real function here and can interfere with testing.
import numpy as np
import matplotlib.pyplot as plt
def bifunc():
R_val = []
X_val = []
R = np.linspace(0.5, 4, 1000)
for r in R:
x = 0.5
for iterations in range(1001):
x = x*r*(1-x)
if iterations > 100:
R_val.append(r)
X_val.append(x)
plt.plot(R_val, X_val, ls='', marker=',')
plt.show()
bifunc()
This results in a plot similar to many found on the web. Usually the plots don't start before R=1
to avoid displaying the flat part between 0.5 and 1.