2x^3-4x^2+3x-6=0
2x^3-4x^2+3x-6=0
f(x)=f(x0)+f'(x0)*(x-x0)+o((x-x0)^2) 令f(x)=0 则x=x0-f(x0)/f'(x0) 循环即可. 如下: #include<stdio.h> #include<math.h> #include<conio.h> #define epsilon 1e-20
double f(double x) { double y; y=((2*x-4)*x+3)*x-6; return y; }
double g(double x) { double y; y=(6*x-8)*x+3; return y; }
void main( ) { double x0,x,t; printf("Input x0:"); scanf("%lf",&x0); t=x0; x=x0-f(x0)/g(x0); while( abs(f(x))>=epsilon ) { x0=x; x=x0-f(x0)/g(x0); } printf("The roots that near %lf is:%lf.\n",t,x); printf("%lf\n",((2*x-4)*x+3)*x-6); getch( ); } 结果如下: Input x0:1.5 The roots that near 1.500000 is:2.061002. 0.701248 Input x0:1.4 The roots that near 1.400000 is:2.014249. 0.158370 Input x0:1.3 The roots that near 1.300000 is:2.058738. 0.674130 Input x0:1.6 The roots that near 1.600000 is:2.020943. 0.233897 Input x0:1.7 The roots that near 1.700000 is:2.005515. 0.060912 Input x0:1.8 The roots that near 1.800000 is:2.035821. 0.404387 故而x0=1.7时已经是比较准确了. 有问题请与我联系.一起进步. qq:283421560