//================================================== file = integrate.c ===== //= Monte Carlo simulation to integrate a function f(x) from 0 to x_max = //=========================================================================== //= Notes: = //= 1) This Monte Carlo simulation estimates the area under the curve = //= f(x) from 0 to x_max = //= 2) The maximum y value (y_max) must be known = //= 3) Must manually set NUM_ITER in the #define = //= 4) Must manually initialize x_max and y_max in main() = //=-------------------------------------------------------------------------= //= Example execution: (NUM_ITER = 1000000) = //= = //= *** BEGIN SIMULATION *** = //= ------------------------------------------------------------- = //= -- *** Results from Monte Carlo integration *** -- = //= ------------------------------------------------------------- = //= - Number of iterations = 1000000 = //= ------------------------------------------------------------- = //= - Estimated area (from 0 to 3.000000) = 9.000072 = //= ------------------------------------------------------------- = //= *** END SIMULATION *** = //=-------------------------------------------------------------------------= //= Build: bcc32 integrate.c = //=-------------------------------------------------------------------------= //= Execute: integrate.c = //=-------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (06/08/09) - Genesis (from findPi.c) = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() #include // Needed for pow() //----- Defines ------------------------------------------------------------- #define NUM_ITER 1000000 // Number of iterations to run //----- Function prototypes ------------------------------------------------- double f(double x); // Function to integrate double rand_val(int seed); // RNG for uniform(0.0, 1.0) from Jain //=========================================================================== //= Main program = //=========================================================================== int main() { double x, y; // X and Y values double x_max; // Integration bound for x for f(x) double y_max; // Integration bound for y for f(x) double hitCount; // Count of hits within the quarter circle double area_est; // Estimated area int i; // Loop counter // Output banner printf("*** BEGIN SIMULATION *** \n"); // Initialize x_max and y_max for f(x) x_max = 3; y_max = f(x_max); // Seed the RNG and initialize hitCount to zero rand_val(1); hitCount = 0.0; // Do for NUM_ITER iterations for (i=0; i 0) x = x_new; else x = x_new + m; // Return a random value between 0.0 and 1.0 return((double) x / m); }