import numpy as np
from matplotlib import pyplot as plt
# Define the logistic map
epsilon = 1e-4
def lmap(x, r):
# "Beefing up" the fixed point:
# if x is in the epsilon neighbourhood of f(x), let it be a fixed point (return x)
if abs(r * x * (1 - x) - x) < epsilon:
return x
else:
return r * x * (1 - x)
# Iterate over the logistic map to find fixed points
def get_fixed_pts(f, x0, r, iterations = 100, keep = 10, decimals = 5):
x = x0
for i in range(iterations):
x = f(x, r)
if i >= (iterations - keep):
yield np.round(x, decimals)
# Setting up some params
x0 = 0.5
n_pts = 1000
r_lims = [2, 4]
fixed_points = []
corresponding_r_values = []
# Looping over r values to find the fixed point for every r value
for r in np.linspace(*r_lims, n_pts):
# Get the fixed points and remove duplicates.
# Then, find the cycle size (number of fixed points)
pts = set(get_fixed_pts(lmap, x0, r))
cycle_size = len(pts)
# Store values
fixed_points.extend(pts)
corresponding_r_values.extend([r] * cycle_size)
# Plot all found points
plt.figure(figsize=(16, 9))
plt.scatter(corresponding_r_values, fixed_points, s = 0.01, c = 'black', label = 'Stable fixed point')
plt.title('Bifurcation diagram of logistic map')
plt.xlabel('Parameter Value')
plt.ylabel('Fixed Points')
plt.xlim(r_lims)
plt.legend()
plt.show()
要用不稳定的固定点生成逻辑图的分叉图,您可以通过分析计算固定点,因为不稳定点驱动轨迹。
使用f(x)= r * x *(1 -x)查找固定点x = 0和x =(r -1)/r,并将它们分类为稳定| f'(x)|
1使用衍生f'(x)= r *(1-2x)
import numpy as np
import matplotlib.pyplot as plt
def logistic_map(x, r):
return r * x * (1 - x)
def logistic_map_derivative(x, r):
return r * (1 - 2 * x)
def compute_fixed_points(r):
if r < 1:
return [0], []
stable = []
unstable = []
if abs(logistic_map_derivative(0, r)) < 1:
stable.append(0)
else:
unstable.append(0)
if r > 1:
x_fixed = (r - 1) / r
if abs(logistic_map_derivative(x_fixed, r)) < 1:
stable.append(x_fixed)
else:
unstable.append(x_fixed)
return stable, unstable
# Iterate through the logistic map to compute periodic points
def iterate_logistic_map(x0, r, iterations=500, discard=100):
x = x0
trajectory = []
for i in range(iterations):
x = logistic_map(x, r)
if i >= discard:
trajectory.append(x)
return trajectory
r_values = np.linspace(2, 4, 1000)
x0 = 0.5
num_iterations = 500
discard = 100
stable_points = []
unstable_points = []
r_stable = []
r_unstable = []
for r in r_values:
# Compute analytically derived fixed points
stable, unstable = compute_fixed_points(r)
stable_points.extend(stable)
unstable_points.extend(unstable)
r_stable.extend([r] * len(stable))
r_unstable.extend([r] * len(unstable))
# Iterate to find attracting fixed points
trajectory = iterate_logistic_map(x0, r, iterations=num_iterations, discard=discard)
r_stable.extend([r] * len(trajectory))
stable_points.extend(trajectory)
plt.figure(figsize=(16, 9))
plt.scatter(r_stable, stable_points, s=0.5, c='black', label='Stable Points')
plt.scatter(r_unstable, unstable_points, s=1, c='red', label='Unstable Points')
plt.title('Bifurcation Diagram of Logistic Map (Stable and Unstable Points)')
plt.xlabel('Parameter r')
plt.ylabel('x')
plt.legend(loc='upper left')
plt.show()