Jump to content

How to debug the Java program that uses the Monte Carlo method to approximate the double integral?

Featured Replies

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

 

My attempt:

//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 montecarlo5 {
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 = Math.random() ; //x is between 0 and 1
x = -1 * Math.random() ; //x is between -1 and 0
y = Math.random(); //y is between 0 and 1
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 2*1;
}
}

 Program Output is as follows:

image.png.a91619e2a5965d4a29991c220f32fb2a.png

Correct integral =0.705; My Java program showed a integral 1.596 which is wrong, so my program is also wrong. How do I debug this program? 

 

Edited by Dhamnekar Win,odd

Not sure if the problem, but noticing that the value of x is being immediately overwritten without being used.

 

Will try and look at it when back at home.

  • Author

Yes.  I think you are correct because as per your suggestion, I modified my Java program and  I got the correct answer as given by author i-e 0.705.

The modified java program and its new output is given below:

//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 new program's output:

 image.png.c415f535be99c1676e85a60606dc791b.png

Glad I could help. :)

Have to say your code is nicely done and well commented.

Edited by Endy0816

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.