自适应正交(C+;+;) 在C++中,我对自适应梯形规则算法有一些问题,基本上,不管指定的公差如何,我都得到了同样的精确近似。对于大公差,递归应该很早停止(因为abs(粗-细)将小于3.0*大公差,最小递归级别约为5)
但是,无论选择何种公差,此函数都会运行最大次数。我在哪里搞砸了? 编辑:我的助手函数中可能存在问题自适应正交(C+;+;) 在C++中,我对自适应梯形规则算法有一些问题,基本上,不管指定的公差如何,我都得到了同样的精确近似。对于大公差,递归应该很早停止(因为abs(粗-细)将小于3.0*大公差,最小递归级别约为5),c++,numerical-integration,numerical-analysis,C++,Numerical Integration,Numerical Analysis,但是,无论选择何种公差,此函数都会运行最大次数。我在哪里搞砸了? 编辑:我的助手函数中可能存在问题 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 function
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);
}
双陷阱规则(双a,双b,双(*f)(双),双公差,整数计数)
{
double粗略=粗略_辅助函数(a,b,f);//从辅助函数获取粗略和精细近似值
双重罚款=罚款(a、b、f);
如果((abs(粗-细)=最低水平))
//如果| c-f |=maxLevel,则返回罚款)
//maxLevel是我们可以执行的最大递归次数
{
退还罚款;
}
其他的
{
//如果这些条件都不满足,则执行trap_规则将[a,b]拆分为[a,c]和[c,b]
//在这些时间间隔上——使用递归自适应地接近可容忍的|粗-细|水平
//这里,(a+b)/2=c
++计数;
返回(陷阱规则(a,(a+b)/2.0,f,公差/2.0,计数)+陷阱规则(a+b)/2.0,b,f,公差/2.0,计数));
}
}
编辑:帮助器和测试功能:
//测试功能
双功能_1(双a)
{
返回功率(a,2);
}
//用于比较和公差的“真”积分
//辅助函数
双粗糙辅助线(双a、双b、双(*f)(双))
{
返回0.5*(b-a)*(f(a)+f(b));//根据粗略近似的定义
}
双人精细辅助(双人a、双人b、双人(*f)(双人))
{
双c=(a+b)/2.0;
返回0.25*(b-a)*(f(a)+2*f(c)+f(b));//根据罚款的定义
}
双助手(双a、双b、双(*f)(双x、双tol)
{
返回陷阱规则(a,b,f,tol,1);
}
下面是main()中的内容:
std::cout b;
真值1=分析值(a,b);
对于(int i=0;i我发现与您建议的情况正好相反——minLevel
步骤后算法终止——原因是您在公差测试中使用了abs
,而不是fabs
。abs
正在将其参数转换为int
,从而得到一个小于1的ny错误将四舍五入为零
使用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)
乍一看,我没有发现任何问题。你在每一步都验证了课程和精细值吗?如果其中一个不好,你会遇到这个问题。你能提供帮助程序、测试函数和初始值以便我们可以尝试吗?有什么想法吗?我仍然不明白:/minLevel和maxLevel设置为什么?(这样我们可以运行您的主服务器)。
(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)