Skip to main content

MCIntegration


The Monte Carlo method evaluates or predicts the aggregate of a system by using a large number of random samples. This method can be applied in many areas of mathematics such as in calculus, statistics, finance, simulations, etc. When compared to the standard numerical methods, there are several advantages and disadvantages associated with the Monte Carlo method.




Java

public class Areaunderthecurve {

 public static double function(double rand) {

  double function;
  function = 4 * rand - rand * rand; // f(x)=4x-x^2
  return function;
 }

 public static void main(String[] args) {

  double minvalue = 1; // a = 1
  double maxvalue = 3; // b = 3
  int N = 1000000;
  double randomnumber, total = 0, Average, Integration;
  for (int i = 1; i <= N; i++) {
   randomnumber = Math.random() * (maxvalue - minvalue) + minvalue;
   total = total + function(randomnumber);
  }
  Average = total / N;
  Integration = (maxvalue - minvalue) * Average;

  System.out.println("Integral of the function (Area under the curve) : "
    + Integration);

 }
}





public class Volumeunderthesurface {

 public static double function(double randX, double randY) {

  double function;
  function = randX * randY;
  return function;
 }

 public static void main(String[] args) {

  double minvalueX = 0, maxvalueX = 3;
  double minvalueY = 1, maxvalueY = 3;
  int N = 1000000;
  double randomnumberX, randomnumberY, total = 0;
  for (int i = 1; i <= N; i++) {
   randomnumberX = Math.random() * (maxvalueX - minvalueX) + minvalueX;
   randomnumberY = Math.random() * (maxvalueY - minvalueY) + minvalueY;
   total = total + function(randomnumberX, randomnumberY);

  }
  double Average = total / N;
  double Integration = (maxvalueX - minvalueX) * (maxvalueY - minvalueY)
    * Average;

  System.out.println("Integral of the function (Volume under the surface) : "+ Integration);

 }

}






public class areaunderthecurve {
 public static double f(double x) {
  double function = 0;
  function = Math.pow(x, (.50));
  return (function);
 }

 public static void main(String[] args) {
  for (int a = 0; a <= 20; a++) {
   double N = Math.pow(2, a);
   double min = 1;
   double max = 5;

   // /////////////////////////////////////////////////////////////////////
   long TrapezoidalStart = System.nanoTime();
   double TrapezoidalArea = Trapezoidal(N, min, max);
   long TrapezoidalStop = System.nanoTime();

   long SimpsonStart = System.nanoTime();
   double SimpsonArea = Simpson(N, min, max);
   long SimpsonStop = System.nanoTime();

   long MonteCarloStart = System.nanoTime();
   double MonteCarloArea = MonteCarlo(N, min, max);
   long MonteCarloStop = System.nanoTime();

   // ////////////////////////////////////////////////////////////////////

   // ////////////////////////////////Printouts///////////////////////////

   System.out.println("2^" + a + " = N = " + N);
   System.out.println("____________________________________________");
   System.out.println("Monte Carlo approximation");
   System.out.println("Area : " + (MonteCarloArea));
   System.out.println("Execution Time : "+ (MonteCarloStop - MonteCarloStart));
   System.out.println();
   System.out.println();

   System.out.println("Trapezoidal Rule approximation");
   System.out.println("Area : " + TrapezoidalArea);
   System.out.println("Execution Time : "+ (TrapezoidalStop - TrapezoidalStart));
   System.out.println();
   System.out.println();

   System.out.println("Simpson's Rule approximation");
   System.out.println("Area : " + SimpsonArea);
   System.out.println("Execution Time : "+ (SimpsonStop - SimpsonStart));
   System.out.println();
   System.out.println();
  }
 }

 // //////////////////////////////////Trapezoidal Rule//////////////////////////////

 public static double Trapezoidal(double N, double a, double b) {
  double Integral;
  double deltax = (b - a) / N;

  double sum = 0;
  double minval = a;
  double maxval = b;

  sum = f(minval) + f(minval + deltax) + f(maxval - deltax) + f(maxval);
  a = a + deltax;
  for (double i = minval + deltax; i < maxval - deltax; i = i + deltax) {
   Integral = f(a) + f(b);
   sum = sum + Integral;
   b = a + deltax;
   a = b;
  }
  return (sum * deltax * .5);
 }

 // ///////////////////////////////////Simpson's Rule/////////////////////////////////

 public static double Simpson(double N, double a, double b) {
  double Integral;
  double deltax = (b - a) / N;
  boolean go = true;
  double sum = 0;
  double minval = a;
  double maxval = b;
  a = a + deltax;

  sum = f(minval) + f(maxval);

  for (double i = minval + deltax; i < maxval; i = i + deltax) {

   if (go) {
    // b = a + deltax;
    Integral = 4.0 * f(a); // instead of multiplying and adding
    go = false;
   } else {
    // b = a + deltax;
    Integral = 2.0 * f(b);
    go = true;
   }
   sum = sum + Integral;
   b = a + deltax;
   a = b;
  }
  return (sum * deltax * (.33333));
 }

 // /////////////////////////////////MonteCarlo Method////////////////////////////////

 public static double MonteCarlo(double N, double a, double b) {
  double randomnumber, total = 0;
  for (int i = 1; i <= (N + 1); i++) {
   randomnumber = Math.random() * (b - a) + a;
   total = total + f(randomnumber);
  }
  return (b - a) * total / (N + 1);
 }
}


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.