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++ 蒙特卡洛求积分相关推荐
- 随机投点法计算定积分java_科学网—0026:蒙特卡洛求定积分三种方法的理解 - 何成文的博文...
蒙特卡洛主要思想就是采用粒子(大多是均匀分布生成的随机数,称为粒子)将积分符号转化为求和,从而实现快速求解目的.定积分求解主要有三种方法:随机投点法.平均值法.重要抽样法: 问题描述:如何求exp(x ...
- 高等数学:e的-t平方次方求积分
题目 求积分:e的-t平方次方 解答 答案: 在一般的<高等数学>教材中,泊松积分很少会涉及,而在实际问题中,例如在研究热传导或是概率问题的时候,都会遇到泊松积分.但由于其被积函数的原函数 ...
- c语言用梯形法计算积分,c语言用梯形法求积分
c语言用梯形法求积分 來源:互聯網 2009-12-29 11:56:13 評論 分類: 電腦/網絡 >> 程序設計 >> 其他編程語言 問題描述: #include fl ...
- matlab中对于xf(x)的积分,[matlab 积分]MATLAB求积分?
MATLAB求积分? 问题补充:我想用MATLAB求一下这个式子的积分,谁能帮我一下?g(f)=(e^4kxf)/sinh(kctf)未知量是f,最后求出来的式子里保留其他几个字母. ●matcom ...
- ZOJ 3898 Stean 矩形法求积分
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5601 题意:求一个绕y轴旋转的旋转体体积.R = 2+cos(z). ...
- 利用python求积分
python的numpy库集成了很多的函数.利用其中的函数可以很方便的解决一些数学问题.本篇介绍如何使用python的numpy来求解积分.代码如下: # -*- coding: utf-8 -*- ...
- COMSOL离散数据的插值拟合,并对插值函数特定点求值求积分
1[离散数据获取] a.首先先获得数据,我这里用的物理场中的一维绘图组的数据(注:虽然看似光滑曲线,但都是离散数据点组成的). b.右键单击线图结果图,[复制到表格] 现在我们就获得了一系列离散数据, ...
- 复合梯形公式与复合辛普森公式求积分
一 实验目的 1. 掌握复合梯形公式与复合辛普森公式的基本思想. 2. 编程实现用复合梯形公式与复合辛普森公式求积分. 3. 熟悉matlab软件的使用. 二 实验内容 1.用复合梯形公式计算积分 I ...
- python蒙特卡洛算法求积分_python中实现蒙特卡洛算法
蒙特卡洛算法,是一种以概率统计理论为指导的一类非常的数值计算方法,是指使用随机数来解决很多计算问题的方法. 应用一:用蒙特卡洛算法求解圆周率 思路:在直角座标系中选取x[-1,1],y[-1,1]的正 ...
最新文章
- 常用排序算法 - 稳定性和复杂度分析
- HttpClient常用的一些常识
- aws部署ssh_将Quarkus应用程序部署到AWS Elastic Beanstalk
- 矩池云上安装 NVCaffe教程
- 斯特林数与斯特林反演
- linux内存源码分析 - 内存压缩(实现流程)
- 1059 Prime Factors (25 分)质因子 易错题
- python好玩的代码-这10个Python项目超有趣!
- 曼联球星普巴来罗!POGMOJI APP即将上市
- SCC1传输请求(同系统跨Client)
- tomcat中JSP跳转Servlet时卡白页的原因
- [python库]psd文件操作库--psd_tools
- 【英语-同义词汇词组】consider,think,believe,count,deem,reckon,regard、hold 表示【认为】时的用法及区别
- Mysql 快速生成日期时间维度表
- 没有比粥更温柔的了。念予毕生流离红尘,就找不到一个似粥温柔的人。
- 开源生物特征识别库 OpenBR
- 【战神引擎】游戏不开门怎么解决?
- 浙师大 计算机科学技术导论,计算机科学技术导论
- 万丈高楼平地起,勿在浮沙筑高台--论程序员基础知识的重要性
- 如何把华为数据分析项目写进简历