这里给出的是最原始的迭代法的代码,此代码不能根据输入函数求出迭代函数,需要自己编写迭代函数,大家可以自己动动手改造一下代码,在程序中实现。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
float g(double x)
{
return((log10(x)+7)/2);
}//迭代函数
int main()
{
double x0,s,x1;
int n;
printf("请依次输入输入x0,精度,最大迭代次数n:\n");
scanf("%lf%lf%d",&x0,&s,&n);
int k = 1;
while(1){
x1 = g(x0);
double m = x1-x0;
fabs(m);
if(m<s)
{
printf("%lf\n",x1);
break;
}
else
{
if(k>=n)
{
printf("迭代失败\n");
break;
}
else
{
k = k+1;
x0 = x1;
}
}
}
return 0;
}