Monte Carlo Problem: Write a Java program with the following.... The \"Monte Car
ID: 3861148 • Letter: M
Question
Monte Carlo Problem: Write a Java program with the following....
The "Monte Carlo Method" is a method of solving problems using statistics. Given the probability, P, that an event will occur in certain conditions, a computer can be used to generate those conditions repeatedly. The number of times the event occurs divided by the number of times the conditions are generated should be approximately equal to P.
How this program works:
If a circle of radius R is inscribed inside a square with side length 2R, then the area of the circle will be pi*R^2 and the area of the square will be (2R)^2. So the ratio of the area of the circle to the area of the square will be pi/4.
This means that, if you pick N points at random inside the square, approximately N*pi/4 of those points should fall inside the circle.
This program picks points at random inside the square. It then checks to see if the point is inside the circle (it knows it's inside the circle if x^2 + y^2 < R^2, where x and y are the coordinates of the point and R is the radius of the circle). The program keeps track of how many points it's picked so far (N) and how many of those points fell inside the circle (M).
Pi is then approximated as follows:
4*M
pi = -----
N
Although the Monte Carlo Method is often useful for solving problems in physics and mathematics which cannot be solved by analytical means, it is a rather slow method of calculating pi. To calculate each significant digit there will have to be about 10 times as many trials as to calculate the preceding significant digit. Calculate pi correct to four decimal places.
Explanation / Answer
Answer: See the code below:
-----------------------------------------------------
package montecarlodemo;
import java.util.Random;
import java.util.Scanner;
/**
* MonteCarloDemo class
*
*/
public class MonteCarloDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int R; //radius of circle inscribed in square
int N=0; //number of points picked inside square
int M=0; //number of points out of N lying inside circle
final int maxTrials=100000;
System.out.println("Enter the value of R:");
Scanner in = new Scanner(System.in); //scanner to read from standard input
R=in.nextInt(); //read radius R of circle
int S=2*R; //side of square
//float areaOfSquare=(float) Math.pow(S, 2); //area of square
System.out.println("Radius:"+R);
//System.out.println("Area of square:"+areaOfSquare);
Random random = new Random(); //random number generator
int i=1;
double pi=0.0; //pi to estimate
//loop to perform trials
while(i<=maxTrials)
{
int x=random.nextInt(S)-S/2; //x coordinate
int y=random.nextInt(S)-S/2; //y coordinate
N++; //increases N by 1
//System.out.println("x:"+x+",y:"+y);
double expr1=Math.pow(x, 2)+Math.pow(y, 2); //expression 1 to compare
double expr2=Math.pow(R, 2); //expression 2 to compare
if(expr1<expr2) //check whether estimated point lies inside the circle. It true, it lies.
{
M++; //increases M by 1 if it lies inside circle.
}
//System.out.println("N:"+N+",M:"+M);
i++;
}
//estimation of pi
pi=4.0*M/N;
System.out.println("Estimated value of pi:"+String.format("%.4f",pi));
in.close();
}
}
-------------------------------------------
Output:
------------------------------------------
Enter the value of R:
97
Radius:97
Estimated value of pi:3.1434
--------------------------------------------