May 24, 2013

Last time we plotted a Mandelbrot set using a small Python script. This set is interesting because an unexpectedly large amount of detail emerges from the iteration of a relatively simple function. Even more interesting is the fact that this detail is not limited to any particular resolution. The Mandelbrot set exhibits fractal geometry, meaning that tiny areas of the set share features with the whole thing. If we zoom in, smaller details are visible no matter how far we choose to zoom.

But you don’t have to believe me – let’s modify our Python script and see what happens. Basically we have to map 600 pixels in each direction to arbitrary sections of the real and imaginary axes. We can have our script read in the desired ranges and calculate appropriate mappings. First, store the inputs in decimal variables:

def get_decimal(m):
    while True:
            x = float(input(m))
            return x
        except ValueError:
            print('Enter a decimal number.')

loX = get_decimal('Minimum X: ')
hiX = get_decimal('Maximum X: ')
loY = get_decimal('Minimum Y: ')
hiY = get_decimal('Maximum Y: ')

Divide 600 by the range in each direction to compute scale coefficients:

scaleX = 600.0/(hiX - loX)
scaleY = 600.0/(hiY - loY)

Now modify the drawing code to use our designated ranges:

for x in range(0,600):
    real = loX + x / scaleX
    for y in range(0, 600):
        imag = loY + y / scaleY
    c = complex(real, imag)
    p = mandel(c)
    w.create_line(x, 600-y, x+1, 601-y, fill=colors[p])

With these changes we can zoom in and see some interesting features. Try using -0.15, -0.05, 0.9, and 1.0 as input values. More detail is visible, but it looks like some of the boundaries are smoothing out! Interestingly, that’s because our mandel() function only checks whether each candidate c escapes within 20 iterations. Points close to the boundary often take more than 20 iterations to escape, but they don’t actually belong in the set. Therefore, as the zoom level increases we have to test each candidate c for more iterations in order to maintain an accurate image. This is left as an exercise for the reader.

2 Responses to “Fractals”

  1. David Ziegler Says:

    I just saw Stoppard’s Arcadia last night at ACT in San Francisco. If you haven’t seen (or read) it, you should. The monologue by Valentine in the 4th scene fills in the _feeling_ of working with these fractals:

    VALENTINE. If you knew the algorithm and fed it back say ten thousand times, each time there’d be a dot somewhere on the screen. You’d never know where to expect the next dot. But gradually you’d start to see this shape, because every dot will be a mathematical object. But yes. The unpredictable and the predictable unfold together to make everything the way it is. it’s how nature creates itself, on every scale, the snowflake and the snowstorm. it makes me so happy. To be at the beginning again, knowing almost nothing. People were talking about the end of physics. Relativity and quantum looked as if they were going to clean out the whole problem between them. A theory of everything. But they only explained the very big and the very small. The universe, the elementary particles. The ordinary-sizes stuff which is our lives, the things people write poetry about – clouds – daffodils – waterfall s- and what happens in a cup of coffee when the cream goes in – these things are full of mystery, as mysterious to us as the heavens were to the Greeks. We’re better at predicting events at the edge of the galaxy or inside the nucleus of an atom than whether it’ll rain on auntie’s garden party three Sundays from now. Because the problem turns out to be different. We can’t even predict the net drip from a dripping tap when it gets irregular. Each drip sets up the conditions for the next, the smallest variation blows prediction apart, and the weather is unpredictable the same way, will always be unpredictable. When you push the numbers through the computer you can see it on the screen. The future is disorder. A door like this has cracked open five or six times since we got up on our hind legs. It’s the best possible time to be alive, when almost everything you thought you knew is wrong.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: