Screaming Stone Design

What is the Mandelbrot Set Anyway?

For some of you who have ever wondered what the Mandelbrot Set is and have performed a Google search you may have been directed to sites such as Wikipedia which gives the following definition:

The Mandelbrot set is the set of complex numbers c for which the function fc(z) = z² does not diverge when iterated from z = 0, i.e., for which the sequence fc(0), fc(fc(0)), etc., remains bounded in absolute value.

Did you find that helpful? I doubt it.

What about the definition given on the Wikipedia for Schools, surely that will give a simple, straightforward explanation:

The Mandelbrot set is a set of points in the complex plane, the boundary of which forms a fractal. Mathematically, the Mandelbrot set can be defined as the set of complex c-values for which the orbit of 0 under iteration of the complex quadratic polynomial xn+1 = xn² + c remains bounded.
Eg. c = 1 gives the sequence 0, 1, 2, 5, 26… which leads to infinity. As this sequence is unbounded, 1 is not an element of the Mandelbrot set.
On the other hand, c = i gives the sequence 0, i, (-1 + i), –i, (-1 + i), -i… which is bounded, and so it belongs to the Mandelbrot set.

That was a more complete description but it really wasn’t much more helpful, was it?

There are many descriptions of the Mandelbrot Set on the internet but unless you are a mathematician they are not going to be much use to you.

Here, finally, is an explanation which everyone will be able to understand.

The Complex Plane

The first thing you need to understand is where  the Mandelbrot Set lives and to explain that you need a quick refresher on the Cartesian Plane:

An example of the Cartesian plane

Every point can be “expressed” by where it is in relation to the X-axis and the Y-axis and this is given in the form of an (x,y) “co-ordinate”.

For example:

Easy enough so far.

The Mandelbrot Set lives on a slightly different version of the Cartesian Plane called the Complex Plane - the difference is the Y-axis is replaced by the iY-axis.

An example of the Complex plane

So, in the image above, the red dot is at (5,3i) and the blue dot is at (-4,-3i).

Basically it is exactly the same except that all of the Y values are multiplied by “i”.

The Mandelbrot Set exists entirely within a circle with a radius of 2 and whose centre is the centre of the complex plane:

Complex plane with Mandelbrot Set shown inside the bailout limit (a red circle)

This is important to remember and this circle will be referred to later on as the bailout distance, meaning that any point outside it has a distance which is a greater than 2 from the centre of the plane.

Irrational Number i

When you multiply any number by itself the result is always  positive.

For example: when you multiply 3 by 3 the result is 9:

3 × 3 = 9

The result is the same when any negative number is multiplied by itself, so if you multiply minus 3 by minus 3 the result will also be 9:

-3 × -3 = 9

The “square root” of a number is the number which must be multiplied by itself to arrive at that number, so for example the square root of 9 is 3 (or -3).

It should be fairly obvious that it isn’t possible to have a square root for a negative number but that is exactly what “i”  is - it is the square root of -1, meaning:

i × i = -1

While “i”  may be irrational (or impossible) it is also very useful for achieving something which can not be achieved otherwise, for example:

3i × 3i = -9

It is this special property of “i”  which is at the heart a fractals like the Mandelbrot Set.

Functions

Telling you that the “equation” or the “function”  zn+1 = zn² + c  is what creates the Mandelbrot Set won’t be much use to you unless you know what “z” and “c” are and unless you know what a “function” is.

Think of a “function” as being like a machine: you load raw materials into one end and something new comes out the other end.

For a sausage making machine you load the raw ingredients into one end (fat, gristle, sawdust from the floor, a tiny amount of actual meat)  and sausages come out the other end.

{animated image} Making sausages

In the case of the “function” mentioned above you put an (x,iy) “co-ordinate” into the function and a new (x,iy) “co-ordinate” comes out of it.

A complex function

z, n and c

The function for the Mandelbrot Set given in its simplest form is:

z = z² + c

z” is actually just shorthand for an (x,iy) co-ordinate, so the function can be translated as:

(new x, new iy) = ((x, iy) × (x, iy)) + c

And what is “c”? - it is also an (x,iy) co-ordinate - it is the co-ordinate you started working on so what you really have is:

(new x, new iy) = ((x, iy) × (x, iy)) + (start x, start iy)

And this work out as:

(new x, new iy) = (x × x) + (2 × x × iy) - (y × y) + (start x, start iy)

To get the new x and new iy co-ordinates you separate the different numbers into ones without an “i” which will be the new x co-ordinate and the ones which contain an “i” which will be the new iy co-ordinate:

new x = (x × x) - (y × y) + (start x)
new iy = (2 × x × iy) + (start iy)

n” is simply the number of times the calculation is performed by the function.

Doing the Calculations

So what you do is, you pick an (x,iy) co-ordinate on the complex plane, for example (#### ,####).

This start point will be used every time you calculate the results of the function:

(start x, start iy) = (x, iy)

As you haven’t performed any calculations yet n will be equal to zero:

n = 0

Then you perform the calculation:

new x = (x × x) - (y × y) + (start x)
new iy = (2 × x × iy) + (start iy)

What this effectvely does is it moves from the co-ordinate where you started to a new co-ordinate.

{image} Point translated to a new point

You then test this new co-ordinate to see if it matches certain conditions (given below)  and if not you put the co-ordinate back into the function and perform the calculations again:

{animated image} co-ordinate being put back into function

Again, you test this new co-ordinate to see if it matches certain conditions and if not you put the co-ordinate back into the function:

{image} Point translated to a new point 2

And so on...

{image} Point translated to a new point 3

3 Results

Depending upon the starting co-ordinate which is put into the function  z = z² + c  there are 3 different end results which are produced.

The first end result is the new co-ordinates spiral outwards away from centre of the complex plane and pass beyond the bailout limit (they have a distance from the centre of the comlpex plane which is greater than 2)  towards infinity:

A complex function

The second end result is the new co-ordinates spiral around for infinity, getting closer and closer to a final co-ordinate:

A complex function

The third end result is the co-ordinates oscilate between two or more new co-ordinates, one of which is, or is near to, the starting co-ordinate:

A complex function

For us the interesting result is the first one as this is the one which yields the beautiful and complex pictures we are all familiar with.

Below is an interactive plot of Mandelbrot Set function which shows you exactly what happens at each point - the initial calculation draws a line which is yellow in colour and as more calculations are made the lines which are drawn become more red in colour - just move your mouse pointer over the image to see what happens:

Creating the Image

To create the Mandelbrot Set we first need to define a palette of colours which we can refer to when we have counted the number of times the function has run:

Colour palette

Next we need to define a grid of squares where we will draw our Mandelbrot Set:

Grid of squares

We will run through all of the squares one-by-one and we will use that square’s co-ordinate as the starting co-ordinate in the function in order to calculate what colour we should end up with.

To test for the first of the three possible results (listed above) and to see if the new (x, iy) co-ordinates are moving away from the centre point towards infinity all we need to do is measure the distance from each new (x, iy) to the centre of the complex plane - if the distance is greater than 2 then we count how many calculations were performed and then we know what colour to use on that square.

To test for the other two end results all we need to do is limit how many times the function can run and if that limit is reached we can colour the square black.

Mandelbrot Set rendered with colours shown above

If we want a better picture all we need is to define a grid with smaller aquares:

Mandelbrot Set rendered with colours shown above

That’s it.