二分法求根问题
#include<stdio.h>#include<math.h>
/*int main()
{
int a = -10,b = 10;
flf = 2*a*a*a-4*a*a+3*a-6;
frg = 2*b*b*b-4*b*b+3*a-6;
mid = (a+b)/2;
fmid = 2*mid*mid*mid-4*mid*mid+3*mid-6;
if(flf*frg >= 0)
printf("原方程在区间上无根或不止一根\n");
else
{
while(flf*mid < 0)
{
}
}
}*/
int main()
{
float a = -10,b = 10,mid,fmid,flf,frg;
float _x=20;
int icount = 0;
do
{
flf = 2*a*a*a-4*a*a+3*a-6;
frg = 2*b*b*b-4*b*b+3*a-6;
//if(flf*frg >= 0)
//{
//printf("原方程在区间上无根或不止一根\n");
//break;
//}
mid = (a+b)/2;
fmid = 2*mid*mid*mid-4*mid*mid+3*mid-6;
if((flf*fmid) < 0)
{
b = mid;
}
if((fmid*frg)<0)
{
a = mid;
}
//if(fabs(fmid)< 1e-6)
// break;
_x= b-a;
//printf("%f\n",_x);
icount++;
}while(_x > 1e-2);
printf("The root is %f\n",(a+b)/2);
printf("%d\n",icount);
}
//想用二分法求方程2x^3 - 4x^2 + 3x - 6 = 0 在[-10,10]上的根,达不到目的(改小1e-2也没用),跪求高手指点