Making Simple Fractals in R

In my GHP Fractals class this summer, I opted to write my sample programs in R. I selected R because it has very nice graphing libraries and built-in complex numbers. Overall, I was pleased with R; it definitely let me built some incredibly straightforward fractal code. It’s not as fast or pretty as other choices might have been, but it highlighted the underlying math which was most important to me.

King's Dream Strange Attractor (parameters from Chaos In Wonderland)


You can see an example on the left, rendered by R. The source of this particular fractal comes from Clifford Pickover’s Chaos in Wonderland. The graph is made by repeatedly iterating a simple function of x and y based on sine and cosine.

You can see the code in R here:

  a <- -0.966918
  b <- 2.879879
  c <- .765145
  d <- .744728
  pointsToPlot <- 100000
  x <- .1
  y <- .1
  allXs = rep(0,pointsToPlot)
  allYs = rep(0,pointsToPlot)
  for(i in 1:pointsToPlot) {
    newx <- sin(y*b) + c*sin(x*b)
    newy <- sin(x*a) + d*sin(y*a)
    x <- newx
    y <- newy
    allXs[i] <- x
    allYs[i] <- y
  plot(allXs, allYs, pch=".", col="red",
       main="Strange Attractor: King's Dream")

Here’s a few more pretty pictures:


  1. affine.R (4 KiB)

  2. circleInvPlay.R (134 B)

  3. complexPlay.R (2.1 KiB)
    Includes the julia sets

  4. henon.R (565 B)

  5. mandel_explore.R (543 B)

  6. mobius.R (3.6 KiB)

  7. rPickover.R (1.1 KiB)

Leave a Reply