Exploring Nonlinear Dynamics

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:

  1. Iterate the map for a range of $(r)$ values (e.g., $2.5$ to $4.0$),
  2. Plot the long-term behavior (steady states or oscillations) as a bifurcation diagram,
  3. 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
import matplotlib.pyplot as plt

# Define the logistic map function
def logistic_map(r, x):
return r * x * (1 - x)

# Parameters
n_iterations = 1000 # Total iterations
n_transient = 800 # Discard initial transient iterations
n_points = 200 # Number of points to plot per r value
r_values = np.linspace(2.5, 4.0, 1000) # Range of r values
x0 = 0.1 # Initial condition (must be between 0 and 1)

# Arrays to store results
r_plot = []
x_plot = []

# Iterate over r values
for r in r_values:
x = x0
# Run iterations, discard transients
for i in range(n_iterations):
x = logistic_map(r, x)
# Store the last n_points after transient
if i >= n_transient:
r_plot.append(r)
x_plot.append(x)

# Create the bifurcation diagram
plt.figure(figsize=(10, 6))
plt.plot(r_plot, x_plot, ',k', alpha=0.25) # Plot as small black dots
plt.xlabel('$r$')
plt.ylabel('$x$')
plt.title('Bifurcation Diagram of the Logistic Map')
plt.grid(True)
plt.show()

Code Explanation

  1. Imports:

    • numpy for numerical computations (e.g., creating arrays of $(r)$ values).
    • matplotlib.pyplot for plotting the bifurcation diagram.
  2. Logistic Map Function:

    • logistic_map(r, x) implements the equation $(x_{n+1} = r x_n (1 - x_n))$.
  3. 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).
  4. 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.
  5. 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$).

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!