I am trying to approximate the root of a polynomial using Newton-Raphson method. The code I wrote to do it looks like this:
#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;
}
}
I know it is not necessary to write the variables o,c,nq, etc sin I could just use their exact values. This is a part of a larger problem and I need those variables, so ignore that.
This program output this:
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
When instead it should converge to the point -0.57735026918963. I know the Newton-Raphson converges for sure, so the error should be on the code. I've also tried to localitzate the problem using printf, and I think The problem comes in the second iteration. I think the program fails to calculate nq correctly but I don't know why.