## R — Barnsley’s Fern Fractal

Posted On: Tuesday, August 22, 2017 at 10:11 AM Last updated: Monday, August 6, 2018 at 5:24 AM

library(grid)

A=vector("list",4)

A[[1]]=matrix(c(0,0,0,0.18),nrow=2)

A[[2]]=matrix(c(0.85,-0.04,0.04,0.85),nrow=2)

A[[3]]=matrix(c(0.2,0.23,-0.26,0.22),nrow=2)

A[[4]]=matrix(c(-0.15,0.36,0.28,0.24),nrow=2)

b=vector("list",4)

b[[1]]=matrix(c(0,0))

b[[2]]=matrix(c(0,1.6))

b[[3]]=matrix(c(0,1.6))

b[[4]]=matrix(c(0,0.54))

n <- 500000

x <- numeric(n)

y <- numeric(n)

x[1] <- y[1] <- 0

for (i in 1:(n-1)) {

trans <- sample(1:4, prob=c(.02, .9, .09, .08), size=1)

xy <- A[[trans]]%*%c(x[i],y[i]) + b[[trans]]

x[i+1] <- xy[1]

y[i+1] <- xy[2]

}

plot(y,x,col="pink",cex=0.1)