#include <stdio.h>
#include <math.h>
double root(double (*f)(double),double (*f1)(double), double x,double eps);
double fx(double x){
return 2*x*x*x-4*x*x+3*x-6;
}
double fp(double x){
return 6*x*x-8*x+3;
}
int main()
{
double x=1.5,y,eps=1e-08;
printf("input eps 1e-08\n"); scanf("%lf",&eps);
y = root(fx,fp,x,eps);
printf("%lf\n",y);
return 0;
}
double root(double (*f)(double x),double (*f1)(double x), double x,double eps){
double x0,x1;
x0=x;
while(1){
x1= x0 - (*f)(x0)/ (*f1)(x0);
if (fabs(x1-x0)<eps) break;
x0=x1;
};
return x1;
}
运行与输出:
input eps 1e-08
1e-08
2.000000
温馨提示:答案为网友推荐,仅供参考