牛顿迭代公式

来源:互联网 发布:数据服务公司 编辑:程序博客网 时间:2024/04/30 15:37
#include <iostream>
using namespace std;
void main()
{
float x0,result,e;
float f(float x);
float f1(float x);
float newdon(float (*f)(float),float (*f1)(float),float x0,float e);
cout<<"输入初始值:";
cin>>x0;
cout<<"输入精度:";
cin>>e;
result=newdon(f,f1,x0,e);
cout<<"运算结果是:"<<result<<endl;
}
float newdon(float (*f)(float),float (*f1)(float),float x0,float e)
{
for(float x1=x0-f(x0)/f1(x0);x1-x0>=e || x0-x1>=e;)
  x0=x1,x1=x0-f(x0)/f1(x0);
return x1;
}
float f(float x)       [url=file://要/]file://要[/url]解的方程为f(x)=0
{
return x*x-x-12;
}
float f1(float x)
{
return 2*x-1;
}