[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Make an animated gif of a Mandelbrot Fractal pattern in R



In the R-help list, Jarek Tuszynski, PhD. (Jaroslaw.W.Tuszynski AT saic.com, Science Applications International Corporation) just posted this neat R code. Assuming you have "fields" and "caTools" packages installed, then this creates an animated gif output file. If you view in your web browser, the output file "Mandelbrot.gif" is truly amazing. My mail program was translating one part of this into an R mistake, and I expect yours will too. So I'm correcting that part and posting here. You should be able to just cut and paste this whole section into a running R and get the result. The R screen does not show the animated gif. It is necessary to view the output file in a web browser.



library(fields)  # for tim.colors
library(caTools) # for write.gif
m = 400          # grid 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)
Z = 0
X = array(0, c(m,m,20))
for (k in 1:20) {
  Z = Z * Z +C
  X[,,k] = exp(-abs(Z))
}
image(X[,,k], col=tim.colors(256)) # show final image in R
write.gif(X, "Mandelbrot.gif", col=tim.colors(256), delay=100)

pj

--
Paul E. Johnson                       email: pauljohn_AT_ku.edu
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
1541 Lilac Lane, Rm 504
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700