Approximating the Double Integral using Monte Carlo Method

In summary, the Monte Carlo method approximates the double integral $\displaystyle\iint\limits_R e^{xy}dA$ where $R = [-1,1] \times [0, x^2]$.
  • #1
WMDhamnekar
MHB
381
28
Write a program that uses the Monte Carlo method to approximate the double integral $\displaystyle\iint\limits_R e^{xy}dA$ where $R = [-1,1] \times [0, x^2]$. Show the program output for N = 10, 100, 1000, 10000, 100000 and 1000000 random points.

My correct answer:

My Java program:

Java:
//Program to approximate the double integral of f(x,y)=e^xy over the
//region bounded by x=-1, x=1, y=0, and y=x^2
public class montecarlo6 {
public static void main(String[] args) {
//Get the number N of random points as a command-line parameter
int N = Integer.parseInt(args[0]);
double x = 0; //x-coordinate of a random point
double y = 0; //y-coordinate of a random point
double f = 0.0; //Value of f at a random point
double mf = 0.0; //Mean of the values of f
double mf2 = 0.0; //Mean of the values of f^2
for (int i=0;i<N;i++) { //Get the random coordinates
x = 1 * Math.random() ; //x is between 1 and 0
y = 1 * Math.random() ; //y is between 0 and 1
if (y < Math.pow(x,2)) { //the point is in the region
f = Math.exp(x*y); // Value of the function
mf = mf + f; //Add to the sum of the f values
mf2 = mf2 + f*f; //Add to the sum of the f^2 values
}
x =  -1 * Math.random() ; //x is between -1 and 0
y = Math.random() ; //y is between 0 and 1
if (y < Math.pow(x,2)) { //the point is in the region
f = Math.exp(x*y); // Value of the function
mf = mf + f; //Add to the sum of the f values
mf2 = mf2 + f*f; //Add to the sum of the f^2 values
}
}
mf = mf/N; //Compute the mean of the f values
mf2 = mf2/N; //Compute the mean of the f^2 values
System.out.println("N = " + N + ": integral = " + vol()*mf +
" +/- " + vol()*Math.sqrt((mf2 - Math.pow(mf,2))/N));
}
//The volume of the rectangle [-1,1]x[0,1]
public static double vol() {
return 1*1;
}
}

The above program's output:

1653239554005.png
 

Attachments

  • 1653139122846.png
    1653139122846.png
    11.4 KB · Views: 112
Last edited:
Technology news on Phys.org
  • #2
N = 10: integral = 0.5232 +/- 0.3242N = 100: integral = 0.4741 +/- 0.1645N = 1000: integral = 0.5111 +/- 0.0805N = 10000: integral = 0.5071 +/- 0.0402N = 100000: integral = 0.5059 +/- 0.0199N = 1000000: integral = 0.5069 +/- 0.0099
 

FAQ: Approximating the Double Integral using Monte Carlo Method

What is the Monte Carlo Method?

The Monte Carlo Method is a statistical technique used to approximate the value of a mathematical function by random sampling. It is based on the concept of repeatedly performing random experiments and using the results to estimate the desired value.

How does the Monte Carlo Method work in approximating double integrals?

In approximating double integrals, the Monte Carlo Method involves randomly selecting points within the integration region and using them to calculate the average value of the function. This average is then multiplied by the area of the integration region to obtain an approximation of the double integral.

What are the advantages of using the Monte Carlo Method for approximating double integrals?

The Monte Carlo Method is advantageous because it is relatively simple to implement and can be used for integrands that are difficult or impossible to integrate analytically. It also allows for the calculation of error estimates, providing a measure of the accuracy of the approximation.

Are there any limitations to the Monte Carlo Method for approximating double integrals?

One limitation of the Monte Carlo Method is that it can be computationally expensive for high-dimensional integrals. Additionally, the accuracy of the approximation depends on the number of sample points used, so a large number of samples may be required for a precise estimate.

How can I improve the accuracy of my approximation using the Monte Carlo Method?

To improve the accuracy of the approximation, you can increase the number of sample points used or use more sophisticated sampling techniques, such as stratified or importance sampling. It is also important to choose an appropriate integration region and to check for convergence by comparing the results with known values or using error analysis methods.

Similar threads

Back
Top