The Logistic Map. A notebook from my complexity notebooks, written in Python with NumPy and Matplotlib.
#Import all the libraries used throughout the notebook
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.font_manager
%matplotlib inline
def logistic_map(x, l):
x = l*x*(1-x) #Logistic Map Equation to determine the next step
return x
def run_logistic_map(x, l, steps, name="map", f=logistic_map):
x_axes = []
y_axes = []
for i in range(steps):
x_axes.append(i)
y_axes.append(x)
x = f(x,l) #Logistic Map Equation to determine the next step
data_plot = pd.DataFrame({"Step":x_axes, "Population":y_axes})
sns.lineplot(x = "Step", y = "Population", data=data_plot)
plt.title("Lambda = " + str(l))
plt.show()
run_logistic_map(0.4, 2.5, 50,"map_2-5")
run_logistic_map(0.4, 3, 50,"map_3")
run_logistic_map(0.4, 3.5, 50,"map_3-5")
run_logistic_map(0.4, 3.7, 50, "map_3-7")




<Figure size 432x288 with 0 Axes>
def cobweb_logistic_map(x, l, steps, f=logistic_map):
data = []
x = x
y = 0
for i in range(steps): #Generate the cobwebs data
if i % 2 == 0:
data.append([x, y])
y = f(x,l)
else:
data.append([x, y])
x = f(x,l)
x_axes = [x[0] for x in data]
y_axes = [x[1] for x in data]
t = np.arange(start=0, stop=1, step=.01)
plt.plot(x_axes, y_axes, 'r') #plot the cobwebs
plt.plot(t,f(t,l)) #plot the fixed points
plt.show()
#Run through
cobweb_logistic_map(0.2, 2.5)
cobweb_logistic_map(0.2, 3)
cobweb_logistic_map(0.2, 3.5)
cobweb_logistic_map(0.2, 3.7)




def bifurcation_logistic_map(x, f=logistic_map):
iter_distance=0.0001
x_axes = []
y_axes = []
for i in np.arange(2.5,4.0,iter_distance):
l = i
x_axes.append(i)
m = []
for j in range(10000): #first 10,000 iterations to find the fixed points
x = f(x,l)
for k in range(100): #iterating through the fixed points/chaos
m.append(x)
x = f(x,l)
y_axes.append(m)
l += iter_distance
plt.figure(dpi=100)
plt.plot(x_axes, y_axes,'k.', alpha=0.9, ms=0.01, mew=0.01) #plot the points
plt.savefig("bifurcation_diagram", dpi=1000)
bifurcation_logistic_map(0.6)
