#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
x = 0.2 #starting condition of x
steps = 50 #number of steps
l = 0 # starting lambda parameter
def logistic_map(x=x, l=l):
x = l*x*(1-x) #Logistic Map Equation to determine the next step
return x
def run_logistic_map(x=x, l=l, steps=steps, 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, "Value":y_axes})
sns.lineplot(x = "Step", y = "Value", data=data_plot)
plt.show()
run_logistic_map(0.2, 2.5)
run_logistic_map(0.2, 3)
run_logistic_map(0.2, 3.5)
run_logistic_map(0.2, 3.7)
def cobweb_logistic_map(x=x, l=l, steps=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=x, f=logistic_map):
iter_distance=0.001
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.2, markersize=0.3) #plot the cobwebs
bifurcation_logistic_map()