Jump to content

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


Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

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.