The Butterfly Effect: A Spartan Introduction to Chaos Theory

1. Introduction to Chaos Theory

— Nina Hall, The New Scientist Guide to Chaos

1.1 Historical Background

The roots of chaos theory can be traced back to the early 20th century with the work of mathematicians such as Henri Poincaré, who discovered deterministic chaos while studying celestial mechanics. However, it wasn’t until the 1960s and 1970s that chaos theory began to gain widespread recognition with the pioneering work of mathematicians and meteorologists like Edward Lorenz and James Yorke.

1.2 Key Concepts

At the heart of chaos theory lies deterministic chaos, which refers to deterministic systems that exhibit highly unpredictable behaviour over time. This unpredictability arises from amplifying small differences in initial conditions, commonly known as the “Butterfly Effect“.

Is chaos just randomness in disguise?

Chaos theory is a fascinating field of study exploring the behaviour of dynamic systems highly sensitive to initial conditions.

Despite its name, chaos theory does not refer to disorder or randomness but rather to the underlying order and complexity present in seemingly chaotic systems.

It seeks to understand the underlying patterns and structures that emerge from seemingly random behaviour.

Deterministic chaos occurs in a system that exhibits chaotic behaviour despite being governed by deterministic laws or equations. In other words, the system’s behaviour is not random but can still appear unpredictable over time due to its sensitivity to initial conditions and complex dynamics.

Key characteristics of deterministic chaos include:

  • Deterministic Laws:
  • The system’s behaviour is governed by deterministic equations or rules, meaning its initial conditions can uniquely determine its future state. The system’s dynamics and evolution do not depend on external factors or stochastic processes and are governed entirely by Newtonian dynamics.
  • Sensitive Dependence on Initial Conditions:
  • Small differences in the system’s initial conditions can lead to significantly different outcomes. This sensitivity to initial conditions, known as the “butterfly effect,” is a hallmark of chaotic systems.
  • The system’s indeterminacy and unpredictability in the long-term behaviour arise from our inability to measure its initial states with arbitrary precision.
  • Nonlinear Dynamics:
  • Chaotic systems typically involve nonlinear interactions between variables, leading to complex and often unpredictable behaviour. Nonlinearities can give rise to feedback loops, bifurcations, and other emergent phenomena contributing to chaotic dynamics.
  • Strange Attractors:
  • Chaotic systems often exhibit strange attractors in phase space, representing their long-term behaviour. Strange attractors are complex, non-repeating patterns arising from deterministic dynamics and sensitive dependence on initial conditions.

We will review some examples of these properties of chaotic systems in the next paragraphs.

1.3 Deterministic vs. Random Systems

— David Tritton, The New Scientist Guide to Chaos

In contrast to random systems, where outcomes are truly unpredictable, deterministic chaos occurs in systems governed by deterministic laws but are highly sensitive to initial conditions. This sensitivity leads to divergent trajectories, making long-term predictions practically impossible despite the underlying determinism.

Modern computers can perform calculations with a fantastic degree of precision. If your calculations are precise enough, you can make good short-term predictions. If you increase the precision, you can extend that prediction period into the future. However, this period cannot be indefinitely extended as long as we work with finite precision. This will be demonstrated later in this article when we explore logistic maps.

1.4 Scope of Chaos Theory

Chaos theory extends beyond mathematics and physics to various disciplines such as biology, economics, and social sciences. It provides a powerful framework for understanding complex phenomena ranging from the behaviour of ecosystems to the dynamics of financial markets.

1.5 The objective of This Article

In this article, we will delve into the core principles of chaos theory and explore its implications through practical examples implemented in Python. By the end, readers will gain a deeper understanding of chaos theory and its relevance in modelling and understanding complex systems.

In the following sections, we will explore key concepts such as the Butterfly Effect, fractals, strange attractors, and their applications in diverse fields, all accompanied by Python code examples for hands-on exploration.

2. The Butterfly Effect

— Ian Percival, The New Scientist Guide to Chaos

The Butterfly Effect is one of chaos theory’s most iconic and captivating ideas. Coined by meteorologist Edward Lorenz, it illustrates how small changes in initial conditions can lead to drastically different outcomes in dynamic systems over time. The term originated from Lorenz’s observations while studying weather patterns using mathematical models.

2.1 Origin of the Term

The Butterfly Effect metaphorically suggests that the flap of a butterfly’s wings in Brazil could set off a tornado in Texas. This poetic imagery vividly encapsulates the idea that even the tiniest perturbation in one part of a system can amplify and propagate, ultimately leading to significant consequences elsewhere.

2.2 Sensitivity to Initial Conditions

The Butterfly Effect concept captures the sensitivity principle to initial conditions. In deterministic chaotic systems, even minuscule differences in the starting state can result in divergent trajectories over time. This sensitivity amplifies exponentially as the system evolves, leading to unpredictable outcomes in the long term.

2.3 Lorenz’s Experiment

Edward Lorenz famously demonstrated the Butterfly Effect through a simplified mathematical model of atmospheric convection known as the Lorenz attractor. In his experiment, he observed how rounding off initial values to just a few decimal places drastically altered the system’s long-term behaviour. This discovery revolutionized our understanding of weather forecasting and the limits of predictability.

The Lorenz system is defined by the following set of three coupled differential equations:

\frac{dx}{dt} = \sigma \cdot (y - x)

\frac{dy}{dt} = x \cdot (\rho - z) - y

\frac{dz}{dt} = x \cdot y - \beta \cdot z

where (x), (y), and (z) represent the state variables of the system, and (sigma), (rho), and (beta) are parameters controlling the system’s behaviour.

The Lorenz system exhibits chaotic behaviour for certain parameter values, particularly when (sigma), (rho), and (beta) fall within specific ranges. Chaotic trajectories in the Lorenz system are characterized by sensitivity to initial conditions, known as the Butterfly Effect, where small differences in initial states lead to divergent trajectories over time.

Lorenz Attractors
Lorenz Attractors

The figure above demonstrates the Lorenz attractors and tells us the following:

  • While it is impossible to make long-term predictions on which orbit the particle will take, let alone its radius or exact location, the range of likely routes can be confined to the two planes around the specific attractors with high confidence.
  • Attractors are complex, non-repeating phase space patterns representing the system’s long-term behaviour. Despite their irregular appearance, strange attractors possess certain geometric properties and are often characterized by fractal dimensionality.
  • The second diagram shows two simulations with slightly different initial conditions (1 part in 100) and two trajectories that do not coincide.

2.4 Strange Attractors

One of the defining features of chaotic systems like the Lorenz system is the presence of strange attractors. Strange attractors are non-periodic, geometric structures in phase space that attract nearby trajectories while exhibiting complex, self-similar patterns. The Lorenz attractor, discovered by Edward Lorenz, is perhaps the most famous example of a strange attractor, characterized by its butterfly-shaped trajectory.

2.5 Implications and Significance

The Butterfly Effect highlights the inherent unpredictability of certain complex systems, challenging the notion of determinism in the classical sense. It underscores the limitations of traditional linear models for predicting long-term behaviour in nonlinear systems, such as weather patterns, ecological dynamics, and even human behaviour.

3. Fractals and Self-Similarity

Fractals are fascinating mathematical objects that exhibit self-similarity across different scales. They possess intricate geometric patterns and structures that repeat infinitely, making them a hallmark of chaos theory. In this section, we will explore the concept of fractals and their relevance in understanding complex systems.

3.1 Definition of Fractals

A fractal is a geometric shape or pattern that displays self-similarity at various scales. This means that as you zoom into smaller fractal regions, you observe similar patterns recurring, regardless of the level of magnification. Fractals often have irregular shapes with fine details repeated at progressively smaller scales.

3.2 Self-Similarity

Self-similarity is a key characteristic of fractals, where smaller parts of the fractal resemble the whole structure. This recursive property means that each fractal component contains smaller copies of itself, leading to an infinite nesting of patterns within patterns.

3.3 Examples of Fractals

One of the most famous fractals is the Mandelbrot set, discovered by mathematician Benoit Mandelbrot in the 1970s. The Mandelbrot set is generated by iterating a simple mathematical equation and determining whether the sequence remains bounded or diverges. The resulting set forms a highly intricate and visually appealing fractal pattern with infinite complexity.

3.4 Applications of Fractals

Fractals find applications in diverse fields such as computer graphics, art, terrain generation, and even the study of natural phenomena. They provide a powerful framework for modelling complex structures and processes that exhibit self-similar patterns, from coastlines and mountain ranges to the branching patterns of trees and blood vessels.

3.5 Python Example: Mandelbrot Set Visualization

We can use Python to generate and visualize the Mandelbrot set (see Appendix).

Mandelbrot set
Mandelbrot set

This code generates a grid of complex numbers representing points in the complex plane and computes the escape time for each point using the Mandelbrot iteration formula. The resulting image reveals the intricate structure of the Mandelbrot set, showcasing its self-similar patterns and complexity.

4. Logistic Map and Population Dynamics

The logistic map is a classic example of a dynamic system exhibiting chaotic behaviour. Initially proposed as a simple model for population growth, it has since become a cornerstone of chaos theory, offering insights into the emergence of complex dynamics from seemingly simple equations. This section will explore the logistic map and its role in modelling population dynamics.

4.1 Introduction to the Logistic Map

The logistic map is a mathematical model that describes the population dynamics of a species with limited resources. The recursive equation defines it:

x_{n+1} = r \cdot x_n \cdot (1 - x_n)

where (x_n) represents the population proportion at time step (n) (normalized to the range ([0, 1])), and (r) is a parameter controlling the growth rate of the population.

The Logistic Population Growth simulation ran twice with growth parameters 1, 2, 3, 3.2, 3.5, and 3.7. During the second, the inputs randomly varied by 5%. We observe no difference in the first five plots (growth rate =1, 2, 3, 3.2, and 3.5); the sensitivity to initial conditions is minimal. By contrast, in the final plot (growth rate = 3.7), the system runs in chaotic mode, displaying high sensitivity to initial conditions.

The diagram above shows us the progression of the logistic function of (x_n) with different growth rate values (1, 2, 3, and 4). We observe the following:

  • The system’s behaviour is qualitatively different in at least three cases.
  • In the first and second cases, the system moves towards a stable equilibrium.
  • In cases 3 to 5, the system oscillates between at least two points in the phase space.
  • In case 6, the system displays chaotic behaviour.
  • Sensitivity to initial conditions.
  • In all six graphs, we see two series of points: one in blue and the other in orange. In both simulations, the input was randomly varied by 5%.
  • We observe that in the five cases where the system moved towards one or more equilibrium points, the variations in input did not significantly modify the original trajectory. The blue and orange series converged more or less similarly towards the equilibrium points.
  • In the final series, where the system shows chaotic behaviour, blue and orange progression varied significantly.

4.2 Bifurcation Diagrams and Chaos

In logistic population models, a period refers to the length of time or the number of iterations for the population dynamics to repeat themselves. Specifically, it relates to the oscillatory behaviour exhibited by the population over time. In the context of the logistic map, periods are associated with the repetitive patterns observed in the bifurcation diagram.

Bifurcation points in logistic population models occur when the system’s behaviour undergoes a qualitative change as a control parameter (typically, the growth rate parameter ( r )) is varied. At each bifurcation point, the stability of the system’s equilibrium points changes, leading to new behaviour patterns.

Bifurcation diagram of the logistic map.

The bifurcation diagram is a graphical representation that maps the values of the control parameter against the system’s long-term behaviour.

The relationship between periods and bifurcation points can be understood as follows:

  • Period Doubling Bifurcations:
  • The system may undergo period-doubling bifurcations as the control parameter ( r ) increases beyond a certain threshold. This means that the period of the oscillations doubles with each successive bifurcation.
  • For example, the system may transition from stable equilibrium to periodic oscillations with period 1 (no oscillation), then to period 2, then to period 4, and so on. These bifurcation points are often associated with the formation of stable periodic orbits.
  • Periodic Windows:
  • In some regions of the bifurcation diagram, periodic windows may appear, where periodic behaviour coexists with chaotic behaviour.
  • Within these windows, the system exhibits periodic oscillations with specific periods, surrounded by regions of chaotic dynamics.
  • These periodic windows correspond to specific control parameter values where stable periodic orbits characterize the system’s behaviour.
  • Chaotic Regions:
  • Beyond certain bifurcation points, the system enters chaotic regions where the behaviour appears random and unpredictable.
  • The system exhibits sensitive dependence on initial conditions and complex, aperiodic dynamics in these regions. Chaotic behaviour typically arises from the accumulation of bifurcations as the control parameter increases.

5. Chaos in Weather Forecasting

Chaos theory has profound implications for weather forecasting, highlighting the inherent limitations of long-term prediction due to the sensitive dependence on initial conditions. Despite advances in technology and computational modelling, accurate weather forecasting beyond a certain timeframe remains a significant challenge. In this section, we will explore the application of chaos theory in weather forecasting and the complexities involved in predicting atmospheric behaviour.

5.1 Challenges of Weather Prediction

Weather is a complex system characterized by nonlinear interactions between atmospheric variables such as temperature, pressure, humidity, and wind. Small changes in initial conditions, such as the position of air masses or the presence of clouds, can lead to vastly different weather outcomes over time. This sensitivity to initial conditions limits the predictability of weather beyond a few days or weeks.

5.2 Role of Chaos Theory

Chaos theory provides a theoretical framework for understanding the chaotic nature of atmospheric dynamics and the emergence of unpredictable weather patterns. It emphasizes the importance of probabilistic forecasting approaches that account for uncertainties and inherent variability in the atmosphere. While deterministic models can provide valuable insights into short-term weather trends, they often fail to capture the full spectrum of chaotic behaviour exhibited by the atmosphere.

5.3 Probabilistic Forecasting

In response to the challenges of chaos, meteorologists have increasingly embraced probabilistic forecasting techniques that provide a range of possible outcomes rather than precise predictions. Ensemble forecasting, for example, involves running multiple simulations with slightly perturbed initial conditions to account for uncertainty and variability. By analyzing the spread of ensemble members, forecasters can assess the likelihood of different weather scenarios and provide more informed forecasts.

5.4 Improving Forecasting Accuracy

Advancements in observational technology, data assimilation techniques, and numerical weather prediction models have improved forecasting accuracy, especially for short-term and regional forecasts. However, long-term predictions beyond a few weeks remain inherently uncertain due to the chaotic nature of the atmosphere. Research efforts continue to focus on refining models, assimilating observational data, and understanding the underlying dynamics of weather systems to enhance predictive capabilities.

5.5 Conclusion

Weather forecasting is a complex endeavour that grapples with the inherent unpredictability of atmospheric dynamics. While chaos theory underscores the challenges of long-term prediction, it also informs the development of innovative forecasting techniques that embrace uncertainty and probabilistic approaches. By acknowledging the limits of predictability and leveraging the principles of chaos theory, meteorologists strive to provide more reliable and actionable forecasts for society.

6. Chaos in Financial Markets

Financial markets are another domain where chaos theory has significant implications, particularly in understanding the dynamics of asset prices and market behaviour. While traditional finance models assume efficient markets and rational behaviour, chaos theory reveals the presence of nonlinear dynamics, herd behaviour, and complex interactions that contribute to market fluctuations and volatility.

In this section, we will explore the application of chaos theory in financial markets and the emergence of chaotic behaviour in asset prices.

6.1 Nonlinear Dynamics in Market Prices

Chaos theory challenges the traditional view of financial markets as linear and predictable systems. Instead, it recognizes the presence of nonlinear dynamics, feedback loops, and self-reinforcing patterns that contribute to the emergence of chaotic behaviour in market prices. Small changes in market sentiment, investor behaviour, or external factors can lead to disproportionate effects on asset prices, triggering cascading effects and market volatility.

6.2 Herd Behavior and Feedback Loops

Herd behaviour, where investors tend to follow the actions of others rather than independently assess information, is a common phenomenon in financial markets. This collective behaviour can amplify market movements, leading to the formation of feedback loops and self-fulfilling prophecies.

Chaos theory highlights how these feedback loops can drive market dynamics and contribute to the emergence of chaotic behaviour, such as price bubbles and crashes.

6.3 Fractal Patterns in Market Data

Market data often exhibit fractal patterns characterized by self-similarity and scale invariance across different time frames. Fractal analysis reveals repeating patterns and structures in market prices, from intraday fluctuations to long-term trends. These fractal patterns provide insights into the underlying dynamics of financial markets and the complex interactions between market participants.

6.4 Limitations of Predictive Models

Chaos theory underscores the limitations of traditional predictive models in financial markets, which often fail to account for nonlinear dynamics and emergent behaviour. While statistical models and technical analysis techniques can provide valuable insights into short-term price movements, they may overlook the underlying complexity and uncertainty of market dynamics. As a result, accurate long-term predictions of market behaviour remain elusive.

6.5 Risk Management and Adaptation

Recognizing the presence of chaos in financial markets, investors and financial institutions focus on risk management strategies and adaptive approaches to navigate uncertainty. Portfolio diversification, hedging techniques, and stress testing are common strategies to mitigate the impact of market volatility and unexpected events. By embracing uncertainty and adopting flexible investment strategies, market participants aim to navigate the chaotic nature of financial markets more effectively.

7. The Role of Computers in Advancing Chaos Theory

The evolution of chaos theory has been profoundly shaped by the advent of computers, which have played a pivotal role in developing and applying this interdisciplinary field. From the initial discoveries of deterministic chaos to the exploration of complex dynamical systems, computers have provided scientists with powerful tools to simulate, analyze, and visualize the intricate behaviours exhibited by nonlinear systems.

One of the defining moments in the history of chaos theory was the pioneering work of Edward Lorenz in the early 1960s. Through numerical simulations of weather patterns using early computer models, Lorenz uncovered the phenomenon of sensitive dependence on initial conditions, giving birth to the concept of the “butterfly effect” and laying the foundation for chaos theory. Without the computational resources afforded by computers, Lorenz’s groundbreaking discoveries would have remained elusive.

Since then, computers have continued to drive progress in chaos theory, enabling researchers to explore the dynamics of complex systems across diverse domains. Numerical simulations, mathematical modelling, and computational experiments have become indispensable tools for studying chaotic systems, from population dynamics and weather forecasting to financial markets and fluid dynamics.

Advances in computational algorithms and techniques have also facilitated the development of chaos theory. From discovering fractals and strange attractors to analysing bifurcation diagrams and Lyapunov exponents, computers have provided researchers with the means to explore the intricate structures and patterns embedded within chaotic systems. Through sophisticated computational methods, scientists have uncovered hidden order in seemingly random behaviour and gained deeper insights into the underlying dynamics of complex systems.

Moreover, computers have democratized access to chaos theory, allowing students, researchers, and enthusiasts worldwide to engage with and contribute to this fascinating field. Open-source software, online resources, and computational tools have made it easier than ever to explore chaotic systems, fostering collaboration and innovation within the scientific community.

Looking ahead, the continued advancement of chaos theory will depend heavily on further developments in computational techniques and technologies. As computing power increases and algorithms become more sophisticated, scientists can tackle more complex questions and uncover new insights into the nature of chaos and complexity.

8. References

9. Appendix

9.1 Lorenz Attractors

The following Python code demonstrates the Lorenz Attractors. This code simulates the Lorenz system using Euler’s method for numerical integration and plots the resulting trajectory in 3D space. The trajectory traces the iconic butterfly-shaped attractor characteristic of chaotic dynamics in the Lorenz system.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def lorenz(x, y, z, sigma=10, rho=28, beta=8/3):
    """
    Computes the time derivatives of the Lorenz system.
    
    Parameters:
        x, y, z : float
            Current state of the system.
        sigma, rho, beta : float, optional
            Parameters of the Lorenz system.
    
    Returns:
        dxdt, dydt, dzdt : float
            Time derivatives of the system state.
    """
    dxdt = sigma * (y - x)
    dydt = x * (rho - z) - y
    dzdt = x * y - beta * z
    return dxdt, dydt, dzdt

# Parameters
sigma = 10
rho = 28
beta = 8/3
dt = 0.01
num_steps = 10000

# Initial conditions
x = 0.1
y = 0.1
z = 0.1

# Arrays to store trajectory
x_values = [x]
y_values = [y]
z_values = [z]

# Time integration using Euler's method
for _ in range(num_steps):
    dxdt, dydt, dzdt = lorenz(x, y, z, sigma, rho, beta)
    x += dxdt * dt
    y += dydt * dt
    z += dzdt * dt
    x_values.append(x)
    y_values.append(y)
    z_values.append(z)

# Plotting the trajectory
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x_values, y_values, z_values, lw=0.5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Lorenz Attractor')
plt.show()

Modified to run twice with slightly differing initial conditions:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.integrate import solve_ivp

def lorenz(t, xyz, sigma, rho, beta):
    x, y, z = xyz
    dxdt = sigma * (y - x)
    dydt = x * (rho - z) - y
    dzdt = x * y - beta * z
    return [dxdt, dydt, dzdt]

# Parameters
sigma = 10
rho = 28
beta = 8 / 3

# Initial conditions for the first simulation
xyz0_1 = [1, 1, 1]

# Initial conditions for the second simulation (slightly different from the first one)
xyz0_2 = [1.01, 1.01, 1.01]

# Time span
t_span = (0, 100)
t_eval = np.linspace(*t_span, 10000)

# Solve the system for the first simulation
sol_1 = solve_ivp(lorenz, t_span, xyz0_1, args=(sigma, rho, beta), t_eval=t_eval)

# Solve the system for the second simulation
sol_2 = solve_ivp(lorenz, t_span, xyz0_2, args=(sigma, rho, beta), t_eval=t_eval)

# Extracting the solutions for both simulations
x1, y1, z1 = sol_1.y
x2, y2, z2 = sol_2.y

# Plotting
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
ax.plot(x1, y1, z1, color='blue', linewidth=0.5, label='Simulation 1')
ax.plot(x2, y2, z2, color='red', linewidth=0.5, label='Simulation 2')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('Lorenz Attractor')
ax.legend()
plt.show()

9.2 The Mandelbrot Set

Below is a simplified code example using the matplotlib library to plot the Mandelbrot set:

import numpy as np
import matplotlib.pyplot as plt

def mandelbrot(c, max_iter=100):
    """
    Computes the escape time for the Mandelbrot set at point c.

    Parameters:
        c : complex
            Complex number representing the point in the complex plane.
        max_iter : int, optional
            Maximum number of iterations.

    Returns:
        iter_count : int
            Number of iterations until escape, or max_iter if the point is within the set.
    """
    z = 0
    for i in range(max_iter):
        z = z**2 + c
        if abs(z) > 2:
            return i
    return max_iter

# Parameters
resolution = 1000
x_min, x_max = -2, 2
y_min, y_max = -2, 2

# Generate grid of complex numbers
x = np.linspace(x_min, x_max, resolution)
y = np.linspace(y_min, y_max, resolution)
X, Y = np.meshgrid(x, y)
C = X + 1j * Y

# Compute Mandelbrot set
M = np.zeros_like(C, dtype=int)
for i in range(resolution):
    for j in range(resolution):
        M[i, j] = mandelbrot(C[i, j])

# Plot Mandelbrot set
plt.figure(figsize=(8, 8))
plt.imshow(M, extent=(x_min, x_max, y_min, y_max), cmap='hot', interpolation='nearest', origin='lower')
plt.colorbar(label='Iterations to Escape')
plt.title('Mandelbrot Set')
plt.xlabel('Real Axis')
plt.ylabel('Imaginary Axis')
plt.show()

9.3 Bifurcation Points in Logistic Population Dynamics

We can use Python to simulate the logistic map and visualize its bifurcation diagram. Below is a Python code example that demonstrates how to generate a bifurcation diagram for the logistic map:

import numpy as np
import matplotlib.pyplot as plt

def logistic_map(r, x0, num_steps):
    """
    Simulates the logistic map for a given parameter r and initial condition x0.

    Parameters:
        r : float
            Growth rate parameter.
        x0 : float
            Initial population proportion.
        num_steps : int
            Number of time steps to simulate.

    Returns:
        x_values : array
            Array of population proportions over time.
    """
    x_values = np.zeros(num_steps)
    x = x0
    for i in range(num_steps):
        x_values[i] = x
        x = r * x * (1 - x)
    return x_values

# Parameters
r_values = np.linspace(2.5, 4.0, 1000)
x0 = 0.5
num_steps = 1000

# Generate bifurcation diagram
plt.figure(figsize=(10, 6))
for r in r_values:
    x_values = logistic_map(r, x0, num_steps)[-100:]
    plt.plot([r] * len(x_values), x_values, ',b', alpha=0.25)  # Plotting individual points
plt.xlabel('r')
plt.ylabel('Population Proportion')
plt.title('Bifurcation Diagram of the Logistic Map')
plt.show()

Below is the modified Python code that simulates four growth parameters (1, 2, 3, and 4) and places the four simulations in a single plot with four subplots. It also runs a second simulation where inputs are randomly modified by 5%.

import numpy as np
import matplotlib.pyplot as plt

def logistic_population(r, x0, num_steps):
    """
    Simulates the logistic population growth model.
    
    Parameters:
        r : float
            Growth rate parameter.
        x0 : float
            Initial population.
        num_steps : int
            Number of time steps to simulate.
    
    Returns:
        population : array
            Array of population counts over time.
    """
    population = np.zeros(num_steps)
    population[0] = x0
    for i in range(1, num_steps):
        population[i] = r * population[i - 1] * (1 - population[i - 1])
    return population

# Parameters
growth_parameters = [1, 2, 3, 3.2, 3.5, 3.7]  # Growth rate parameters
x0 = 0.1  # Initial population
num_steps = 50  # Number of time steps

# Create subplots
fig, axs = plt.subplots(3, 2, figsize=(12, 8))

# Run simulations and plot on subplots
for i, r in enumerate(growth_parameters):
    row = i // 2
    col = i % 2
    population = logistic_population(r, x0, num_steps)
    axs[row, col].plot(range(num_steps), population, marker='.', linestyle='-', label=f'r = {r} (Original)')
    axs[row, col].set_title(f'Logistic Population Growth (r = {r})')
    axs[row, col].set_xlabel('Time')
    axs[row, col].set_ylabel('Population')
    axs[row, col].grid(True)
    axs[row, col].legend()

    # Run additional simulations with random initial population variation
    x0_variation = np.random.uniform(-0.05, 0.05)  # Random variation of ±5%
    population_variation = logistic_population(r, x0 * (1 + x0_variation), num_steps)
    axs[row, col].plot(range(num_steps), population_variation, marker='.', linestyle='-', label=f'r = {r}')

# Adjust layout
plt.tight_layout()

# Show plot
plt.show()

Leave a Reply

Your email address will not be published. Required fields are marked *