What is the Mandelbrot set?

Posted on 22 August 2012 in Techstuff

When I came acros images like those displayed above or those zoom movies you can see on youtube i was wondering for a long time: What is this and how is it made? So i decided to look into it. I knew math was involved to somehow generate these cool images but not much more. It turns out that it's not verry hard to generate a image displaying the Mandelbrot set with a simple program (download below). Here i will try my best to explain what i know, i am not the best at mathematics, i just found it interesting.

Before jumping to the Mandelbrot set i must first explain some things like iteration and complex numbers. After that i can piece them togother.

Iteration

Y = (X*X) + C
Figure 1 - A formula

The above formula has variables in it (X and C) when these variables are assigned a value, Y can be calculated. For example when X = 1 and C = 0.5 Y will be 1.5. So a certain input for the variables X and C yields a certain output Y.

Iteration is using the output of a function as a input value for another calculation. Y is calculated in the first iteration and becomes X in the second iteration. C is a constant value so it never changes. Lets see this in action with the constant C = -1.3

iter		X(Input)		Y(Output)
#########################################################
1		1			-0.3
2		-0.3			0.1641
3		0.1641			-1.27307119
4		-1.27307119		0.32071025480802
5		0.32071025480802	-1.197144932461
Figure 2 - Iteration

In the first five iterations nothing verry interesting is visible, but what if 100 or 1000 iterations were done, would there emerge a pattern? Let's see and try it again with the constant C chosen as -0.2:

iter		X(Input)		Y(Output)
#########################################################
98		-0.17082039324994	-0.17082039324994
99		-0.17082039324994	-0.17082039324994
100		-0.17082039324994	-0.17082039324994
Figure 3 - 100 iterations, single attractor visible

Clearly the iteration is "stuck" at -0.17 this is called an attractor. The cool thing is, is that the input for X in the first iteration does not matter. 0, -0.2, 1.1 will all lead to -0.17082039324994. So the only thing that matters is the constant value C.

Now another test with C = -1.2

iter		X(Input)		Y(Output)
#########################################################
297		-1.1708203932499	0.17082039324994
298		0.17082039324994	-1.1708203932499
299		-1.1708203932499	0.17082039324994
300		0.17082039324994	-1.1708203932499
Figure 4 - Double attractor for C = -1.2

This time it took more iterations to see a attractor and this time there are two repeating values, so there are two attractors for C = -1.2. Not all values of C have a clear attractor, for some values of C the output will be chaotic, this is called a strange attractor.

Complex numbers

Until now only real numbers were used. To move in to the Mandelbrot set we must first know some things about complex numbers. To be honest i dont know everything about complex numbers, i learned just enough to make the Mandelbrot set apear on the screen.

A complex number consists of two parts, a real part and an imaginary part. An example of a complex number is (2 + 1i). So a complex number has two parts, this means it can be graphed along two axis forming a 2d plane, called the complex plane. Complex numbers can be added, subtracted, multiplyed etc. The figure below displays 3 numbers in the plane: Green = (-0.5 + 0.5i), red = (1 + 1i) and blue = (1.5 + -1i).

Figure 5 - Points in the complex plane

The Mandelbrot set

So now what is the Mandelbrot set and what does iteration, attractors and complex numbers have to do with it? Now it's time to combine these and see them in action together.

Z = Z*Z + C
Figure 6 - The Mandelbrot formula

In the above figure the Mandelbrot formula is displayed, it's the same formula as displayed in figure 1 but now with complex numbers. The formula will be used to iterate over points in the complex plane a set number of times, say 80. If during 80 iterations the output is tending towards infinity, the point is considered to be not part of the set. C is the number in the plane beiing tested and is constant during iterations, Z is allways zero the first iteration.

So why is it called the Mandelbrot “SET” To begin with a set is a collection of things. In this case the Mandelbrot set is formed by all the complex numbers that can be iterated an infinite amount of times and will never go towards infinity. In other words there is a attractor that is making sure it stays inside the set.

To test for this, iterations are performed and if the absolute value of the complex number exceeds 2 than we know it will go towards infinity for sure, just take that for truth that it is 2. The absolute value of a complex number is a bit hard to calculate but it turns out that the .NET framework has a handy class (System.Numerics.Complex) to do this for us.

Displaying the Mandelbrot set

To display the Mandelbrot set each pixel on the screen has to correspond to a complex number to test if it is in the set. If it is not in the set the pixel will be white, if it is black.

Figure 7 - Black/white mandelbrot set with complex plane axis

To get nice colors the number of iterations it took for the point to go to infinity can be used. The more iterations the brighter the pixel for example.

Figure 8 - Colored mandelbrot set in my program, download it! =]

Downloads

Above is a screenshot of the mandelbrot viewer i made, clicking on a point lets you zoom in. Below you can find an executable and the source code, enjoy!

Links