Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use C++ Estimating PI The Monte Carlo method is used in modeling a wide-range of physical systems at the forefront of scientific research today.

Please use C++

Estimating PI

The Monte Carlo method is used in modeling a wide-range of physical systems at the forefront of scientific research today. Monte Carlo simulations are statistical models based on a series of random numbers. Let's consider the problem of estimating Pi by utilizing the Monte Carlo method.

Suppose you have a circle inscribed in a square (as in the figure). The experiment simply consists of throwing darts on this figure completely at random (meaning that every point on the dartboard has an equal chance of being hit by the dart). How can we use this experiment to estimate Pi? The answer lies in discovering the relationship between the geometry of the figure and the statistical outcome of throwing the darts. Let's first look at the geometry of the figure.

Let's assume the radius of the circle is R, then the Area of the circle = Pi * R^2 and the Area of the square = 4 * R^2. Now if we divide the area of the circle by the area of the square we get Pi / 4.

But, how do we estimate Pi by simulation? In the simulation, you keep throwing darts at random onto the dartboard. All of the darts fall within the square, but not all of them fall within the circle. Here's the key. If you throw darts completely at random, this experiment estimates the ratio of the area of the circle to the area of the square, by counting the number of darts within each area. Our study of the geometry tells us this ratio is Pi/4. So, now we can estimate Pi as

 Pi = 4 x (Number of Darts in Circle) / (Number of Darts in Square)

PLEASE FIX THE FOLLOWING CODE

// This program implements the Monte Carlo Method for estimating the value of PI.

#include

#include

#include

using namespace std;

// given the coordinates of a point (x,y) computes if the point is inside the circle

// centered at origin with radius r. Returns 'true' if it is inside, 'false' otherwise.

bool isInside(double x, double y, double r)

{

//find the circle diameter as r square

double circle_diameter = pow(r, 2);

//check if the value x square plus y square is less than circle diameter

//if yes then the point (x, y) is inside the circle.

//return true.

if (pow(x, 2) + pow(y, 2) < circle_diameter)

return true;

//else return false

return false;

}

// given s, the size of the side of a square that is centered at the origin,

// chooses a random coordinates inside the square, and calls isInside function

// to test if the point is inside the circle or not.

bool throwDart(int s)

{

double x, y;

// assign x and y to two random integers between -s/2 and s/2

// x=(-s/2)+(rand()%(int)((s/2)-(-s/2)+1));

// y=(-s/2)+(rand()%(int)((s/2)-(-s/2)+1));

x = (rand() % (s + 1)) - (s / 2);

y = (rand() % (s + 1)) - (s / 2);

// x=(-s/2)+(rand()%(int)((s/2)-(-s/2)+1));

//y=(-s/2)+(rand()%(int)((s/2)-(-s/2)+1));

//Call the isInside function and return its output.

bool isPointInside = isInside(x, y, s / 2.0);

return isPointInside;

}

int main()

{

srand(333);

int side; // this is the side of the square and is also our resolution.

int tries; // this is the number of tries.

//Ask the user for the size (integer) of a side of the square

cout<<"Enter the side of the square:"<

//Get the users input using cins

cin >> side;

//Ask the user for the number of tries using cout.

cout<<"Enter the number of tries:"<

//Get the users input using cin.

cin >> tries;

int inCount = 0; //counter to track number of throws that fall inside the circle

for (int i = 0; i < tries; ++i)

{

//throw a dart using throwDart method and increment the counter depending on its output.

if (throwDart(side) == true)

inCount++;

}

//Compute and display the estimated value of PI. Make sure you are not using integer division.

double PI = 4.0 * (static_cast (inCount) /(tries));

cout << PI<< endl;

return 0;

}

RESULT MUST BE:

Input

1000

1000000

Output

3.13454

Input

500

100

Output

3.16

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

Students also viewed these Databases questions

Question

She came to the office with a bruised swollen knee.

Answered: 1 week ago