← Back to Mandelscope

Understanding and Programming the Mandelbrot Set

Mathematical explanation and step-by-step implementation guide

What is the Mandelbrot Set?

The Mandelbrot set is a mathematical set of complex numbers that produces beautiful fractal patterns when visualised. It was discovered by Benoit Mandelbrot in 1980 and has become one of the most famous examples of mathematical beauty.

The set is defined by a surprisingly simple iterative formula applied to complex numbers, yet it produces infinitely complex patterns. What makes it special is that you can zoom into the boundary forever and keep finding new intricate structures at every scale.

The Mathematics

Complex Numbers Review

Complex numbers have the form a + bi, where:

To multiply complex numbers: (a + bi)(c + di) = (ac - bd) + (ad + bc)i

The Mandelbrot Formula

For each complex number c, we test whether it belongs to the Mandelbrot set by repeatedly applying this formula:

zn+1 = zn² + c

Starting with z₀ = 0, we calculate:

The Membership Test

Key Concept: A complex number c is in the Mandelbrot set if the sequence stays bounded (doesn't "escape to infinity") as we iterate forever.

In practice, we use two rules to decide if a number is in the set:

  1. Escape condition: If |z| > 2 at any point, the sequence will definitely escape to infinity. The number is NOT in the set.
  2. Iteration limit: If we've done many iterations (e.g., 100) and |z| is still ≤ 2, we assume the number IS in the set.
Note: The magnitude |z| of a complex number z = a + bi is calculated as √(a² + b²)

Worked Example

Let's test whether c = 0.3 + 0.5i is in the Mandelbrot set:

For this value, |z| remains less than 2 throughout many iterations, so c = 0.3 + 0.5i is in the Mandelbrot set.

Colouring and Visualisation

The beautiful colours you see in Mandelbrot images aren't part of the mathematical set itself — the Mandelbrot set is typically shown as dark! The colours represent how quickly points outside the set escape to infinity:

The main Mandelscope explorer uses "Smooth Periodic Wave Colouring" which creates fluid rainbow patterns by using the final iteration count and the magnitude of z to generate smooth, continuous colour transitions.

Why Complex Numbers?

Complex numbers are essential because they give us a 2D space to explore. The real part of c corresponds to the x-axis (horizontal position), and the imaginary part corresponds to the y-axis (vertical position). This allows us to visualise the set as an image where each pixel represents testing a different complex number.

Connection to Julia Sets

Julia sets are closely related to the Mandelbrot set: instead of fixing z₀ = 0 and varying c, we fix c to a specific value and vary z₀. Each point in the Mandelbrot set corresponds to a different Julia set. Points inside the Mandelbrot set produce connected Julia sets, while points outside produce disconnected "dust" Julia sets.

You can explore Julia sets in the main Mandelscope application by clicking on any point in the Mandelbrot view!

The Algorithm

Key Implementation Details

Working Implementation

Here's a complete, minimal implementation in JavaScript using a 2D canvas. This code draws a simple two-colour Mandelbrot set with no additional features.

600×450 pixels, 100 iterations per point

JavaScript Implementation

Editable Code: Try modifying the code below and click "Run Code" to see your changes!

Try These Experiments:
  • Change maxIterations to 50 or 200 to see how detail changes
  • Modify the viewing window (xMin, xMax, yMin, yMax) to zoom in
  • Change the colours: try '#ff5555', '#88ff88', or experiment with different hex codes
  • Add a third colour for points that escape quickly: if (iterations < 10) ctx.fillStyle = '#999';

← Back to Mandelscope