# Creating Fractals

## Exercise

The purpose of the present exercise is to generate a fractal with the method of Iterated Maps. This method is demonstrated in the Game of Chaos for an example.

Consider the initial point $P_0$ with coordinates ($x_0$, $y_0$) = (0,0). We will produce 10,100 other points in sequence using the following procedure: the coordinates ($x_k$, $y_k$) of a generic point $P_k$ will be computed using the coordinates ($x_{k-1}$, $y_{k-1}$) of the previous point in the sequence $P_{k-1}$ using the following formulas:

$a, b, c, d, e,$ and $f$ are coefficient that are assigned in the table below.

Every time we generate a new point we select one of the rows at random in the table above and use the coefficients in that row to compute the coordinates of the point. The rows are not be selected with uniform probability, but with the following ones:

• Pr(A) = .85
• Pr(B) = .07
• Pr(C) = .07
• Pr(D) = .01

Finally, we then draw the last 10,000 point you generated.

Below is the code to execute the above detail exercise.

#### Generate Functions Which Do the Heavy Lifting

​x1from numpy.random import choice2​3def select_locator():4    """Method to select the row randomly (w/ weights)"""5    locator_list = ["A", "B", "C", "D"]6    locator = choice(locator_list, 1, p=[0.85, 0.07, 0.07, 0.01])7    return locator8​9def get_coefficients(locator):10    """Method to return the coefficients to use"""11    if locator == "A":12        a = .8513        b = .0414        c = -.0415        d = .8516        e = 0.017        f = 1.618        return a,b,c,d,e,f19    elif locator == "B":20        a = .221        b = -.2622        c = .2323        d = .2224        e = 0.025        f = 1.626        return a,b,c,d,e,f27    elif locator == "C":28        a = -.1529        b = .2830        c = .2631        d = .2432        e = 0.033        f = .4434        return a,b,c,d,e,f35    elif locator == "D":36        a = 0.037        b = 0.038        c = 0.039        d = .1640        e = 0.041        f = 0.042        return a,b,c,d,e,f43​44def get_new_points(a,b,c,d,current_x,current_y):45    """A function to calculate the new points location 46    based on the previous points location."""47​48    new_x = a * current_x + b * current_y + e49    new_y = c * current_x + d * current_y + f50    51    return new_x, new_y

#### Checking the select_locator() function works properly via quick simulation...

xxxxxxxxxx141import pandas as pd2import matplotlib.pyplot as plt3import seaborn as sns4​5# Simulate 10000 draws and plot the histgram6options = []7for x in range(10000):8    options.append(select_locator())9​10plt.figure(figsize=(6,6))11_ = pd.Series(options).value_counts().plot(kind = 'bar', zorder=3)12​13plt.grid()14sns.despine() #### Calculate New Coordinate Points

xxxxxxxxxx251# Create original points2x_vals = 3y_vals = 4​5# Number of new points to run...6new_points = 101007​8# Calculate all the new points...9for x in range(new_points):10    11    # Get coefficient locator12    locator = select_locator()13    14    # Get the coefficients15    a,b,c,d,e,f = get_coefficients(locator)16    17    # Get most recent coordinates18    current_x = x_vals[-1]19    current_y = y_vals[-1]20    21    # Pass coefficients and coordinates to calculate the new locations22    new_x, new_y = get_new_points(a,b,c,d,current_x,current_y)23    24    x_vals.append(new_x)25    y_vals.append(new_y)

#### Create Fractal Figure

xxxxxxxxxx71plt.figure(figsize=(10,10))2​3plt.scatter(x_vals[:], y_vals[:], c="#005400")4​5sns.despine(left=True, bottom=True)6plt.yticks([])7plt.xticks([]) 