MAE 9: Quiz #3, Fall 2010 Due on Wednesday, November 3rd by 21:

MAE 9: Quiz #3, Fall 2010 Due on Wednesday, November 3rd by 21:. MAE 9: Quiz #3, Fall 2010Due on Wednesday, November 3rd by 21:00pm(Turnin only on Wednesday) In this quiz you will compute a two-dimensional integral by using a simplistic version of the Monte Carlo Method. This method relies on random numbers to evaluate integrals and is the method of choice for numerical integration on higher dimensions (consult your favorite Wall Street investment bank). We want to integrate the area of a circle in the plane. The circle is centered at (xc, yc) and has radius r. We will limit our search to the box delimited by the points (0,0) and (4,4). Technically we will compute the area of the intersection of the circle with this box. Consider the function:/ 1, if distance between (x,y) and (xc,yc) is less or equal than rif distance between (x,y) and (xc,yc) is greater than r The area of the intersection of the circle with the box is the double integral: area = int_{0}^{4} int_{0}^{4} ind(x, y) dx dy Our simple Monte Carlo method will compute an approximation to the above integral by drawing N uniformly distributed random points in the box (xi, yi) and computing the sum area = (16/N) * sum_{i = 1}^{N} ind(xi, yi) Write a C program (quiz3.c) that: a) Reads the double values ofxc, yc and r (circle parameters)and the integer value ofN (number of points)from the terminal by using scanf; b) If N <= 0 set N = 1000 and issue a warning message to the user; c) If r <= 0 set r = 1 and issue a warning message to the user; d) Use a for loop to generate N uniformly distributed random pointsin the box (0,0)-(4,4) and compute the approximate integral asdiscussed above; e) Print the result of the approximate integral. HINTS: The function rand() generates a uniformly distributed pseudo-random integer between 0 and RAND_MAX. Use this function and RAND_MAX to generate double points (xi, yi) in the box (0,0)-(4,4). You will need to include the header <stdlib.h> in order to use rand(). For example, the statement 10 * rand() / RAND_MAX will generate a floating point between 0 and 10 (do not forget to properly cast integers as doubles or all you will get will be zero!) The function rand() will always draw from the exact same random sequence every time you run your program (it is not a true random number, but a pseudo-random number). In order to add some “spice” to this sequence you may want to initialize rand() by calling the function srand ( time(NULL) ); also provided in <stdlib.h>. The above initialization will use the current time to set the random generator and you will get a different answer every time you run your program. You must include the header <time.h> in order to use time.

MAE 9: Quiz #3, Fall 2010 Due on Wednesday, November 3rd by 21: