Skip to main content

MandelbrotArea



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] 1.5168

Javascript (DIY):

Estimate the area of the Mandelbrot set here.

MonteCarlo Repetitions :
Dwell Limit :


Estimated Area:

Popular posts from this blog

SalsaNight

Zorgania

The Zorganian Republic has some very strange customs. Couples only wish to have female children as only females can inherit the family's wealth, so if they have a male child they keep having more children until they have a girl. If they have a girl, they stop having children. What is the ratio of girls to boys in Zorgania?
The ratio of girls to boys in Zorgania is 1:1. This might be a little counter-intuitive at first. Here are some ways of tackling this problem. 1. Monte Carlo Simulation: Although, Monte Carlo simulation does not necessarily show why the result is 1:1, it is appropriate because of the very counter-intuitive nature of the problem. At the very least, it helps us see that the result is indeed 1:1. Therefore, this is a good start.
The following R code estimates the probability of a child being a boy in Zorgania. 
couples <-100000 boycount <-0for (i in1:couples){ # 0: boywhile (sample(c(0,1),1) ==0) { boycount=boycount+1 } } probability <- boycount/(co…

Simple Launcher

A simple minimal launcher application for Android devices that shows battery percentage using lzyzsd's CirclProgress library (ArchProgress used in this case) and BroadcastReciever for battery state, Android's clock widgets, a built-in flash light switch and an app list view that can be toggled. Currently, the toggle simply filters all the app that I am working on at present. Future implementation can allow users to select their favorite apps or populate second toggle based on the most used applications.