Skip to main content
Mathematics LibreTexts

14.3: Fractals

  • Page ID
    65564
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In this section we are going to explore using transformations to generate fractals. Consider the following set of linear equations. Each one takes a 2D point as input, applies a \(2 \times 2\) transform, and then also translates by a \(2 \times 1\) translation matrix

    \[\begin{split}
    T_1:\left[ \begin{matrix}
    x_1 \\
    y_1
    \end{matrix}
    \right]
    =
    \left[ \begin{matrix}
    0.86 & 0.03 \\
    -0.03 & 0.86
    \end{matrix}
    \right]
    \left[ \begin{matrix}
    x_0 \\
    y_0
    \end{matrix}
    \right] +
    \left[\begin{matrix}
    0\\
    1.5
    \end{matrix}
    \right]
    : probability = 0.83 \end{split} \nonumber \]

    \[\begin{split}
    T_2: \left[ \begin{matrix}
    x_1 \\
    y_1
    \end{matrix}
    \right]
    =
    \left[ \begin{matrix}
    0.2 & -0.25 \\
    0.21 & 0.23
    \end{matrix}
    \right]
    \left[ \begin{matrix}
    x_0 \\
    y_0
    \end{matrix}
    \right] +
    \left[\begin{matrix}
    0\\
    1.5
    \end{matrix}
    \right]
    : probability = 0.08 \end{split} \nonumber \]

    \[\begin{split}
    T_3 : \left[ \begin{matrix}
    x_1 \\
    y_1
    \end{matrix}
    \right]
    =
    \left[ \begin{matrix}
    0.15 & 0.27 \\
    0.25 & 0.26
    \end{matrix}
    \right]
    \left[ \begin{matrix}
    x_0 \\
    y_0
    \end{matrix}
    \right] +
    \left[\begin{matrix}
    0\\
    0.45
    \end{matrix}
    \right]
    : probability = 0.08 \end{split} \nonumber \]

    \[\begin{split}
    T_4: \left[ \begin{matrix}
    x_1 \\
    y_1
    \end{matrix}
    \right]
    =
    \left[ \begin{matrix}
    0 & 0 \\
    0 & 0.17
    \end{matrix}
    \right]
    \left[ \begin{matrix}
    x_0 \\
    y_0
    \end{matrix}
    \right] +
    \left[\begin{matrix}
    0\\
    0
    \end{matrix}
    \right] : probability = 0.01 \end{split} \nonumber \]

    We want to write a program that use the above transformations to “randomly” generate an image. We start with a point at the origin (0,0) and then randomly pick one of the above transformation based on their probability, update the point position and then randomly pick another point. Each matrix adds a bit of rotation and translation with \(T_4\) as a kind of restart.

    To try to make our program a little easier, lets rewrite the above equations to make a system of “equivelent” equations of the form \(Ax=b\) with only one matrix. We do this by adding an additional variable variable \(z=1\). For example, verify that the following equation is the same as equation for \(T_1\) above:

    \[\begin{split}
    T_1: \left[ \begin{matrix}
    x_1 \\
    y_1
    \end{matrix}
    \right]
    =
    \left[ \begin{matrix}
    0.86 & 0.03 & 0 \\
    -0.03 & 0.86 & 1.5
    \end{matrix}
    \right]
    \left[ \begin{matrix}
    x_0 \\
    y_0 \\
    1
    \end{matrix}
    \right]
    \end{split} \nonumber \]

    Please NOTE that we do not change the value for \(z\), and it is always be 1.

    Do This

    Verify the \(Ax=b\) format will generate the same answer as the \(T_1\) equation above.

    The following is some pseudocode that we will be using to generate the Fractals:

    1. Let \(x=0\), \(y=0\), \(z=1\)
    2. Use a random generator to select one of the affine transformations \(T_i\) according to the given probabilities.
    3. Let \((x′,y′)=T_{i}(x,y,z)\).
    4. Plot \((x′,y′)\)
    5. Let \((x,y)=(x′,y′)\)
    6. Repeat Steps 2, 3, 4, and 5 one thousand times.

    The following python code implements the above pseudocode with only the \(T_1\) matrix:

    %matplotlib inline
    
    import numpy as np
    import matplotlib.pylab as plt
    import sympy as sym
    sym.init_printing(use_unicode=True) # Trick to make matrixes look nice in jupyter
    
    T1 = np.matrix([[0.86, 0.03, 0],[-0.03, 0.86, 1.5]])
    #####Start your code here #####
    T2 = T1 
    T3 = T1
    T4 = T1
    #####End of your code here#####       
    
    prob = [0.83,0.08,0.08,0.01]
    
    I = np.matrix([[1,0,0],[0,1,0],[0,0,1]])
    
    fig = plt.figure(figsize=[10,10])
    p = np.matrix([[0.],[0],[1]])
    plt.plot(p[0],p[1], 'go');
    for i in range(1,1000):
        ticket = np.random.random();
        if (ticket < prob[0]):
            T = T1
        elif (ticket < sum(prob[0:2])):
            T = T2
        elif (ticket < sum(prob[0:3])):
            T = T3
        else:
            T = T4
        p[0:2,0] = T*p    
        plt.plot(p[0],p[1], 'go');
    plt.axis('scaled');
    Do This

    Modify the above code to add in the \(T_2\), \(T_3\) and \(T_4\) transforms.

    Question

    Describe in words for the actions performed by \(T_1\), \(T_2\), \(T_3\), and \(T_4\).

    Do This

    Using the same ideas to design and build your own fractal. You are welcome to get inspiration from the internet. Make sure you document where your inspiration comes from. Try to build something fun, unique and different. Show what you come up with with your instructors.


    This page titled 14.3: Fractals is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.