问题简要说明:我在多项式求根中使用了牛顿-拉夫森算法,在某些情况下无法正常工作。为什么?
我从《C++数值计算》中借鉴了一个牛顿-拉夫森混合算法,如果牛顿-拉夫森算法不收敛(导数值低或收敛速度不快),则采用二分法。
我检查了该算法在多个多项式上的表现,并且它可以正常工作。现在我正在软件中测试它,在特定的多项式中总是出现错误。我的问题是,我不知道为什么这个多项式不能得到结果,而其他多项式可以。因为我想改进该算法以适用于任何多项式,所以我需要知道哪个多项式导致不收敛,以便我可以正确地处理它。
接下来,我将发布有关该算法和多项式错误的所有信息。
多项式:
问题在于算法超出了最大迭代次数,并且存在一个约为0.15的根误差。
我的直接而简洁的问题是:为什么许多(至少1000个)非常相似的多项式达到了1e-10的精度和少量迭代,但是这个多项式无法达到精确的误差?
我知道这个问题很难,可能没有一个真正的直接答案,但我被困扰了几天,不知道该怎么解决。非常感谢您花时间阅读我的问题。
我从《C++数值计算》中借鉴了一个牛顿-拉夫森混合算法,如果牛顿-拉夫森算法不收敛(导数值低或收敛速度不快),则采用二分法。
我检查了该算法在多个多项式上的表现,并且它可以正常工作。现在我正在软件中测试它,在特定的多项式中总是出现错误。我的问题是,我不知道为什么这个多项式不能得到结果,而其他多项式可以。因为我想改进该算法以适用于任何多项式,所以我需要知道哪个多项式导致不收敛,以便我可以正确地处理它。
接下来,我将发布有关该算法和多项式错误的所有信息。
多项式:
f(t)= t^4 + 0,557257315256597*t^3 - 3,68254086033178*t^2 +
+ 0,139389107255627*t + 1,75823776590795
它的一阶导数:
f'(t)= 4*t^3 + 1.671771945769790*t^2 - 7.365081720663563*t + 0.139389107255627
情节:
根(由 Matlab 提供):
-2.133112008595826 1.371976341295347 0.883715461977390
-0.679837109933505
算法:
double rtsafe(double* coeffs, int degree, double x1, double x2,double xacc,double xacc2)
{
int j;
double df,dx,dxold,f,fh,fl;
double temp,xh,xl,rts;
double* dcoeffs=dvector(0,degree);
for(int i=0;i<=degree;i++)
dcoeffs[i]=0.0;
PolyDeriv(coeffs,dcoeffs,degree);
evalPoly(x1,coeffs,degree,&fl);
evalPoly(x2,coeffs,degree,&fh);
evalPoly(x2,dcoeffs,degree-1,&df);
if ((fl > 0.0 && fh > 0.0) || (fl < 0.0 && fh < 0.0))
nrerror("Root must be bracketed in rtsafe");
if (fl == 0.0) return x1;
if (fh == 0.0) return x2;
if (fl < 0.0) { // Orient the search so that f(xl) < 0.
xl=x1;
xh=x2;
} else {
xh=x1;
xl=x2;
}
rts=0.5*(x1+x2); //Initialize the guess for root,
dxold=fabs(x2-x1); //the "stepsize before last,"
dx=dxold; //and the last step
evalPoly(rts,coeffs,degree,&f);
evalPoly(rts,dcoeffs,degree-1,&dx);
for (j=1;j<=MAXIT;j++) { //Loop over allowed iterations
if ((((rts-xh)*df-f)*((rts-xl)*df-f) > 0.0) //Bisect if Newton out of range,
|| (fabs(2.0*f) > fabs(dxold*df))) { //or not decreasing fast enough.
dxold=dx;
dx=0.5*(xh-xl);
rts=xl+dx;
if (xl == rts)
return rts; //Change in root is negligible.
} else {// Newton step acceptable. Take it.
dxold=dx;
dx=f/df;
temp=rts;
rts -= dx;
if (temp == rts)
return rts;
}
if (fabs(dx) < xacc)
return rts;// Convergence criterion
evalPoly(rts,coeffs,degree,&f);
evalPoly(rts,dcoeffs,degree-1,&dx);
//The one new function evaluation per iteration.
if (f < 0.0) //Maintain the bracket on the root.
xl=rts;
else
xh=rts;
}
//As the Accuracy asked to the algorithm is really high (but usually easily reached)
//the results precission is checked again, but with a less exigent result
dx=f/df;
if(fabs(dx)<xacc2)
return rts;
nrerror("Maximum number of iterations exceeded in rtsafe");
return 0.0;// Never get here.
}
算法被调用时需要使用下一个变量:
x1=0.019
x2=1.05
xacc=1e-10
xacc2=0.1
degree=4
MAXIT=1000
coeffs[0]=1.75823776590795;
coeffs[1]=0.139389107255627;
coeffs[2]=-3.68254086033178;
coeffs[3]=0.557257315256597;
coeffs[4]=1.0;
问题在于算法超出了最大迭代次数,并且存在一个约为0.15的根误差。
我的直接而简洁的问题是:为什么许多(至少1000个)非常相似的多项式达到了1e-10的精度和少量迭代,但是这个多项式无法达到精确的误差?
我知道这个问题很难,可能没有一个真正的直接答案,但我被困扰了几天,不知道该怎么解决。非常感谢您花时间阅读我的问题。