//牛顿迭代法的实现
#include <iostream>
#include <math.h>
using namespace std;
double f(double x)
{
return exp(x)+10*x-2;
}
double f1(double x)
{
return exp(x)+10;
}
void NDDA(double x0,double e,int N)
{
int k=1,I;
double x1;
while (k<N)
{
if(f1(x0)==0)
{
I=-1;
cout<<"I="<<I<<" 因f'(x0)=0中断!"<<endl;
break;
}
x1=x0-f(x0)/f1(x0);
if(fabs(x1-x0)<e)
{
I=0;
cout<<"I=0"<<" 可求得满足精度的根:"<<endl;
cout<<"x1="<<x1<<","<<"f(x1)="<<f(x1)<<","<<"k="<<k<<endl;
break;
}
x0=x1;
k++;
}
if(k>N)
{
cout<<"I=1"<<" 迭代次数过多!"<<endl;
}
}
int main()
{
double x0,e;
int N;
cout<<"请输入初始值x0:";
cin>>x0;
cout<<"请输入允许误差e:";
cin>>e;
cout<<"请输入最大循环次数N:";
cin>>N;
NDDA(x0,e, N);
return 0;
}