#include
#include
double f(double d)
{
return 2*pow(d,3)-4*d*d+3*d-6;
}
void main()
{
int k=0;double a,b,limit;
printf("\nplease input the 区间:");
scanf(""%lf %lf",&a,&b);
printf("\nplease input the 解的精确程度:");
scanf("%lf",&limit);
if(f(a)*f(b)>0)
printf("\n 无法用二分法求解");
else
{
while((b-a)>limit)
{
if((f(a+b)/2)*f(b)<0)//异号
a=(a+b)/2;
else //同号
b=(a+b)/2;
k++;
}
print("\n经历了%d次二分法求解\n",k);
}
}
#include
#include
double f(double x)
{return 2*x*x*x-4*x*x+3*x-6;}
double key(double a, double b,double l)
{ if(f(a)*f(b)>0)
printf("方程在该区域无解。");
else
while(fabs(f(a)-f(b))>l)
{if(f((a+b)/2)*f(b)<0)
a=(a+b)/2;
else
b=(a+b)/2;
}
return a;
}
int main()
{double k;
k=key(-10,10,0.00001);
printf("解为%f\n",k);}