2-d monte carlo integration help
First, I'd like to thank everyone that willingly gives their time to help, thank you!!
*NOTE: This is for school, and I am NOT wanting someone to just do this for me or give answers. I just need some tutoring and help because I am completely lost and have made 0 to little progress (I'll post what have done at the end.)
Right now I'm trying to write a program that makes a rectangle, and inside the rectangle is a circle. Virtual darts are thrown via the rand function, and the area of the circle is calculated based on hit / misses. Here are some details:
-I need to compute the area of a shape (circle) by throwing virtual darts
-Produce a tester that tells if my code is working
At the end of the program the following should be printed out:
1. Length and width of enclosing rectangle
2. Area of enclosing rectangle
3. Number of darts thrown
4. How many darts hit the shape
5. Percentage of darts that hit the shape
6. Area of the shape computed by out dart throwing
7. Actual area of shape as known from an equation
8. The absolute value difference between these quantities (i.e., the error)
In the tester we must provide the function: void GetBorders (double *xmin, double *xmax, double *ymin, double *ymax);
I'm particularly confused here, am I suppose to make a GetBorders function, then call the function in the tester function?
Here's some pseudo code that was provided:
(1) Write function for shape we know answer to
--> circle has area PI*r*r
--> Easy to write function for circle wt center on axis!
--> Answer 1 if distance of (x,y) from axis <= r
--> distance: sqrt(x*x + y*y) (pythagorean theorem):
** draw grid wt triangle, to show how hypotenuse is the distance
--> RETURN 1 if sqrt(x*x + y*y) <= r, ELSE 0
==> Call this function:
int DartHits(double x, double y)
(2) Have known answer of area as formula
-> circle is PI*R*R
-> make PI and R cpp macros so we can vary easily
(3) Compare known area to area computed by throwing darts!
And for the other functon:
(1) Get how many darts to throw
--> N, read in from keyboard, store as unsigned long int
(2) Get coordinates of surrounding rectangle
--> xmin: x coordinate of left side of rectangle, which is known to be
less than smallest X value of function
--> xmax: x coordinate of right side of rectangle, >= largest X val of func
--> ymin,ymax: same for Y lower/upper of rectangle
==> types must be double, since points are continuous!
==> double xmin, xmax, ymin, ymax
==> These values must be supplied by tester, since they are tied to the
function being evaluated!
==> we will have tester supply all four values using function:
--> void GetBorders(double *xmin, double *xmax,
double *ymin, double *ymax);
(3) Compute area of rectangle:
--> Area of rect = length * width = (xmax - xmin) * (ymax - ymin)
(4) Loop over N darts thrown, and count hits
a. Generate random # between [xmin,xmax]; call this xdart (xcoord of dart)
-> call xdart = RandNumInRange(xmin, xmax);
b. Generate random # between [ymin,ymax]; call this ydart (ycoord of dart)
-> call ydart = RandNumInRange(ymin, ymax);
c. hit = DartHits(xdart, ydart);
e. if hit != 0, add one to # of hits
(5) Compute area as: rectArea*(percent hits) = rectArea*(nhits/N)
--> Make sure nhits/N computed as a double to avoid integer truncation!
** How do we get a number between xmin, xmax (more generally between w & z)
-> man -a rand brings up manpage
-> set seed using srand
-> rand(void) returns a number between [0, RAND_MAX]
-> Can change integer between [0,RAND_MAX] to real between [0,1]:
- interpret rand() and RAND_MAX as doubles, and simply divide them:
* rn = ((double)rand())/((double)RAND_MAX); /* real between [0,1] */
* make span any distance by multiplying by distance
- Distance is z-w (z known to be >= to w)
* Make start at particular value by adding starting value in
(will change ending value by same amount, so be prepared for that)
==> Can write func:
double RandNumInRange(double W, double Z)
And for what I have so far, I started the Throw Darts bit first, because I am understanding that part slightly more.
#define PI 3.14159
#define RADIUS 5
int main (void)
int n; /* How many darts to throw. */
printf(" Please enter how man virtual darts you would like to throw: \n\n ");
/* Need to call GetBorders function here*/ // Why are they pointers?
int rectArea = (xmax - xmin) * (ymax - ymin);
while ( 1 <= n ) //Started the loop, but I'm not sure what it actually is looping because the instructions seemed arbitrary for making this loop
I'm so lost at the moment I'm not even sure what else I should ask, but any advice / tips / tutoring is GREATLY appreciated. Again this is homework for me, please do not post answers. Thanks everyone!!!