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, 04.07.2019 18:10
The temperature of air decreases as it is compressed by an adiabatic compressor. a)- true b)- false
Answers: 2
question
Engineering, 04.07.2019 18:10
Different types of steels contain different elements that alter the characteristics of the steel. for each of the following elements, explain what the element does when alloyed with steel.
Answers: 2
question
Engineering, 04.07.2019 18:10
Draw the engineering stress-strain curve for (a) bcc; (b) fcc metals and mark important points.
Answers: 1
question
Engineering, 04.07.2019 18:10
A-mn has a cubic structure with a0 0.8931 nm and a density of 7.47 g/cm3. b-mn has a different cubic structure, with a0 0.6326 nm and a density of 7.26 g/cm3. the atomic weight of manganese is 54.938 g/mol and the atomic radius is 0.112 nm. determine the percent volume change that would occur if a-mn transforms to b-mn.
Answers: 2
You know the right answer?
C++ coding

the monte carlo method is used in modeling a wide-range of physical systems a...
Questions
question
Mathematics, 15.04.2020 03:44
question
Mathematics, 15.04.2020 03:44