## Example code from presentation: Ways of Seeing Julia Sets

I gave a presentation at yesterday’s DIT School of Mathematical Sciences lunchtime research seminar titled Ways of Seeing Julia Sets: Visualizing the forces that shape fractal Julia sets. This was the abstract:

Early in the 20th century, work by mathematicians such as Pierre Fatou and Gaston Julia on complex dynamics led to the definition of so-called Julia sets. When a rational complex polynomial function is applied iteratively to a complex number, z, it produces a sequence of complex values called the orbit of z. Depending on the particular function and on the value of z, that orbit may or may not remain bounded. For a given function, the Julia set forms the boundary between those regions of the complex plane where the orbits remain bounded and those where they do not.Intriguingly, even for quite simple iterative complex functions, Julia sets often take on very striking fractal shapes. With the aid of a computer, it is easy to visualize the Julia set of a function but, for most people, understanding why it takes on a fractal shape is difficult. In my own ongoing struggle to gain a more intuitive understanding of fractals, I have written many short computer programs to visualize them in different ways. In this presentation, I will explore some ways of visualizing Julia sets which I found helpful in understanding why they are fractal. My Prezi slides can be viewed here:
http://prezi.com/f5ymufi-bfqc/ways-of-seeing-julia-sets/

During my presentation, I demonstrated two short C programs that generated these fractal images.

This is the complete C code for the Mandelbrot example:

```#include <stdio.h>
#include <complex.h>

void main()
{
complex double z, c;
int x, y, i;

printf("P2\n4000 4000\n255\n");
for (y=0 ; y<4000 ; ++y)
{
for (x=0 ; x<4000 ; ++x)
{
z = 0;
c = (-2 + x*0.001) + (-2 + y*0.001)*I;
i = 0;
while (cabs(z) < 2 && ++i < 255) z = z*z + c;
printf("%4d", i);
}
printf("\n");
}
}
```

I compiled and ran the program as follows:

```gcc mandelbrot.c -o mandelbrot -lm
./mandelbrot > mandelbrot.pgm
```

The C code for the Julia set example is almost identical except that it’s now z rather than c which changes from pixel to pixel (i.e. with changing x and y coordinates) and c is constant throughout the image (the value of c you choose determines which Julia set you end up with).

```#include <stdio.h>
#include <complex.h>

void main()
{
complex double z, c;
int x, y, i;

printf("P2\n4000 4000\n255\n");
for (y=0 ; y<4000 ; ++y)
{
for (x=0 ; x<4000 ; ++x)
{
c = -0.625 - 0.4*I;
z = (-2 + x*0.001) + (-2 + y*0.001)*I;
i = 0;
while (cabs(z) < 2 && ++i < 255) z = z*z + c;
printf("%4d", i);
}
printf("\n");
}
}
```

I compiled and ran the program as follows:

```gcc julia.c -o julia -lm
./julia > julia.pgm
```
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.