自适应正交 (C++)
Adaptive Quadrature (C++)
我在C++的自适应梯形规则算法中遇到了问题 - 基本上,无论指定的公差如何,我都会得到相同的精确近似值。对于大容差,递归应该很早就停止(因为 abs(粗细)将小于 3.0*大容差,最小递归级别约为 5)。
但是,此函数的作用是运行最大次数,而不考虑选择公差。我哪里搞砸了?编辑:也许我的帮助程序函数有问题?
double trap_rule(double a, double b, double (*f)(double),double tolerance, int count)
{
double coarse = coarse_helper(a,b, f); //getting the coarse and fine approximations from the helper functions
double fine = fine_helper(a,b,f);
if ((abs(coarse - fine) <= 3.0*tolerance) && (count >= minLevel))
//return fine if |c-f| <3*tol, (fine is "good") and if count above
//required minimum level
{
return fine;
}
else if (count >= maxLevel)
//maxLevel is the maximum number of recursion we can go through
{
return fine;
}
else
{
//if none of these conditions are satisfied, split [a,b] into [a,c] and [c,b] performing trap_rule
//on these intervals -- using recursion to adaptively approach a tolerable |coarse-fine| level
//here, (a+b)/2 = c
++count;
return (trap_rule(a, (a+b)/2.0, f, tolerance/2.0, count) + trap_rule((a+b)/2.0, b, f, tolerance/2.0, count));
}
}
EDIT: Helper and test functions:
//test function
double function_1(double a)
{
return pow(a,2);
}
//"true" integral for comparison and tolerances
//helper functions
double coarse_helper(double a, double b, double (*f)(double))
{
return 0.5*(b - a)*(f(a) + f(b)); //by definition of coarse approx
}
double fine_helper(double a, double b, double (*f)(double))
{
double c = (a+b)/2.0;
return 0.25*(b - a)*(f(a) + 2*f(c) + f(b)); //by definition of fine approx
}
double helper(double a, double b, double (*f)(double x), double tol)
{
return trap_rule(a, b, f, tol, 1);
}
以下是main()中的内容:
std::cout << "First we approximate the integral of f(x) = x^2 on [0,2]" << std::endl;
std::cout << "Enter a: ";
std::cin >> a;
std::cout << "Enter b: ";
std::cin >> b;
true_value1 = analytic_first(a,b);
for (int i = 0; i<=8; i++)
{
result1 [i] = helper(a, b, function_1, tolerance[i]);
error1 [i] = fabs(true_value1 - result1 [i]);
}
std::cout << "(Approximate integral of x^2, tolerance, error )" << std::endl;
for (int i = 0; i<=8; i++)
{
std::cout << "(" << result1 [i] << "," << tolerance[i] << "," << error1[i] << ")" << std::endl;
}
我发现与您建议的情况完全相反---算法仅在--- minLevel
步后终止,原因是由于您使用了 abs
,而不是在公差测试中fabs
。abs
正在将其参数转换为int
因此任何小于 1 的错误都将四舍五入为零。
有了abs
,我从一个非常相似的程序获得此输出:
(0.333496,0.001,0.00016276)
(0.333496,0.0001,0.00016276)
(0.333496,1e-05,0.00016276)
(0.333496,1e-06,0.00016276)
(0.333496,1e-07,0.00016276)
(0.333496,1e-08,0.00016276)
(0.333496,1e-09,0.00016276)
(0.333496,1e-10,0.00016276)
(0.333496,1e-11,0.00016276)
替换为fabs
我得到这个:
(0.333496,0.001,0.00016276)
(0.333374,0.0001,4.06901e-05)
(0.333336,1e-05,2.54313e-06)
(0.333334,1e-06,6.35783e-07)
(0.333333,1e-07,3.97364e-08)
(0.333333,1e-08,9.93411e-09)
(0.333333,1e-09,6.20882e-10)
(0.333333,1e-10,3.88051e-11)
(0.333333,1e-11,9.7013e-12)
相关文章:
- 如何检查助推融合序列是否为自适应结构
- C++中排序的自适应功能
- Boost.Spirit.Qi 语法,用于 Boost.Fusion 自适应结构中的默认值
- 自适应路径 - 阿斯塔尔修改
- 具有自适应 std::p air 点类型的 R 树查询
- 将自适应阈值应用于范围函数 opencv c++
- 使用C 使用PayPal自适应付款API的最佳方法是什么?
- 将 Rcpp 与 C 代码链接,以实现自适应城域拒绝采样
- 采用 Opencv c++ 的自适应中值滤波器
- 自适应正交 (C++)
- Boost Spirit Parser用三个字符串的矢量编译成一个结构,自适应不工作
- OpenCV / C++ - 如何在矢量上使用自适应阈值而不是垫子
- 您将如何在调度程序中实现这种自适应的"软糖因子"?
- OpenCV中的自适应算法实现
- 用自适应MeanShift对特征空间-SURF描述符进行聚类
- 自适应阈值opencv
- boost::spirit::qi具有相同的简单自适应结构属性的规则会导致编译错误
- 为什么BoostSpirit正确地将标识符解析为std::字符串,而不是解析为仅由std::string组成的自适应结构
- boost::spirit可选解析为融合自适应结构
- (自适应)opencv中的阈值错误(cvarrToMat中的错误参数(未知数组类型))