# Little useless-useful R functions – Mandelbrot set

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**R – TomazTsql**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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!

To

**leave a comment**for the author, please follow the link and comment on their blog:**R – TomazTsql**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.