Fractals: The Mandelbrot Set

Fractal Intro Mandelbrot Sierpinski Gallery Return to Homepage

Construction:





The Mandelbrot set is the set of all complex numbers c such that iterating z <= z^2 + c does not ascend to infinity, starting with z=0.


The terms z and c are complex numbers (see Note 1 for an overview of complex numbers, if necessary). "Ascend to infinity" means that z will continue to grow with each iteration; in calculus terms, it means that z diverges; more on this, as well as the initial condition z=0, later. I will proceed with explaining simply how to graph the set, and place explanations for the mathematical intricacies in footnotes for those curious.

We could probably find a few elements in the Mandelbrot set by pencil and paper, but in order to crank out enough iterations to produce even a semi-decent graph before dying of either old age or a mental segmentation fault, we will want to determine the elements of the Mandelbrot set using a simple computer program. I will use Java-like pseudocode. Let us first declare a complex number class complexNumber:


// A class for complex numbers.
class complexNumber {

  private double a, b;                                               // a and b from the form a + bi     

  public complexNumber(double realNumber, double imaginaryNumber) {  // Class constructor.  
    a = realNumber; b = imaginaryNumber;
  }

  public double realPart() {                                         // Returns real value a.
    return a;
  }

  public double imaginaryPart() {                                    // Returns imaginary value b.
    return b;
  }

  public double absoluteValue() {                                    // Returns vector magnitude, sqrt(a^2 + b^2).
    return (sqrt(exp(a,2)) + exp(b,2)));
  }

}

Each object in the complexNumber class has two double-precision fields, a and b. The functions are self-explanatory.

With the one sentence description of the Mandelbrot set at the top of the page, we will write a function checkMandelbrotElement() for checking whether c is a member of the Mandelbrot set. Let z be a local free variable, and c be an input constant. We initialize z to 0(see Note 2 for an explanation of this initialization). Following the described algorithm, we would start by reassigning z to the value of z^2 + c = 0^2 + c = c. Then we reassign z to the sum of c and the square of the new z we had just computed (e.g. the second iteration is z = c^2 + c, third iteration is z = (c^2 + c)^2 + c, fourth is z = [(c^2 + c)^2 + c]^2 + c, etc.) ... and continue doing this for some large number of iterations N. This can be done with a for loop. As for an exit condition for our loop, with each iteration we will check the value of |z| and see if it is greater than 2. If it is, the value of z will diverge to infinity in further iterations (see Note 3 for a proof), and we thus we know that c is not a solution. Otherwise, we iterate further; if |z| remains less than or equal to 2 for all N iterations, c is a solution. Pseudocode:


checkMandelbrotElement(int N, complexNumber c) {

  for (int i=N, complexNumber z=0; i>=0; i--) {
    if (i == 0) {
      inTheSet(c);
    } else if (z.absoluteValue() > 2) {
      notInTheSet(z);
      break;
    } else {
      z = z^2 + c;
    }
  }

}

The program implements the described algorithm, taking as input the number of iterations N and the complex number c. Reexplaining the algorithm in terms of the code above, we declare a local complexNumber-typed variable z on which we do the iterative reassignments necessary for checking if c is a solution. These iterations are done in a simple for loop which is set to execute a maximum of N iterations. If the magnitude of z at any iteration is greater than 2, we can immediately mark z as not being in the solution set, and exit the loop with break(). Otherwise, we reassign z and continue looping; if |z| is never greater than 2 after N iterations, the computer finally evaluates the first if statement (whose condition is true when i is 0), marking z as a solution with function "inTheSet(z)," and then exits the loop normally as i is decremented to -1.

So we have a function that checks whether any one complex number c is in the set. Now we will want to apply this function to many values of c to get a better picture of what values are in the set. We can have a computer execute checkMandelbrotElement() on a large domain in the complex plane - for instance, the set of all pixels on a a computer screen, where pixels represent complex numbers (horizontal indices mapping to their real parts on the complex plane, and vertical indices mapping to their imaginary parts on a complex plane:


// True monitors do not have the same width as height, but we will assume so to make computation easier.
#define monitorWidth 800;
#define monitorHeight 800;

generateMandelbrot(complexNumber c, int N) {

  int pixels[i][j];

  for (i=0; i<=monitorWidth; i++) {
    for (j=0; j<=height, j++) {

    // Note 4 explains subtracting (i,j) by 400 and dividing by 200
    complexNumber z = new complexNumber( ((i-400)/200), ((j-400)/200) );
    checkMandelbrotElement(N, z, c);
    }
  } 



Thus we now have a program which checks if each pixel on a screen is a member of the Mandelbrot set dictated by the parameters c and N given to function generateMandelbrot(), c being the parameter space and N giving greater levels of detail for greater values. We could graph this set on a computer screen by assigning a certain color to each pixel based on whether they are in the set or not. In the first code segment, function checkMandelbrotElement() also calls functions inTheSet() and notInTheSet(), which mark whether or not the complex plane vector corresponding to a particular pixel is in the Mandelbrot set. Let us say that inTheSet() colors pixels black, while notInTheSet() colors them white. Following this scheme, the code we have written would generate a picture similar to that shown on the left, displaying the distinctive shape of the Mandelbrot set.



How can we get more colors? The code above only puts pixels in one of two categories, in the set, or not in the set. To get more categories, we could make a more complicated program that assigns a color to pixel c according to the value of z for that c after N iterations. Some values will quickly converge to zero, some will reach an cyclic equilibrium, some will immediately diverge to infinity. These growth differences allow for such divisions as "infinitely small", "relatively small", "relatively large", and "infinitely large", each mapping to a different color; we could even generate a 3-dimensional graph by mapping pixel values to heights. This concludes the construction.





Mandelbrot Set Variations:

The recursive function z <= z2 + c can be considered a special case of the formula z <= zn + c, where n is preferably a positive integer. Below are graphs of this more general formula for n = 2, 3, and 4. Fractals can also be made from the formula z <= z-barn + c, where z-bar is the complex conjugate of z; such fractals are called Mandelbar sets. Below are graphs of these formulae for n = 2, 3, and 4. Notice the pattern of expansion in z <= zn + c. The growth pattern in the Mandelbar set is more obvious, each shape resembling a polygon with n+1 sides, and flaring at the corners.



n z <= zn + c z <= z-barn + c
2
3
4







We end our discussion of the Mandelbrot set with two pictures. The left picture (leftmost) views the entire Mandelbrot set. The right picture is actually the highly magnified image of a small fraction of the first picture. Look closely at the center of the second picture, and you can see a small black figure identical to the leftmost picture but reduced in size. Click on the file link below to witness this cyclic repetition of the set's complexities. The fantastic 12-second MPEG clip starts at topmost view of the Mandelbrot set, and zooms in, showing reductions of the same figure we started with along the way. Highly recommended.


Update: Looks like I have lost the aformentioned file. More precisely, inst.eecs.berkeley.edu erased it. But here is a much better one, produced by Thomas Marsh and Jan Hubicka of Xaos. Includes many zoom-ins. The clip cannot be fast forwarded, so just watch the whole thing patiently.

MPEG Tutorial On The Mandelbrot Set
MPEG Tutorial On The Julia Set





Footnotes:





Home Introduction to Fractals Mandelbrot Sierpinski Gallery


William Wu, Copyright 2000-2002 ©. All rights reserved.