The area of the Mandelbrot Set

The Mandelbrot set is a fractal (illustrates self-similarity). The set is obtained from the quadratic recurrence equation, with , where points in the complex plane for which the orbit of does not tend to infinity are in the set. The Mandelbrot set is a compact set, contained in the closed disk of radius 2 around the origin. Since it is contained in a disk of known area, it is possible to approximate the area of the Mandelbrot Set's using Monte Carlo method.

Java:

Since Java does not inherently understand complex numbers, a "real" approach will be applied to perform the quadratic recurrence equation,

First, as shown in the figure above, inscribe the disk in a square of length 4 units. Let represent the coordinate along x-axis (real) and represent the coordinate along y axis. Now set and , where and are randomly generated real numbers from [-2, 2]. Basically, the and coordinates are being duplicated at this step to preserve the point . Next, iteratively compute the following from (for programming purposes, choose a large Dwell Limit). Simultaneously, check if . If yes, increase count (not in the set) by 1 and get out of the loop (since all points should be contained in the disk).
Compute the ratio of number of points that are in the set to total number of points used. Then multiply the area of square (16 units square) to get the approximate are of the Mandelbrot set.

Java Code:

import java.util.Date;
import java.util.Random;

public class MandelbrotArea {

public static int mcRep = 5000;
public static int dwellLimit = 2048;

/**
* @return random double in [-2,2]
*/
public static double random() {
return (new Random().nextDouble() * 4) - 2;
}

/**
* @param r: real part of the complex number
* @param s: imaginary part of the complex number
* @return
*/
public static boolean isMandelbrotSet(double r, double s) {

double a = r, b = s, temp;

// Iterative function
for (int j = 1; j <= dwellLimit; j++) {
temp = a;
a = Math.pow(a, 2) - Math.pow(b, 2) + r;
b = (2 * temp * b) + s;

if (Math.pow(a, 2) + Math.pow(b, 2) > 4) {
return false;
}
}
return true;
}

public static void main(String[] args) {
long startTime = new Date().getTime();
long count = 0;
for (int i = 0; i <= mcRep; i++) {
if (isMandelbrotSet(random(), random())) {
count++;
}
}
System.out.println("Input -> DwellLimit: " + dwellLimit + ", McRep: " + mcRep);
System.out.println("Area: " + ((double) (count * 16)) / mcRep);
System.out.println("Execution time: " + (new Date().getTime() - startTime) + " ms");
}
}


Result:

Input -> DwellLimit: 2048, McRep: 5000
Area: 1.5136
Execution time: 389 ms



R

monte.Carlo <- 5000
x <- runif(monte.Carlo, -2, 2)
y <- runif(monte.Carlo, -2, 2)
list <- numeric(monte.Carlo)

for (j in 1:monte.Carlo){
list[j] <- if (inmandelbrotset(complex(real = x[j], imaginary = y[j]))) 1 else 0
}
area<-mean(list)*16


# function that checks if a point E mandelbrot set
inmandelbrotset <- function(c)
{
dwell.limit <- 2048
z <- 0
for (i in 1:dwell.limit)
{
z <- z ** 2 + c
if (Mod(z) > 2)
{
return(FALSE)
}
}
return(TRUE)
}


Result:

> system.time(source("mandelbrot.r"))
user  system elapsed
1.91    0.00    1.90
> area
 1.5168


Javascript (DIY):

Estimate the area of the Mandelbrot set here.

 MonteCarlo Repetitions : Dwell Limit : Estimated Area: