#include
#include
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)
};
return x1;
}
运行与输出:
input eps 1e-08
1e-08
2.000000
2x³-4x²+3x-6=0
(2x³-4x²)+(3x-6)=0
2x²(x-2)+3(x-2)=0
(x-2)(2x²+3)=0
x1=2
2x²+3=0
2x²=-3
x²=-3/2
x=±√6i/2