GNUPLOT

- not so Frequently Asked Questions -

update 2004/11/29

Mandelbrot Set

Suppose we have a complex value A, and we calculate the following recurrence relation. If |z(n)|^2 does not diverge when n becomes very large numbers, the complex value A belongs to the Mandelbrot set.

z(0)   = 0.0
z(n+1) = z(n)*z(n) + A

We regard the XY plane as the complex plane, and the point A is defined on it. Calculate the recurrence relation above, and if the |z|^2 value begins to diverge at a certain number of iteration n, we give this number n as the Z value at (X,Y) point. You can draw a 3-dim plot of the Mandelbrot set with those (X,Y,Z) data. Firstly we define this recurrence relation as a recursive function. This function returns the number of iteration n when |z|^2 becomes larger than a certain real number (2, in this case). The upper limit of iteration is 1000, which we need to avoid an infinite-loop.

In gnuplot a complex variable z whose real part is a and imaginary is b is written by z={a,b}. Functions real(z) and imag(z) return the real and imaginary part, respectively. The absolute value of a complex z is abs(z). On the other hand there is no easy way to make a complex value from a real value, we define a new function, complex(x,y)=x*{1,0}+y*{0,1}.

complex(x,y) = x*{1,0}+y*{0,1}
mandel(x,y,z,n) = (abs(z)>2.0 || n>=1000) ? \
n : mandel(x,y,z*z+complex(x,y),n+1)

The coordinates, x,y, are the location of the value A on the complex plane. The function mandel calculates z^2+A, and when its absolute value exceeds 2.0, the function returns the number of iteration, n. When you call this function you have to give initial values of z and n which are {0,0} and 0, in addition to the values of x and y.

gnuplot> set xrange [-1.5:0.5]
gnuplot> set yrange [-1:1]
gnuplot> set logscale z
gnuplot> set isosample 50
gnuplot> set hidden3d
gnuplot> set contour
gnuplot> splot mandel(x,y,{0,0},0) notitle
mandel1
mandelbrot0

You may often see a nice computer graphics of the Mandelbrot set. To make such an image on your computer, each pixel of the image is alloted to a certain point on the complex plane, and calculate the recurrence relation for the pixel. When the calculation diverged, put a color-point at the pixel, and the color is changed by the number of iteration. Here we drew a 2-dim. Mandelbrot image with a simple X Window program. The plateau in the graph above corresponds to the black part in the right image.


mandel1

As you can see in the left image, a structure of the magnified Mandelbrot set is very complicated. We showed those fractal images by using very fine contour lines, however, a line-art like gnuplot makes is not so good for playing with computer graphics. A new version of gnuplot, version 3.8, can draw nicer color-3D graph.


up

Here are some CGs made on the X Window system with my toy program.

mandelbrot1 mandelbrot2

mandelbrot3 mandelbrot4
up