二分迭代法解一元方程。
来源:百度文库 编辑:神马文学网 时间:2024/06/03 11:49:26
#include
#include
using namespace std;
float f(float);
void main()
{
const double error=0.00001;
float x1=0,x2=3,x;
do
{
x=(x1+x2)/2;
if(f(x)*f(x1)>0)
x1=x;
else
x2=x;
// cout<}while(fabs(x1-x2)>error);
cout<}
float f(float x)
{
return exp(x)-x-2;//x*x-x-2
}
#include
using namespace std;
float f(float);
void main()
{
const double error=0.00001;
float x1=0,x2=3,x;
do
{
x=(x1+x2)/2;
if(f(x)*f(x1)>0)
x1=x;
else
x2=x;
// cout<
cout<
float f(float x)
{
return exp(x)-x-2;//x*x-x-2
}