Visualizing The Mandelbrot Set

For some mathematical functions, let's call one of these functions f, interesting things happen when you take the result of applying f to a value, take the result and again apply f to it; creating a loop by using the result as the new input to the function. Following this process may seem like a funny thing to do. After all, most math courses never mention doing this kind of repetition. Certainly, function composition is a topic of algebra, but why are students not shown some of the things that can arise when doing this? Anyway, surprising things can happen when this looping is done. The Mandelbrot set is just one of these surprises.

To get a better idea of this looping, or iteration, of a mathematical function some notation will help. Let zn be the nth z value. So the first z value will be written as z0, the second as z1, and so on. Using this notation the generation of four successive values can be shown this way:

You may have seen pretty images of the Mandelbrot set (or M-set), so how are they generated? The type of function that defines the M-set is called a complex quadratic polynomial; this funny term just means that the function is a polynomial (with 2 being the highest power) and that it operates on complex numbers, not the real numbers. The function (which I'll continue calling f) has this definition:

zn+1 = f(zn) = zn2+c

There is a lot going on in the above equation at first sight, but it really is very simple. Only two things happen: zn is squared and the result is summed with c. Just two elementary mathematical operations are present. Only a small complication is introduced because complex numbers are used, but still: only two operations! This is how you use f to generate a sequence of complex numbers and determine if the number if part of the M-set:

    1. Begin by picking a complex number c and let z0 be equal to the complex number 0, i.e. 0+0i
    2. Apply the Mandelbrot function to generate the second value in the series: z1 = f(z0)
    3. With that done, go back to step 2 and substitute the value of z1 for z0 and compute f for this new value.

I bet you have already noticed that an infinite loop exists going from step 3 to step 2. The way to know when to stop is to examine the value of zn. Any complex number c is in the M-set only if the absolute value of zn is less than or equal to 2. Or in more mathematical terms: c is only a member of the M-set when |zn| ≤ 2 for all n ≥ 0. Of course, since zn can't be computed for all n, the computation must stop after some value of n is exceeded.

You now have enough background to take a given complex number, compute a sequence using f, and determine if the starting complex number is a member of the M-set. So now back to the pretty images. An image of the Mandelbrot set is generated by treating each pixel of the image as a number from the complex plane (this is the value assigned to c) and running the operations mentioned above. If c is in the M-set then color the corresponding pixel black (for instance), otherwise color the pixel white to show that it isn't part of the M-set. Doing this creates an image such as the one shown below:

View the full size image.

One of the most striking features of this image is the self similarity that it shows. Notice that the main bulb in the image has smaller buds attached to it that themselves resemble the whole M-set. Things that possess self similar properties are said to be fractal. An example of the fractal nature of the M-set can most easily be seen by magnifying and looking more closely at some part of the M-set. When this is done smaller features are seen that look just like the features that were present before the magnification. This self similarity, regardless of scale, is the essence of what it means for something to have a fractal nature.

Black and white images of the M-set are rather plain. How are the more colorful images created? One method is to count how many iterations of applying f it takes until |zn| is greater than 2. For a particular point, if it takes five iterations to exceed 2 then set the corresponding pixel to some color. If two iterations are needed use a different color for the pixel. Usually there is a predefined table or rule that is used to determine the color.

One way to think of the coloring problem is that for some point c if |zn| becomes greater than 2 after only a couple of iterations then think of that point as being hot or moving fast. Color the corresponding pixel a "hot" color, such as yellow or red. In the same way if many iterations are needed color the pixel a slow or "cool" color such as blue or violet. The result of using this method is shown in the next image.

View the full size image.

In the above image, points for which |zn| becomes greater than 2 after the fewest iterations are colored white. These points are said to "escape" very quickly. Points that escape more slowly are given cooler colors. The cooler red and blue colors can only be seen close to the boundary of the M-set (viewing the full size image will help to see this).

Other coloration schemes are possible as the following images show.

Areas outside of the M-set are colored black or white depending on whether the number of iterations is even or odd.

In this image points which have an iteration number divisible by 2 are colored black, points divisible by 3 are white, and if neither is the case the point is colored based on the number of iterations it took to escape.

This image is similar to the first except that only areas outside the M-set which have an iteration value divisible by 3 are colored black.

So that I would have firsthand experience creating images of the Mandelbrot set, I wrote my own software. Lots of free software exists to create images of the M-set, but I would like to challenge the reader to write their own programs. Don't worry if you can't write such programs at the moment, go ahead and play with the M-set, there are many other mathematical objects you can examine in the future by writing your own software, if you wish.

Using the mset program

Instead of creating a separate page for the software, I'm going to include some instructions here. I apologize that the software isn't very user friendly, but if you know C++ and run Linux, Unix, or Cygwin on Windows you should be ready to go. Other software that is needed by my program is the GD image library and the GNU MP library. Oh, there is also a Ruby script in the archive that can create sequences of images that zoom in on a point; I used images generated by this script to create a movie (which isn't online yet) that zooms in on the so-called sea horse valley.

Two versions of the program exist: mset and mset-double. The first uses the GNU MP library for numerical calculations and the second uses the double floating point data type for calculations. The reason for the two versions is that I hoped that by using multiple precision arithmetic, sharper and more detailed images could be created at small scales. Unfortunately, the images produced by both programs look to be the same. Maybe the algorithm I used is to blame?

Other web sites of interest

    • Mandelvox is a Java applet that generates sounds based on the orbits of user selected points in the Mandelbrot set. Check it out, it's very cool!
    • The Buddhabrot fractal method page describes a way to generate an image of the Mandelbrot set that looks like a budda.
    • Clifford Pickover is an author who writes about mathematics and many other things. His books have helped me keep and interest in math, and computer graphics and visualization.
    • The Julia set is a fractal mathematical object much like the Mandelbrot set.

Anyway, I hope that you have fun with this interesting area of mathematics!