版权声明:请勿商业化使用 https://blog.csdn.net/qq_40991687/article/details/89394260
问题描述:
对方程f(x)=x-sin(x)-0.5=0使用迭代法求根,初始值x0=1.0
迭代法:由给出的方程改写成 x=g(x),选取方程的一个初始值x0,且按照xk+1=g(xk)逐次代入,g(x)称为迭代函数 [^1]: 单点迭代法
注意 :只有当|g’(x)|<1时函数收敛才可用单点迭代法
迭代法求 f(x)=x-sin(x)-0.5
#include<cstdio>
#include<cmath>
double fact(double x) {
return sin(x)+0.5;//返回函数f(x)=x-sin(x)-0.5的值
}
int main() {
double x0;
scanf("%lf",&x0);//迭代的初始值
double x1=fact(x0);//迭代公式:x=sin(x)+0.5;
while(x1-x0>10e-7) {//精确到小数点后六位,所以误差在10-7内
printf("%.6lf\n",x1);
x0=x1;
x1=fact(x0);
}
return 0;
}
/*Authors
: 江楚郎*/
输出: