The Monte Carlo Method for Approximating , Consider a circle. The equation of th
ID: 3634049 • Letter: T
Question
The Monte Carlo Method for Approximating , Consider a circle. The equation of the circle with center at the origin: x3 + y2 + r2. where r = the radius. Consider 1/4 of a circle. Imagine a dartboard behind the quarter of o circle. Throw darts at the board. If a dart lands within the shaded region - score it as a hit. Calculate the ratio: (number of hits)/(number of throws). If we throw the dart many times, this ratio can be approximated as: (# of hits)/(# of throws) -(Shaded area)/(Total area of square). The area of a circle is pi.r2. The area of a square of side r Is r2. (# of hits)/(# of throws) -(pi r2)/4r2) = pi/4. Therefore, pi = 4 (# of hits)/(# of throws) Write program to approximate pi. Let N=number of throws. H = number of hits. A hit occurs If x2 + y2 A miss occurs if x2 + y2 > 1 Use random number generator to generate a value for x and y. Use a DO loop to simulate N throws. Since the random number generates give a number between 0 and 1, the program simulates a point in the first quadrant of a circle with radius=1. Therefore, simulate the above analysis and show thatExplanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int genNumber();
main()
{double mypi,pi=3.14159265358979,diff;
int i,in,throws=10000;
in=0;
throws=throws*10;
for(i=0;i<throws;i++)
in+=genNumber();
mypi=4.0*((double)in/throws);
diff=((mypi-pi)/pi);
cout<<"Throws: "<<throws<<endl;
cout<<"Hits: "<<in<<endl;
cout<<"Estimated Pie: "<<mypi<<endl;
cout<<"Different from pi: "<<fabs(diff)<<endl;
system("pause");
return 0;
}
int genNumber()
{double x,y;
int in=0;
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
if(sqrt(x*x+y*y)<=1.)
in=1;
return in;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.