Dhamnekar Win,odd Posted May 21, 2022 Posted May 21, 2022 (edited) 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: 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 May 21, 2022 by Dhamnekar Win,odd
Endy0816 Posted May 22, 2022 Posted May 22, 2022 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. 1
Dhamnekar Win,odd Posted May 22, 2022 Author Posted May 22, 2022 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:
Endy0816 Posted May 22, 2022 Posted May 22, 2022 (edited) Glad I could help. Have to say your code is nicely done and well commented. Edited May 22, 2022 by Endy0816
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now