The area of the Mandelbrot Set
The Mandelbrot set is a fractal (illustrates self-similarity). The set is obtained from the quadratic recurrence equation,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
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] 1.5168
Javascript (DIY):
Estimate the area of the Mandelbrot set here.