我试图使用Newton-Raphson方法逼近多项式的根。我写的代码看起来像这样:
#include <stdio.h>
#include <math.h>
int main (){
double c, nq, nnq, nnnq, v, h, q, o;
o=2;
c=-0.55;
nq=-0.04625;
nnq=-0.55;
nnnq=1;
while(fabs(c-v) > 0.000001)
{
nq=((2*(o-1)+1)*(c)*(nnq)-(o-1)*nnnq)/((o-1)+1);
q=(-o*c*nq+o*nnq)/(1-(c*c));
h=(c-(nq/q));
printf("The better root is %.15lf\n",h);
v=c;
c=h;
}
}
我知道没有必要写变量o,c,nq等sin我只能使用它们的确切值。这是一个更大问题的一部分,我需要这些变量,所以忽略它。
该程序输出:
The better root is -0.578030303030303
The better root is -0.591696792857493
The better root is -0.598609887802599
The better root is -0.602171714355970
The better root is -0.604024260228500
The better root is -0.604992519745332
The better root is -0.605499890229896
The better root is -0.605766110042157
The better root is -0.605905895095070
The better root is -0.605979319651017
The better root is -0.606017894664121
The better root is -0.606038162857992
The better root is -0.606048812800124
The better root is -0.606054408979837
The better root is -0.606057349623975
The better root is -0.606058894866533
The better root is -0.606059706860161
相反,它应该收敛到-0.57735026918963点。我知道Newton-Raphson肯定会收敛,因此错误应该在代码上。我也尝试使用printf本地化问题,我认为问题出现在第二次迭代中。我认为该程序无法正确计算nq,但我不知道为什么。
答案 0 :(得分:1)
这是你方程式的牛顿方法(它是一个快速代码,不检查变量名称):
#include <stdio.h>
#include <math.h>
int main ()
{
double s = 2.0, fx = 0, dfx = 0, p = 0;
while(fabs(s - p) > 0.000001)
{
fx = 0.5 * (3 * s * s - 1);
dfx = 3 * s;
p = s;
s = s - (fx / dfx);
printf("The better root is %.15lf\n", s);
}
return 0;
}
它收敛到0.577350269189626
。您的问题是您正在尝试同时计算2次递归。顺便说一句,在你的问题中,你说你想要计算多项式的根和#34;。我没有得到你的意思。如果从root开始,则表示等式的平方根,您需要更新此代码并相应地更改fx
和dfx
。
答案 1 :(得分:1)
您希望将勒让德多项式的递归计算结合到订单x = sqrt(1.0/3)
,而不是仅计算o
,可能会稍后将该方法扩展为大于2的o
值。迭代是
P(0,c) = 1; P(1,c) = c;
(n+1)*P(n+1,c) = (2*n+1)*c*P(n,c) - n*P(n-1,c), n=1, ... , o-1
,导数可以计算为
(1-c^2)*P'(o,c) = -n*c*P(o,x) + n*P(o-1,c).
这个迭代你需要包含在Newton循环中,在理想情况下使用Legendre多项式的对象以及值和导数的方法。我已经修改了你的结构以使用JavaScript:
var my_div = document.getElementById("my_div");
var c = -0.55;
var v = -20;
var o = 2;
while( Math.abs(v-c) > 1e-12 ) {
p0 = 1;
p1 = c;
for(n=1; n<o; n++) {
// p0, p1, p2 stand for P(n-1,c), P(n,c), P(n+1,c)
p2 = ((2*n+1)*c*p1 - n*p0) / (n+1)
p0 = p1; p1 = p2;
}
// p0, p1 now contain p(o-1,x), p(o,x)
p1prime = ( -o*c*p1 + o*p0) / (1-c*c);
h = c - p1/p1prime;
my_div.innerHTML += "<p>The better root is "+h+"</p>";
v = c; c = h;
}
<div id="my_div"></div>