Mandelbrot set in R

Click to animate the gif above

The Mandelbrot set is always fun

Here are the first 20 iterations of equation z = z² + c plotted for different complex constants c.

Depending on your monitor resolution you may see some interesting Moire patterns in the design.

I found some R code for generating the Mandelbrot set. So I had to try it out. Here is the code.

library(caTools) # external package providing write.gif function
jet.colors < - colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000")) m <- 1200 # 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=1000)

To do this yourself you may need to install the package caTools.


After making the gif you can resize it and change the speed over at online for free.

There are more examples on theWikipedia page for R.

Here is a larger version a 1200 x 1200 Mandelbrot gif download that has a much slower image change speed (10 seconds).

Comments are closed.


A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.