首页 > 应用开发 > IT综合 > 简单迭代法(C++版)
本程序为本人参考一些资料后编写,在visual c++6.0下调试通过。
[@more@]///////////////
/////简单迭代法:f(x)=0改写成x=g(x)
/////原理:x_(k+1)=g(x_k)
/////有条件限制,可能不收敛
/////改进:输出每一次迭代的结果,将迭代模块做全面
///////////////
#include
#include
#include
using namespace std;
//////////
double fun(double x);
void SimpleIterative(double(*pf)(double x));
//////////
//////////
int main()
{
SimpleIterative(fun);
return 0;
}
//////////
//////////
double fun(double x)
{
return pow(x+1,1/3.);///f(x)=x^3-x-1转化成x^3=x+1
}
/////////
void SimpleIterative(double(*pf)(double x))
{
double x,x0;
int maxflag,k=1;
double err;
cout<
cout<<" 请输入精度要求:err=";cin>>err;
cout<<" 请输入初始值:x0=";cin>>x;
cout<
{
x0=x;
x=pf(x0);
cout<<" 第"<
}while(fabs(x-x0)>err&&k
cout<<" 迭代失败!此时k="<
{
cout<<" 迭代成功!迭代次数为:k="<
}
/////////
/////////
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/11355887/viewspace-974945/ ,如需转载,请注明出处,否则将追究法律责任。