The Mandelbrot Set is perhaps the most famous fractal of all time. It's simple in its definition: iterate the complex equation *z*_{n+1} = *z*_{n}^{2} + *c *(starting with *z*_{0} = 0) for various values of c, and if doesn't go to infinity then c is part of the Mandelbrot Set. The result, however, is amazingly complex. Thinking of *c* as defining a 2-dimensional area, the boundary of set (between where *z* does and does not zoom to infinity) is infinitely crenellated. And if you decorate the interior of the set with colors defined by the absolute value of *z* after N iterations, beautiful patterns emerge.

Because complex numbers are natural data types in the R language, it's easy to generate the Mandelbrot Set in just a few lines of code. And it's very quick, because each iteration is done over an entire grid of *c* values in a single statement. The code can be found in R's Wikipedia page (and reproduced after the jump). You've probably seen the Mandelbrot set before, but you may never have seen how it evolves from one iteration to the next. The R code below shows us by using the caTools package to creare an animated GIF.

Beautiful. (Update: here is a larger version.)

Wikipedia: R Programming Language, Example 2

library(caTools) # external package providing write.gif function jet.colors = colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000")) m = 600 # define size C = complex( real=rep(seq(-1.8,0.6, length.out=m), each=m ), imag=rep(seq(-1.2,1.2, length.out=m), m ) ) C = matrix(C,m,m) # reshape as square matrix of complex numbers Z = 0 # initialize Z to zero X = array(0, c(m,m,20)) # initialize output 3D array for (k in 1:20) { # loop with 20 iterations Z = Z^2+C # the central difference equation X[,,k] = exp(-abs(Z)) # capture results } write.gif(X, "Mandelbrot.gif", col=jet.colors, delay=100)

however, running the code you put here does not generate the beautiful gif like this on my PC, rather a quite ugly one. Did you copy this Mandelbrot plot from wiki or you actually create it with the R code you paste here?

Posted by: Jason | September 30, 2010 at 21:33

If you like to create fractals in R, maybe you'll find this post useful :-) https://www.marcoscan.com/2009/12/frattali-con-r.html

Posted by: Marcoscan | September 30, 2010 at 23:45

running the code on wikipedia doesnot produce the graph on wikipedia-i tried it too- there was a thread on this on r-help.

Peter Dalgaard suggested this as a solution-

image(X[,,20],col=jet.colors(100)) instead of CA tools

but the.gif is still bad.

read the nabble thread here https://r.789695.n4.nabble.com/why-I-could-not-reproduce-the-Mandelbrot-plot-demonstrated-on-R-wiki-td2591429.html

Posted by: Ajay | October 01, 2010 at 02:38

Worked fine for me using R 2.11.1 on a Mac, so maybe the caTools is platform-specific.

Incidentally, I re-ran the code to double the resolution of the GIF. Took less than 10 seconds to produce all 20 iterations and write the animated GIF. You can download the large version here (warning, it's about 6Mb).

Posted by: David Smith | October 01, 2010 at 08:44