Newton迭代法数学原理
求非线性方程的根,牛顿迭代法计算公式
一般地,牛顿迭代法具有局部收敛性,为保证迭代收敛,要求,对充分小的,。如果,,,那么,对充分小的,当时,由牛顿迭代法计算出的收敛于,且收敛速度是2阶的;如果,,,那么,对充分小的,当时,由牛顿迭代法计算出的收敛于,且收敛速度是1阶的;
问题
利用牛顿迭代法求的根
输入:初值,精度,最大迭代次数
输 出:方程根的近似值或计算失败标志
程序流程
核心代码
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
double x, e1, e2;
int n;
double f(double x) { return cos(x) - x; }
double df(double x) { return -sin(x) - 1; }
int main() {
scanf("%lf%lf%lf%d", &x, &e1, &e2, &n);
for (int i = 1; i <= n; i++) {
double F = f(x), DF = df(x);
if (fabs(F) < e1) {
printf("%lf", x);
return 0;
}
if (fabs(DF) < e2) {
printf("Failed");
return 0;
}
double x1 = x - F / DF;
double tol = fabs(x - x1);
if (tol < e1) {
printf("%lf", x1);
return 0;
}
x = x1;
}
printf("Failed");
return 0;
}