The Logistic Map Bifurcation Diagram in Python
Let’s dive into an example problem in nonlinear dynamics and solve it using Python in Google Colaboratory.
We’ll consider the logistic map, a classic example of a nonlinear dynamical system that exhibits rich behavior, including chaos, depending on its parameter values.
The logistic map is defined by the recursive equation:
$$
x_{n+1} = r x_n (1 - x_n)
$$
where:
- $(x_n)$ is the state variable at iteration $(n)$, constrained between $0$ and $1$ (e.g., representing a normalized population),
- $(r)$ is a growth rate parameter, typically between $0$ and $4$,
- The nonlinearity arises from the quadratic term $(x_n (1 - x_n))$.
Problem Statement
We’ll simulate the logistic map for different values of $(r)$ and visualize how the system transitions from stable fixed points to periodic orbits and eventually to chaotic behavior. Specifically, we’ll:
- Iterate the map for a range of $(r)$ values (e.g., $2.5$ to $4.0$),
- Plot the long-term behavior (steady states or oscillations) as a bifurcation diagram,
- Use Python to compute and graph the results.
Python Code
Here’s the complete code to simulate and visualize the logistic map’s bifurcation diagram:
1 | import numpy as np |
Code Explanation
Imports:
numpy
for numerical computations (e.g., creating arrays of $(r)$ values).matplotlib.pyplot
for plotting the bifurcation diagram.
Logistic Map Function:
logistic_map(r, x)
implements the equation $(x_{n+1} = r x_n (1 - x_n))$.
Parameters:
n_iterations
: Total number of iterations per $(r)$ value.n_transient
: Number of initial iterations to discard (to focus on steady-state behavior).n_points
: Number of points to plot after transients.r_values
: Array of $(r)$ from $2.5$ to $4.0$ with $1000$ steps.x0
: Initial condition set to $0.1$ (any value between $0$ and $1$ works).
Simulation:
- For each $(r)$, start with $(x = x0)$.
- Iterate the map
n_iterations
times. - After
n_transient
iterations, store the subsequent $(x)$ values and corresponding $(r)$ values.
Plotting:
- Use
plt.plot
with a comma marker (,
) to plot points as tiny dots, creating a dense bifurcation diagram. - Add labels and a title with TEX-formatted variables (e.g.,
$r$
).
- Use
Results and Graph Interpretation
When you run this code in Google Colaboratory, it generates a bifurcation diagram. Here’s what you’ll see:
- For $(r < 3)$: The system converges to a single fixed point (e.g., at $(r = 2.5)$, $(x)$ stabilizes around $0.6$). This is a stable equilibrium.
- For $(3 < r < 3.57)$: Period-doubling occurs. At $(r = 3.2)$, you’ll see two values (a period-2 cycle), then four (period-4), and so on.
- For $(r > 3.57)$: The system enters chaotic regimes, interspersed with periodic windows (e.g., a clear period-3 window around $(r = 3.83)$).
- At $(r = 4)$: Fully chaotic behavior, with $(x)$ values filling the interval [0, 1] densely.
The graph shows $(x)$ versus $(r)$.
Initially, you see a single line (fixed point), which splits into two, four, etc., before becoming a scattered cloud of points in the chaotic region.
This beautifully illustrates how a simple nonlinear equation can produce complex dynamics.
Conclusion
This example demonstrates key features of nonlinear dynamics: stability, bifurcations, and chaos.
The Python code is efficient and leverages NumPy for fast computation and Matplotlib for clear visualization.
You can tweak parameters like r_values
or n_iterations
to explore further!