#include<stdio.h>
#include<math.h>
#define epsl 1e-6
double F(double x){
return x * x -2;
}
double f(double x){
return 2 * x;
}
double NewTon(double(*F)(double), double(*f)(double)){
double x = 1.0;
while(fabs(F(x)) > epsl){
x -= F(x) / f(x);
}
return x;
}
int main(){
printf("方程的根近似%lf\n", NewTon(F,f));
return 0;
}
当然这里你可以随意定义方程,传入参数x,让其进行迭代。