constint numberOfMCSimulations =1000000;double lowerBound =0.0;double upperBound =1.0;// We need to draw a rectangle which, for positive functions, has a range on the y--axis from 0.0 till (at least) the maximum of the function within the rectanagle// Since we integrate the exponential function here, which is monotonic, the maximum is simply the value of the function at the right side of the rectangledouble maxYRectangle =exp(upperBound);double minYRectangle =0.0;// This variable counts the simuated MC points that end up under the curvedouble pointsUnderCurve =0.0;// The random numbers are generated in the same way as for the Shape project
std::random_device random_dev;
std::mt19937 random_gen(random_dev());// We sample from uniform distributions on a rectangle in the XY-plane. We have put explicitly the bounds of the rectangle// Note that you have a uniform distribution in any finite interval, you can simply performa linear transformation to map it to any other interval (this is what we did in the first shape project)
std::uniform_real_distribution<double>distX(lowerBound, upperBound);
std::uniform_real_distribution<double>distY(minYRectangle, maxYRectangle);for(int i =0; i < numberOfMCSimulations;++i){double randX =distX(random_gen);double randY =distY(random_gen);if(exp(randX)>= randY){++pointsUnderCurve;}}double integralThorughMC =(upperBound - lowerBound)*(maxYRectangle - minYRectangle)* pointsUnderCurve / numberOfMCSimulations;std::cout <<"Integral_0^10 exp analytically: "<<exp(upperBound)-exp(lowerBound)<< std::endl;
std::cout <<"Integral_0^10 exp calculated with basic MC integrator: "<< integralThorughMC << std::endl;
但這個方法有個缺陷,我必須知道此函數的最值才能確定長方形的范圍,所以采用改進的方法。
這里假設 a=0,b=1
voidintegration_standard(){constint numberOfMCSimulations =1000000;double lowerBound =0.0;double upperBound =1.0;std::random_device random_dev;std::mt19937 random_gen(random_dev());// We sample from uniform distributions on a rectangle in the XY-planestd::uniform_real_distribution<double>distX(lowerBound, upperBound);double functionValues =0.0;for(int i =0; i < numberOfMCSimulations;++i){// We simply sum all function values of the uniformly distributed numbers togetherfunctionValues +=exp(distX(random_gen));}// The approximation of the integral is simply the area of the rectangle times the average function value under the uniform distribution in the integration intervaldouble integralThorughMC =(upperBound - lowerBound)* functionValues / numberOfMCSimulations;// For the exp function we know the analytic resultstd::cout <<"Integral_0^10 exp analytically: "<<exp(upperBound)-exp(lowerBound)<< std::endl;// Now we output the MC resultstd::cout <<"Integral_0^10 exp calculated with basic MC integrator: "<< integralThorughMC << std::endl;}