用二分法求方程在(-10,10)之间的根:2x^3-4x^2+3x-6=0.

解:x1<=x0=(x1+x2)/2<=x2

程序:

#include

#include

int main()

{

float x0,x1,x2,fx0,fx1,fx2;

do

{

printf("输入x1,x2的值:");

scanf("%f,%f", &x1, &x2);

fx1 = 2*x1*x1*x1 - 4 * x1*x1 + 3 * x1 - 6;

fx2 = 2 *x2*x2*x2 - 4 *x2*x2 + 3 * x2 - 6;

} while (fx1*fx2>0);

do

{

x0 = (x1 + x2)/2;

fx0 = 2 * x0*x0*x0 - 4 * x0*x0 + 3 * x0 - 6;

if (fx0*fx1 < 0)

{

x2 = x0;

fx2 = fx0;

}

else

{

x1 = x0;

fx1 = fx0;

}

} while (fabs(fx0)>= 1e-5);

printf("x=%5.2f\n",x0);

return 0;

}

结果:

输入x1,x2的值:-10,15

x= 2.00

请按任意键继续. . .