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;
}
标签:prime,fp,return,函数,double,newton,笔记,C语言,pow 来源: https://www.cnblogs.com/quanjun/p/14541252.html