Unpredictable outcome#

Let’s warm up for the task a bit and do some simple calculations.

Let’s translate the mathematical formula into something more imperative first:

- take a number
- update the number by squaring it and adding the original number to it
- repeat this a few times and watch the value of the number at each iteration

To put this into a valid python algorithm we could write a function to support us.

def calculate(x, max_iterations=10):
    print(f"{x=}, {max_iterations=}")
    x0 = x
    for index in range(max_iterations):
        x = x**2 + x0
        print(x)

Let’s try this with a few numbers that immediately come to mind. Say numbers like these …

numbers = [0, 1, .5, .25, .125, -.125, -.25, -.5, -1, -1.5, -2, -2.1]
Hide code cell source
import ipywidgets as ipw

tabs_children = []
numbers = [
    (0, 5),
    (1, 10),
    (.5, 10),
    (.25, 10),
    (.25, 20),
    (.25, 30),
    (.125, 20),
    (-.125, 10),
    (-.25, 10),
    (-.5,  10),
    (-1, 10),
    (-1.5, 20),
    (-2, 5),
    (-2.1, 10)
]
for number,max_iterations in numbers:
    output = ipw.Output()
    with output:
        calculate(number, max_iterations)
    tabs_children.append(output)
    
tabs = ipw.Tab()
tabs.children = tabs_children
for index, number in enumerate(numbers):
    tabs.set_title(index, f"{number}")
    
tabs

Quite an interesting behaviour for a simple series definition. And highly unpredictable.

But I think we need a more visual approach here to understand the details.