Dream to Learn is shutting down...

We are very sorry to say that Dream to Learn will be shutting down as of December 28th, 2019. If you have content that you wish to keep, you should make a copy of it before that date.


0COMMENTS0RECOMMENDS

Using R Programming Language to Generate Mandelbrot set and images

22
POSTED IN: Data Analytics & Visualization Blog

I was playing around with R tonight, and came across a tight little bit of code on the WIkipedia page for R, to generate a Mandelbrot set.  It is below.  I needed to lighten up a few of the loops and variables, (it threw an error at 250mb vector size initially, but I'm sure there is a memory setting somewhere to crank up if you're keen on the resolution.


## Source Code from http://en.wikipedia.org/wiki/R_(programming_language)  - second example (I modified a couple of the variables to not crash my old laptop)

library(caTools)         # external package providing write.gif function
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F",
                                 "yellow", "#FF7F00", "red", "#7F0000"))

setwd("C:/Users/Home/Documents/R/Mandelbrot")

#m <- 1200                # define size - MODIFIED TO LIGHTEN UP
m <- 800  # went smaller because 171mb vector size error-ed out

C <- complex( real=rep(seq(-1.8,0.6, length.out=m), each=m ),
              imag=rep(seq(-1.2,1.2, length.out=m), m ) )
C <- matrix(C,m,m)       # reshape as square matrix of complex numbers
Z <- 0                   # initialize Z to zero
X <- array(0, c(m,m,20)) # initialize output 3D array

#for (k in 1:20) {        # loop with 20 iterations  MODIFIED TO 10 - ERROR WENT AWAY
for (k in 1:10) {        # loop with  to see if smaller better - 10 ok; 15 too much
  Z <- Z^2+C             # the central difference equation
  X[,,k] <- exp(-abs(Z)) # capture results
}
write.gif(X, "Mandelbrot.gif", col=jet.colors, delay=1000)

## gif is multi-image, be patient if you just see a blob to begin

 

Background on the Mandelbrot Set

The Mandelbrot set is a mathematical set of points whose boundary is a distinctive and easily recognizable two-dimensional fractal shape. The set is closely related to Julia sets (which include similarly complex shapes), and is named after the mathematician Benoit Mandelbrot, who studied and popularized - Source: http://en.wikipedia.org/wiki/Mandelbrot_set 

  • The Mandelbrot set is defined as the set of all points c such that the above sequence does not escape to infinity.
  • the Mandelbrot set is just a set of complex numbers. A given complex number c either belongs to M or it does not. A picture of the Mandelbrot set can be made by colouring all the points c that belong to M black, and all other points white. The more colourful pictures usually seen are generated by colouring points not in the set according to how quickly or slowly the sequence |P_c^n(0)| diverges to infinity

Interested in more content by this author?

About the Author

Ryan Anderson

Ryan Anderson

Hi! I like to play with data, analytics and hack around with robots and gadgets in my garage. Lately I've been learning about machine learning.

About this blog

Description is...<br/>Data Analytics & Visualization Blog - Generating insights from Data since 2013

Created: July 25, 2014

Englishfrançais

Up Next