Inspired by one of Veritasium Youtube video where he explained chaotic bifurcation map (logistic map). the mathematical equation is simply: X[i+1]=R*X[i](1-X[i])
the first graph he plots: this X[i] values (y-axis values, in the range of 0 to 1) versus iteration time i (x-axis values, number of calculation) with a certain value of R (say, R_init=2, and I wanted to incorporate a Slider in my code for changing the value of R)
the second graph (the bifurcation graph) he plots: the value R (x-axis values) versus equilibrium population of X[i], that is: the number of X[i] values that it oscillates between (depending on the R value, after certain/many iteration i, X[i] can oscillate between finite numbers and also infinite numbers -- chaos!)
Eventually, in the code, I want two side by side subplot, left one is the "first graph" with the Slider for those two variables, right side is the "second graph". Below I pasted the my code for a starter, where I only try to achieve plotting "first graph" with a Slider for R and initial value of X[0] (but, of course, it failed to show anything so far..) I'd appreciate if anyone can help me finishing up this project, or giving me some advice on my problematic code.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
R = 1.5
x = np.linspace(0, 100, 1)
k_init = 0.4 # initial y value
fig = plt.figure(figsize=(8, 8))
ax = plt.axes([0.125, 0.15, 0.775, 0.80])
myplot, = plt.plot(0, 0, c="royalblue") # not really ploting anything first
plt.xlim(0, 100)
plt.ylim(0, 1)
# create slider panel & values sets
slider_r = plt.axes([0.125, 0.03, 0.775, 0.03])
slider_k = plt.axes([0.125, 0.07, 0.775, 0.03])
r_slider = Slider(slider_r, "R", 1, 20, valinit=R, valstep=0.1)
k_slider = Slider(slider_k, "k_init", 0, 1, valinit=k_init, valstep=0.05)
k = [] # y-values starts with List items
def update(*args):
k.append(k_init)
for i in np.arange(1,100,1):
k_new = R*(k[i-1])*(1-(k[i-1]))
k.append(k_new)
y = np.array(k)
myplot.set_xdata(x)
myplot.set_ydata(y)
r_slider.on_changed(update)
k_slider.on_changed(update)
update()
plt.show()
