//1.区间二分法
#include <iostream.h>
#include <math.h>
void main()
{ float f(float x);
float a=3,b=4,y1,y2,y0;
do{y1=f(a);
y2=f(b);
y0=f((a+b)/2);
if(y0==0) cout<<"x="<<(a+b)/2<<endl;
else if(y1*y0<0) b=(a+b)/2;
else if(y2*y0<0) a=(a+b)/2;
}while(fabs(b-a)>0.001);
cout<<"x="<<(a+b)/2<<endl;
}
float f(float x)
{ float y;
y=x*x*x-2*x*x-4*x-7;
return y;
}
#include <iostream.h>
#include <math.h>
void main()
{ float f(float x);
float a=3,b=4,y1,y2,y0;
do{y1=f(a);
y2=f(b);
y0=f((a+b)/2);
if(y0==0) cout<<"x="<<(a+b)/2<<endl;
else if(y1*y0<0) b=(a+b)/2;
else if(y2*y0<0) a=(a+b)/2;
}while(fabs(b-a)>0.001);
cout<<"x="<<(a+b)/2<<endl;
}
float f(float x)
{ float y;
y=x*x*x-2*x*x-4*x-7;
return y;
}
****************************************************
//2迭代法
#include <iostream.h>
#include <math.h>
double f(double x);
main()
{
double x0=1.0,x;
x=f(x0);
cout<<"
#include <iostream.h>
#include <math.h>
double f(double x);
main()
{
double x0=1.0,x;
x=f(x0);
cout<<"