Pages

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);
 }
}


Ayush Subedi

Coffee Connoisseur