Naive Mandelbrot Set

By Monty, 17th April 2015

MandelbrotThis is your basic Mandelbrot Set plotter. Very little effort made to speed it up, except for plotting top & bottom pixels together as they’re mirrored across the x-axis. Later I’ll show you some much faster programs!

The heart of this program is the iteration of z2+c where c is a point in the complex plane and z is initially complex 0. c is a member of the Mandelbrot Set if z remains bounded forever. In practice only a finite number of iterations is made, after which we assume the point is in the set. Also, it’s known that if z’s magnitude exceeds 2, then it will eventually escape.

It was originally written for Pygame, which I then found wasn’t set up for Python 3 on my computer. Converting it to Pillow was an easier option than dealing with the lengthy instructions I Googled for upgrading my Pygame (I’ll get around to it later…). I’ve left in the Pygame instructions as comments in case you want to go that way đŸ™‚

#   Python+Pillow/Pygame program to illustrate computing the Mandelbrot Set.
#   Note that it's far from efficient; it can easily be sped up!
#   Authour: Alan Richmond,

#import pygame                                       # see
from PIL import Image
width, height = 1000,1000                           # display window size
#screen = pygame.display.set_mode((width, height))   # initialise pygame window"RGB",(width,height))
xaxis = width / 1.5 + 140                           # scaling for x & y axes
yaxis = height / 2
scale = 400
maxit = 99                                          # maximum iterations
for iy in range(int(height/2)+1):                   # scan y-axis
    for ix in range(width):                         # scan x-axis
        z = 0 + 0j                                  # initialise z=0
                                                    # map pixel position to complex plane
        c = complex(float(ix - xaxis) / scale, float(iy - yaxis) / scale)

        for it in range(maxit):                     # up to maximum iterations:
            z = z*z + c                             # iterate z^2 + c
            if abs(z) > 2:                          # z is flying off to infinity!
                col=(it % 4 * 64, it % 8 * 32, it % 16 * 16)    # pick a colour
                break                               # break out of closest loop
        else:                                       # loop finished so
            col = (0, 0, 0)                         # point is in set = colour black

#        screen.set_at((ix, iy), col)                # set colour on top half
#        screen.set_at((ix, height-iy), col)         # set colour on bottom half
#    pygame.display.update()                         # update window on screen
#raw_input("Done")                                   # picture disappears when Enter"Mandelbrot.png", "PNG")

The Mandelbrot set is the set of complex numbers c {\displaystyle c} for which the function f c ( z ) = z 2 + c {\displaystyle f_{c}(z)=z^{2}+c} does not diverge when iterated from z = 0 {\displaystyle z=0} , i.e., for which the sequence f c ( 0 ) {\displaystyle f_{c}(0)} , f c ( f c ( 0 ) ) {\displaystyle f_{c}(f_{c}(0))} , etc., remains bounded in absolute value.

Its definition and name are due to Adrien Douady, in tribute to the mathematician Benoit Mandelbrot. The set is connected to a Julia set, and related Julia sets produce similarly complex fractal shapes.

Mandelbrot set images may be created by sampling the complex numbers and determining, for each sample point c {\displaystyle c} , whether the result of iterating the above function goes to infinity. Treating the real and imaginary parts of c {\displaystyle c} as image coordinates ( x + y i ) {\displaystyle (x+yi)} on the complex plane, pixels may then be colored according to how rapidly the sequence z n 2 + c {\displaystyle z_{n}^{2}+c} diverges, with the color 0 (black) usually used for points where the sequence does not diverge. If c {\displaystyle c} is held constant and the initial value of z {\displaystyle z} —denoted by z 0 {\displaystyle z_{0}} —is variable instead, one obtains the corresponding Julia set for each point c {\displaystyle c} in the parameter space of the simple function.

Images of the Mandelbrot set exhibit an elaborate and infinitely complicated boundary that reveals progressively ever-finer recursive detail at increasing magnifications. The "style" of this repeating detail depends on the region of the set being examined. The set's boundary also incorporates smaller versions of the main shape, so the fractal property of self-similarity applies to the entire set, and not just to its parts.

The Mandelbrot set has become popular outside mathematics both for its aesthetic appeal and as an example of a complex structure arising from the application of simple rules. It is one of the best-known examples of mathematical visualization.

What do you think?

Leave a Reply

%d bloggers like this: