A Bifurcation Diagram shows that the stability of a system can be highly dependant on the inputs.

It is calculated by looping the folowing equation, for a number of iterations, and every r in a defined range. Then the results are plotted with each r on the x-axis, and x on the y-axis Logistic Map

Then the results are plotted with each r on the x-axis, and x on the y-axis, resulting in the following diagram.

Reulting Bifurcation Diagram

The code for it is as follows:

import numpy as np
import matplotlib.pyplot as plt
import numba

@numba.jit(nopython=True, parallel=True)
def calc():
  # Parameters
  min_r = 2.8
  max_r = 3.65
  step_r = 0.001
  max_iterations = 10000
  skip_iterations = 500
  max_counter = int((max_iterations - skip_iterations) * (max_r - min_r) / step_r)
  # The x and r results will be stored in these two arrays
  result_x = np.zeros(max_counter)
  result_r = np.zeros(max_counter)
  # Start the main loop
  i = 0
  for r in np.arange(min_r, max_r, step_r):
    x = 0.1
    for it in range(max_iterations):
      x = r * x * (1-x)
      if it > skip_iterations:
        result_x[i] = x
        result_r[i] = r
        i += 1
  result_x = result_x[result_r != 0].copy()
  result_r = result_r[result_r != 0].copy()
  return result_x, result_r

result_x, result_r = calc()

# Plot
plt.figure(figsize=(5, 3), dpi=200)
plt.plot(result_r, result_x, ",", color='k')
plt.show()

Notes:

  • Remember to pip install numpy matplotlib numba
  • numba is just used to make the calculations faster

Github Gist