Reputation: 1342
I recently began coding in R and stumbled upon this code, which draws a Mandelbrot fractal:
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=100)
I made a few tests and looked at the result. I found out the images had a too low resolution, so I tried this code to improve the resolution: essentially, it calculates the function twice the times (i.e. f(1)
, f(1.5)
, f(2)
, f(2.5)
instead of f(1)
, f(2)
), I think.
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*2)) # initialize output 3D array
for (n in 1:20) { # loop with 20 iterations
for (m in 1:2) { # Loop twice
k <- n+m/2 # Does the trick of adding .5
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)
Although it calculates twice the amount of numbers, the resolution of Mandelbrot.gif
seems to be the same, as well as the dimensions (1200x1200).
Upvotes: 1
Views: 222
Reputation: 132706
The resolution is increased by simply increasing the value of m
. However, the number of elements in X
is m^2*20, which can easily become larger than 2^31-1, which is the current limit of vector length (an array is a vector with additional attributes) in R. Either you find a different function to create the animated GIF, that does not need to save all information in one array, or you wait for the next major update of R, which will increase the object size limit afaik.
The following code allows higher resolutions, but beware, it needs a lot of CPU time and you will run out of memory quickly. You also need ImageMagick.
library(animation) # external package providing saveGIF function
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F",
"yellow", "#FF7F00", "red", "#7F0000"))
m <- 200 # 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
saveGIF(
for (k in 1:20) { # loop with 20 iterations
Z <- Z^2+C # the central difference equation
image(exp(-abs(Z)),col=jet.colors(255)) # plot results
},"Mandelbrot2.gif"
)
Upvotes: 1