有没有办法在C语言中计算一个未知的函数的偏导数值?我在这里读到了如何实现求导数:Compute a derivative using discrete methods,但这只有当我知道函数f(x)的确切形式时才能实现。但是,如果我想将函数f(x)作为参数传递,这是否有可能呢?我觉得不行,因为我需要解析字符串(必须像'x0*exp(x1)*ln(x2*x1)'这样通过字符串传递)!谢谢!编辑:我的问题不是关于如何将函数作为指针传递给C语言,而是关于如何评估以字符串形式输入的数学函数。
在C语言中执行函数指针搜索,这应该会对你有所帮助。
编辑:添加一个示例,以便更加明显...
#include <stdio.h>
typedef float (*real_univariate_function)(float x);
float calculate_derivative(real_univariate_function f, float x, float h) {
return (f(x+h)-f(x))/h;
}
float square(float x) {
return x*x;
}
int main() {
printf("derivative for x^2 in 2 with h 0.001 is: %f\n",
calculate_derivative(&square, 2, 0.001));
}
可能会有一些语法错误,我的C语言已经生疏了...