Skip to content

Instantly share code, notes, and snippets.

@sohang3112
Last active April 20, 2024 14:46
Show Gist options
  • Save sohang3112/a11b5612f795cffa8cdf0384ac0f49c0 to your computer and use it in GitHub Desktop.
Save sohang3112/a11b5612f795cffa8cdf0384ac0f49c0 to your computer and use it in GitHub Desktop.
Plot of Chaos experiment from math talk Four Ways of Thinking: https://youtu.be/PPCfDe8TfJQ

Doubling Rule (math chaos experiment)

In his talk Four Ways of Thinking, mathematician David Sumpter did a Chaos experiment with his audience. Here, I have plotted it in Python that shows the chaotic result plot.

Chaos definition: In maths, Chaos means a system where a small difference in a system's starting conditions can lead to huge changes in the final result.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

NUM_ITERS = 100       # can vary this no. of iterations to see how the result plot would change
def eventual_number(x: int) -> int:
    """Both x and return value (y) are between (inclusive) 1 to 99"""
    for _ in range(NUM_ITERS):
        if x < 50:
            x = 2 * x
        elif x > 50:
            x = 2 * (100 - x)
        elif x == 50:
            x = 99    # spl case for 50 - doubling would get 100, which is outside required bounds of 0 <= x <= 99
    return x

# Draw scatter plot
initial_vals = np.array(range(1, 100))
final_vals = np.array([eventual_number(x) for x in range(1,100)])
plt.scatter(initial_vals, final_vals)
plt.show()

Chaos plot

@sohang3112
Copy link
Author

sohang3112 commented Apr 20, 2024

Upon reflection, the above "chaos" plot actually appears to have a regular pattern of rise & fall.

Fun Problem: Try to approximate this with a function 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment