Plotting fractals in the complex plane

Phys291 Project Spring 2020

Tarje Hillersoey - thi007


My project is to plot the most familiar fractals you can construct in the complex plane, being the Mandelbrot set and some Julia sets for quadratic polynomials. These are iterative functions of a complex variable, z. To perform the task I used root and C++ code to create the functions and then I plotted them using both TCanvas and a Portable Pixel Format (ppm). The latter has better visuals and serve as an artistic counterpart to the more analytical TCanvas structure.


A complex number z is a doublet of two numbers and can be written as z=a+bi for two real numbers a and b with i defined as the square root of -1. The set of all complex numbers form the two-dimentional complex plane. A function f whose input and output is a complex number is called a function of a complex variable. We will restrict ourself to functions whose input only depends on one parameter, being z. An iterative function is a function whose input depends on the previous value of the function. Such functions form a sequence such that the (n+1)'th iteration is dependent on the n'th iteration where n is a natural number. The relevant definitions for the project are the following

From the definitions, the key property is that if the absolute value of the complex number, |z|, is larger than 2, then the sequence will diverge. Thus, every member of the Mandelbrot set has to be confined within a disk of radius 2 centered at the origin. Therefore, we can limit ourselves to this region of the complex plane as other values need not to be considered. If we consider the Julia set of quadratic polynomials, then the subset of complex numbers c which converge under iterations will form the Mandelbrot set.

Implementing the code

The code for for the Mandelbrot set can be found here. To run it, simply save the code as "Mandelbrot" and then run it in root.
The code for the Julia set can be found here here . To run it, save it as "julia" and then run it in root.

The Julia set code is mostly recycled code from the Mandelbrot set code, the only changes are in the definition of the iterated function and adding a nonzero complex number c as an additional initial condition. To change the output of the Julia set code, simply change the value of c to some arbitraty complex number. However, to get some intresting fractal structure, the value of c needs to be a member of the Mandelbrotset. There are a lot of nice looking graphs emerging from different values of c in the Julia set. As the behaviour is chaotic, small changes in this initial condition alters the global structure completely.
The code is already commented with the most relevant information to what the code does, however brief. The first thing one notices when looking at such fractal patterns is the colors of the graph. I have arranged it in such a way that for each pixel, the color of said pixel is related to how fast the that point diverges to infinity. If the function for a given pixel (complex number c), stays within the bounded region after a threshold number of iterations(100), then it is given a color value of 0. In root graphics this is white. A white region of the plane means that the complex number at that region is a member of the Mandelbrot set. The dark blue regions corresponds to subsets of the complex plane where the iterated points diverge rapidly. An overview of how fast points diverge is included in the Mandelbrot graph.

For the paramaters I set the width and height of the canvas to 1000x1000 and the number of iterations to 100. These parameters are of course chosen arbitratily, but they give a good resolution and the runtime is not that long (under a minute). The spatial resolution is good enough such that you can see the fractal pattern clearly. A higher resolution would not be useful as the marginal effect on the picture would not outweigh the runtime. The choice of coloring is also arbitrary, but useful as it is an easy way to get a nice image. One can change this however one likes depending on what sort of colored graph is desired. For example you can simply change the points which are in the set by returning a value other than zero.

In my code I include the "complex" class which makes life easier essentially. It knows complex numbers so that you don't have to write z out as a real plus an imaginary number. This simplifies calculations in a few places as for example you can just write abs(z) instead of the square root of a*a + b*b for z=a+bi.

To open the ppm formated pictures in Windows you need to install a program which can actually open the format. I used IrfanView 64 for this part. It is quick to download and does the job. I you only want the ppm generated pictures, I found an easy way was to just have the code in C++ and then just removing the root parts of the code. If you then just change from mandelbrot() /julia() to main(), it should do the job. Doing it this way, you can generate a lot of pictures in a short time.


The following set of images are the output of the programs linked in the previous section. To the left is the images generated in root by using TCanvas and the images to the right are the corresponding images created in the ppm format.

The last two rows of pictures cleary do not cover the same regions in the plane. I did it this way as in my opinion this illustrates the point of fractal geometry and fractal structures. As fractals are self-similar under scale translations one cannot say which of these two images is the "largest" one, that is, covers the largest region of the plane. For all you know, the right pictures may be zoomed in or out a million times relative to the left pictures.
The left columns of pictures, those generated with TCanvas serve as an analytical way of measuring divergences. It adds a much more qualitiative ascept to the complex dynamics going on as it has numbers describing it and statistical quantities. The pictures in the right column are artistic. They look more elegant, as they sacrifice information for beauty essentially. You still get the same fractal pattern, and for illustrative purposes they are in my opinion preferred.


The overall project turned out fine as I managed to do what I had in mind from the start. The first thing that comes to mind regarding improvements is the reusability of the Julia set plots. By that I mean that it should not be too difficult to create a small program which takes in input regarding the paramaters of the Julia set. That is, the initial condition, c and some parameters regarding the size of the canvas, the number of iterations and the number of pixels in the plane. As these were chosen arbitraty, a small program to simply change these before running would be nice, instead of having to go into the code and changing manually. Another feature could be a small input screen where you could type the name of the pictures generated and where to save them. One last feature could be to consider iterated functions which are not quadratic. That is, you could write whatever polynomial you want to iterate and then the program could run it and generate the corresponding images in TCanvas and ppm related to the divergence of said iteration.