ニュートンラフソン法を使用して10次多項式の根を推定することを想定しているcでプログラムを作成しています。ユーザーは 10 個の係数を入力し、方程式の根を推定することを想定しています。絶対相対誤差は 0.00000001 で、許容される反復の最大数は 70 です。サンプル コードは以下のとおりです。
n=0;
while(abserr<=0.00000001){
yold=y;
y = y-(poly(y,coefficients,11)/poly_der(y,coefficients,11));
ynew = y;
error=ynew-yold;
abserr=sqrt(error*error);
printf("iteration x%d = %.2f error =%.2f\n",n+1,y,abserr);
n++;
iteration++;
if(iteration==70){
printf("you have reached the maximum number of iterations\n");
break;}
}
関数 poly と poly_der は、それぞれ多項式とその導関数の値を計算します。定義は以下のとおりです。
float poly(float x, float coefficients[], int order)
{
int idx;
float total;
for (idx = 0; idx < order; idx++)
total += coefficients[idx] * pow(x, idx);
return total;
}
float poly_der(float x, float coefficients[], int order)
{
int idx;
float total;
for (idx = 0; idx < order; idx++)
total += coefficients[idx] * deri(x, idx);
return total;
}
deri は、多項式の項の導関数を計算する関数です。残念ながら、このプログラムは予期しない結果をもたらします。コンパイルして正常に実行されるため、どこが間違っているのかわかりません。ニュートン法を使用してルートを推定できる別の方法はありますか? 必要な結果が得られるようにプログラムを改善するにはどうすればよいですか。