c 牛顿法求方程近似解

 

 

#include <stdio.h>
#include <math.h>
#define EPSILON 1e-6

double f(double x) {
    return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;
}

double f_prime(double x) {
    return 6 * pow(x, 2) - 8 * x + 3;
}
double h(double x) {
    return  pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;
}

double h_prime(double x) {
    return 3 * pow(x, 2) - 8 * x + 3;
}


double newton(double (*fp)(double),double (*fp_prime)(double)) {
    
    
    double x = 1.5;
    while (fabs(fp(x)) > EPSILON){
        x = x - fp(x) / fp_prime(x);
    }
    return x;
}

int main() {
    printf("%g\n", newton(f,f_prime));
    printf("%g\n",newton(h,h_prime));
    return 0;
}

 

上一篇:数据处理(txt的提取)


下一篇:OAuth2.0介绍