Mathematics

Mandelbrot Set

Years ago when I first came across ‘Chaos Theory’, I remember writing a program to plot the Mandelbrot set. That program was lost so I decided to re-write it and post it here for future reference. Here’s how the plot looks:

Mandelbrot Set

R code:


library(ggplot2)

mandelbrot_set <- function(precision) {
  is_mandelbrot_point <- function(a, b) {
    orig_a <- a
    orig_b <- b
    n_iters <- 30 # defines escape propensity
    for (i in seq(n_iters)) {
      x <- a
      y <- b
      a <- (x^2 - y^2) + orig_a
      b <- (2*x*y) + orig_b
    }
    # If the number escapes to infinity, it's not a mandelbrot point.
    if (is.nan(a) || is.nan(b))
      F
    else
      T
  }

  n <- 3 # axes restricted to -3 to +3
  x <- NULL
  y <- NULL
  for (i in seq(-n, n, precision))
    for (j in seq(-n, n, precision)) {
      flag <- is_mandelbrot_point(i, j)
      if (flag) {
        x <- c(x, i)
        y <- c(y, j)
      }
    }
  data.frame(x, y)
}

mdat <- mandelbrot_set(.01)
ggplot(mdat, aes(x, y)) + geom_point(color="brown", size=.1) +
  theme(axis.title=element_blank(), axis.text=element_blank(),
  axis.ticks=element_blank())

Discussion

Comments disabled.