#include #include double func(double x){ // return tan(x) - 4.0/3.0; return x*x+2.0*x+0.999999; } double dfdx(double x){ // return 1.0/cos(x)/cos(x); return 2.0*x+2.0; } int step = 0; /* Find x | func(x)=0 by newton method. acc is accuracy of the solution. */ double newton_method(double x1, double x2, double acc){ double tx = (x1+x2)/2.0; double ftx = func(tx); double dftx = dfdx(tx); while(fabs(ftx)>acc){ step++; printf("step:%d tx:%lf\n", step, tx); tx = tx - ftx/dftx; ftx = func(tx); dftx = dfdx(tx); } return tx; } int main(){ double x1, x2; scanf("%lf %lf", &x1, &x2); printf("%lf\n", newton_method(x1, x2, 0.000001)); return 0; }