Basic Mandelbrot set in R

On a train home from Norwich at the end of a very long day…

The Mandelbrot set has been back in the news of late due to the
unveiling of the awesome “Mandelbulb” 3-D fractal:

So my programming exercise for tonight was to write a complete program
to compute and plot a basic old-style 2-D Mandelbrot set:

in fewer than 10 lines of code. It turns out to be quite doable in R,
exploiting the fact that R has built-in support for complex numbers
and can perform vectorised operations on complex matrices. Code and
image attached. I should point out that this vectorised approach has
limitations, but leads to code that executes reasonably quickly in
vectorised languages like R (and python, and Matlab,…). It’s
certainly not how I’d approach the problem using a language that
doesn’t have a huge penalty associated with explicit looping over
pixels (like C, or Java, or …). See the above wikipedia article for further details,
including pseudocode that is a lot longer than 10 lines! 😉



Published by


I am Professor of Stochastic Modelling within the School of Mathematics & Statistics at Newcastle University, UK. I am also a computational systems biologist.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s