#include <stdio.h>
#include <math.h>
float f(float x) {
return x*x*x - 2*x*x + 4*x + 1;
}
float f_(float x) {
return 3*x*x - 4*x + 4;
}
int iterate(float x0, float eps, float *x1) {
*x1 = x0 - f(x0) / f_(x0);
return fabs(f(*x1)) < eps ? 1 : 0;
}
void NewTon(float x0, float eps, float *x1) {
int cnt = 0;
float old = x0, new;
while(cnt < 200 && !iterate(old, eps, &new)) {
old = new;
cnt++;
}
printf("x = %f\tf(x) = %f\n", new, f(new));
}
int main() {
NewTon(0, 1e-6, 0);
return 0;
}
牛顿迭代法求根
最新推荐文章于 2024-06-13 09:24:28 发布