Little useless-useful R functions – Mandelbrot set
This article is originally published at https://tomaztsql.wordpress.com
The Mandelbrot set is a set of complex numbers c for which the function does not diverge to infinity when iterate from , and therefore remains bounded in absolute value.
For little stretching, we can create a Mandlebrot set and draw it with image function.
MandelBrotImage <- function(){
cols <- colorRampPalette(c("white","black","white","grey","black"))(11)
n <- 400
x <- seq(-2, 1, length.out=250)
y <- seq(-1.5, 1.5, length.out=250)
c <- outer(x,y*1i,"+")
z <- matrix(0.0, nrow=length(x), ncol=length(y))
k <- matrix(0.0, nrow=length(x), ncol=length(y))
for (rep in 1:n) {
for (i in 1:250) {
for (j in 1:250) {
if(Mod(z[i,j]) < 2 && k[i,j] < n) {
z[i,j] <- z[i,j]^2 + c[i,j]
k[i,j] <- k[i,j] + 1
}
}
}
}
image(x,y,k, col=cols, axes = FALSE, xlab = "" , ylab = "" )
}
# run function
MandelBrotImage()
As always, code is available on Github in Useless_R_function repository. The sample file in this repository is here (filename: MandelbrotSet.R) Check the repository for future updates.
Happy R-coding and stay healthy!
Thanks for visiting r-craft.org
This article is originally published at https://tomaztsql.wordpress.com
Please visit source website for post related comments.