使用 MC 方法计算积分
Calculating the integrals using MC method
我正在尝试编写一种算法,该算法将使用蒙特卡洛方法解决积分。然而,对于给定的输入数据,计算结果与预期结果不同;我计算表达式 exp(-ax^2(,a = 1,点在 [0.5, 1] 范围内。我期望得到的结果大约是 0.29,但我得到了大约 0.11。也许有任何建议我做错了什么?
#include<iostream>
#define N 100000000
#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstdlib>
double pickPoint(double left, double right);
double functionE(double a, double x);
int main(){
srand(time(NULL));
double a;
std::cin >> a;
double leftBorder, rightBorder;
std::cin >> leftBorder >> rightBorder;
double result = 0;
for (int j = 0; j < N; j++){
result += functionE(a, leftBorder + pickPoint(leftBorder, rightBorder));
}
printf("%lf", (rightBorder - leftBorder) * (result / N));
return 0;
}
double pickPoint(double left, double right){
return left + (double)(rand() / (RAND_MAX + 1.0) * (right - left));
}
double functionE(double a, double x){
return exp((-a*pow(x, 2)));
}
result += functionE(a, leftBorder + pickPoint(leftBorder, rightBorder));
应该是
result += functionE(a, pickPoint(leftBorder, rightBorder));
你把边界推得很远。
您正在向leftBorder
添加pickPoint(leftBorder, rightBorder)
。您已经获得了一个介于 leftBorder
和 rightBorder
之间的值。这种添加是不必要的。
相关文章:
- 是否有一种标准方法来计算两个 asctime() 值之间的天数
- 有没有一种惯用的方法可以在不存储变换或不必要地重新计算的情况下找到数组变换的最小/最大值?
- 寻求提高Microsoft密封库计算效率的方法
- 在 C++20 计算范围内相邻对的最简洁明了的方法是什么?
- 计算浮点余数的最佳方法
- 计算将整数n分解为4平方和的方法的数量
- 计算两个uint8_t变量之间差值的最快方法是什么?
- 使用 MC 方法计算积分
- 计算 <Classtype*> 向量中所有项的布尔值的最有效方法,如果全部为真则返回 true
- C++ std::string 中是否有任何函数可以计算两个字符串的相同起始字符的总数或任何最佳方法
- C/C++ 方法中最快/最短计算二进制数字总和/又名二进制中的 1 数
- 对于我扩展此程序来计算最高10x10矩阵的倒数的最简单方法是什么
- 什么是计算C中PI的最有效方法
- 是否有一种计算CGAL中Surface_mesh对象的PCA的方法
- 是否有任何数据类型或方法可以计算当前单元格中先前数组单元格的总和
- 有没有一种简单的方法来计算库中记录的函数
- 复数程序编译时的计算方法错误
- 数据包CRC计算方法
- C++中错误的计算方法
- 编译器本身使用并行计算方法运行