Engineering
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;
}

answer
Answers: 1

Other questions on the subject: Engineering

image
Engineering, 04.07.2019 18:20, luisgonz5050
Find the kinematic pressure of 160kpa. for air, r-287 j/ kg k. and hair al viscosity of air at a temperature of 50°c and an absolute (10 points) (b) find the dynamic viscosity of air at 110 °c. sutherland constant for air is 111k
Answers: 3
image
Engineering, 04.07.2019 18:20, cristykianpour
Describe one experiment in which the glass transition temperature and melting temperature of a totally amorphous thermoplastic material can be determined. show the relevant experimental results in a diagram which should be properly annotated with the two temperatures clearly marked. what is likely to happen to the curve in the diagram if the amorphous polymer is replaced by a thermosetting type?
Answers: 2
image
Engineering, 04.07.2019 18:20, rbgrh9465
An open feedwater heater operates at steady state with liquid entering at inlet 1 with t? = 40°c and pl = 1 .2 mpa. water vapor att2-200°c and p2 = 1.2 mpa enters at inlet 2. saturated liquid water exits with a pressure of pa 1.2 mpa. neglect heat transfer with the surroundings and all kinetic and potential energy effects, determine the mass flow rate of steam at inlet 2 if the mass flow rate of liquid water at inlet 1 is given as 2 kg/s.
Answers: 3
image
Engineering, 04.07.2019 18:20, rhussein6452
Wiy doeres rere okhn a pump whon working betwon the same pressure range?
Answers: 2
Do you know the correct answer?
C++ coding

the monte carlo method is used in modeling a wide-range of physical systems a...

Questions in other subjects:

Konu
Social Studies, 16.10.2019 22:30