日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ 蒙特卡洛求积分

發布時間:2023/12/20 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 蒙特卡洛求积分 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 最簡單的蒙特卡洛法求指數函數在[0,1]上的積分,面積法
  • const int 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 rectangle double maxYRectangle = exp(upperBound); double minYRectangle = 0.0; // This variable counts the simuated MC points that end up under the curve double 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
  • void integration_standard() {const int 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; }

    總結

    以上是生活随笔為你收集整理的C++ 蒙特卡洛求积分的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。