私は最近 R でコーディングを開始し、Mandelbrot フラクタルを描画するこのコードに出くわしました。
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)
私はいくつかのテストを行い、結果を見ました。画像の解像度が低すぎることがわかったので、このコードを試して解像度を改善しました。基本的に、関数を 2 回計算します (つまりf(1)
、f(1.5)
の代わりに、f(2)
) 。f(2.5)
f(1)
f(2)
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)
数値の2倍の量を計算しますが、の解像度とMandelbrot.gif
寸法(1200x1200)は同じようです。