subject
Engineering, 16.01.2020 00:31 jchay02

C++ coding

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)
follow this template:

// 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 or on the circle
// centered at origin with radius r. returns 'true' if it is inside or on the circle, 'false' otherwise.
bool isinside(double x, double y, double r)
{
if(sqrt(pow(x,2)+pow(y,2))< =r)

return true;

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)
{
int x, y;
// assign x and y to two random integers between -s/2 and s/2
x=(-s/2)+(rand()%(//2)+1));

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

if(isinside(x, y,s/2)){

return true;
}
return false;
//call the isinside function and return its output.
//you do not have to cast x & y to doubles, it is done automatically.

}

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 cin
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;

double 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))

incount++;
}

//compute and display the estimated value of pi. make sure you are not using integer division.
double pi=4*(incount)/(tries-incount);

cout< < "estimated pi value: "< < pi <
return 0;
}

ansver
Answers: 1

Another question on Engineering

question
Engineering, 03.07.2019 14:10
Line joining liquid phase with liquid and solid phase mixture is known as: a) liquidus b) solidus c) tie line d) none of the mentioned
Answers: 2
question
Engineering, 04.07.2019 18:10
Aflywheel accelerates for 5 seconds at 2 rad/s2 from a speed of 20 rpm. determine the total number of revolutions of the flywheel during the period of its acceleration. a.5.65 b.8.43 c. 723 d.6.86
Answers: 2
question
Engineering, 04.07.2019 18:20
Acertain flow of air (at stp) has a velocity distribution given by v i (in ft/s). if this flow is going through a 4 ft square area in the yz-plane (centered at the origin), what is the mass flow rate (in lbm/s)?
Answers: 2
question
Engineering, 04.07.2019 18:20
Air flows over a heated plate ร t a velocity of 50m/s. the local skin factor coefficient at a point on a plate is 0.004. estimate the local heat transfer coefficient at this point.the following property data for air are given: density = 0.88kg/m3 , viscosity 2.286 x 10 ^-5 kgm/s , k = 0.035w/mk ,cp = 1.001kj/kgk. use colburn reynolds analogy.
Answers: 1
You know the right answer?
C++ coding

the monte carlo method is used in modeling a wide-range of physical systems a...
Questions
question
English, 14.04.2020 22:53
Questions on the website: 13722360