Mandelbrot Set in Excel

In my previous post, I promised "something fun with complex numbers in Excel". This is it.

The Mandelbrot Set

The Mandelbrot Set is famous as a very visually-appealing fractal. It is based on iteratively applying a simple rule to complex numbers (which comprise a real and an imaginary part).

A complex number, z0, is part of the Mandelbrot Set if it remains bounded (i.e. does not increase toward infinity) no matter how many times the following iteration is applied.

zn+1 = zn2 + z0

As long as the absolute value of zn ≤ 2, it is considered bounded.

Implementation in Excel

Spreadsheets are far from the most elegant or powerful tool for computing fractals, but by having intermediate results and equations conveniently available for inspection, they can aid understanding. Their grid structure can also be a good starting point (albeit, one that comes with its own limitations) for setting up repetitive calculations.

Here's how I used the complex number tools in Excel to draw the Mandelbrot set. First, I set up a grid of complex numbers. The real coefficient ranges from -1.25 to -0.45 in the example shown below (along the first row) and the imaginary coefficient ranges from -0.8 to 0.8 (along the first column). The grid is filled in with the following function:

=COMPLEX(B$1,$A2) 

Each subsequent iteration is calculated with the following function. Note that the squared term (which actually uses the IMPRODUCT function as ^2 notation does not work with complex numbers in Excel) uses relative referencing so that it can be copied and always refer to the previous iteration, while the final added term uses absolute referencing to the initial grid of complex numbers.

=IMSUM(IMPRODUCT(B2,B2),$B$2)

I copied-and-pasted a grid with this equation repeatedly (up to 10 iterations for this example). Then, on another tab, I calculated the absolute value of each cell from each iteration:

=IMABS(Iterations!B2)

The first image shows the start of the calculation page.

Calculation page

The next image shows the absolute value calculations. Conditional formatting has been applied to darken cells with an absolute value less than 2. In the tenth iteration, the outlines of the part of the Mandelbrot Set from -1.25 – 0.8i to -0.45 + 0.8i can begin to be seen.

After 10 iterations

Further Information

A proper implementation (in obfuscated Python code, no less!) can be found here. The following image was generated using that code.

Mandelbrot Set image generated using Preshing's code

To conclude this post, Benoit Mandelbrot, the discoverer of the Mandelbrot Set, has an interesting TED talk about fractals.

Permalink