#include ‘iostream.h’
#include 'cmath.h'
double fuc(double x)
{
double y;
y=x*x*x-x-1;
return y;
}
double fu(double x)
{
double y;
y=3*x*x-1;
return y;
}
int f()
{
double x0,x1,e;
int N,M;//N是迭代最大次数,M是下山最大次数
cout<<"输入数据:"<<endl;
cout<<" x0=";
cin>>x0;
cout<<" e=";
cin>>e;
cout<<" N=";
cin>>N;
cout<<" M=";
cin>>M;
int i=0,k=1;//k记录迭代次数,i记录下山次数
for(k=1;k<=N;)
{
if(fu(x0)==0)
cout<<"error"<<endl;
else
{
double h=1;//h为下山因子
for(i=0;i<M;)
{
x1=x0-h*fuc(x0)/fu(x0);
if(fabs(fuc(x1))<fabs(fuc(x0)))
{
cout<<"x"<<i<<"="<<x0<<" h="<<h<<endl;
i++;
h=h*0.5;
//continue;
}
else
break;
}
if(i>=M)
{
cout<<"请重新输入x0"<<endl;
f();
}
if(fabs(x1-x0)<e)
{
cout<<"x="<<x1<<endl;
break;
}
else
{
k++;
x0=x1;
//continue;
}
}
}
if(k>N)
cout<<"failure"<<endl;
return 0;
}
int main()
{
f();
return 0;
}
为什么他运行的时候只计算一步结果就不再变了,就算不变的话也应该因为精度问题跳出结束的呀?求指点