aitken迭代方法求方程根
对于收敛的迭代过程,只要迭代足够多次,就可以达到任意的精度,但有时候迭代过程收敛过程缓慢,从而增大了计算量。而aitken迭代法具有加速收敛效果。
#include <stdio.h>
#include <math.h>
#define MaxDepth 100 /*最大迭代深度*/
#define epsilion 1e-5
typedef double (*calfun) (double);
double f1(double x)
{
return x*x*x-1;
}
int aitken(calfun fun,double x0,double *ans)
{
/*
x0初始值
x1,x2存放迭代的中间结果
*/
int i;
double x1,x2,y,z;
x1=x0;
for(i=0;i<MaxDepth;i++)
{
y=fun(x1);
z=fun(y);
x2=z-((z-y)*(z-y)/(z-2*y+x1));
if(fabs(x2-x1)<1e-5)
{
*ans=x2;
return 1;
}
x1=x2;
}
printf("After %d repeate,no solved.\n",MaxDepth);
return 0;
}
int main()
{
double ans;
if(aitken(f1,1.5,&ans))
{
printf("%lf\n",ans);
}
getche();
return 0;
}
本文来源于网络小筑 http://www.iwebtrados.com.cn/ , 原文地址:http://www.iwebtrados.com.cn/post/293.html