我想写一个子程序来计算函数的n阶导数,这个函数由形式的子程序:
double func(double x)
{
// a mathematical expression representing a smooth curve
//returns the value of the function at x
}
我编写了以下子例程:
double nthderive(double (*f)(double),double x0,int n)
{
const double delta=1.0e-8;
double x1=x0 - delta;
double x2=x0 + delta;
if(n==0) {
return f(x0);
}
else {
return (nthderive(f,x2,n-1)-nthderive(f,x1,n-1))/(2*delta);
}
}
有没有人能推荐一个更好的算法来求n阶导数?
发布于 2016-09-08 09:20:26
N阶导数可以使用Cauchy's integral lemma计算。
将路径上的积分转换为“标准积分”(参见Line integral)。
然后,您可以集成该表达式(例如,使用trapezoidal rule)。
如果你想计算高阶导数,这种方法很可能是稳定的。
发布于 2016-09-01 13:32:35
不包括数值不稳定问题,这使得Delta
的调整变得困难,并排除了高阶导数(比如n > 3
!)的使用,递归解的效率非常低,因为它需要2^n
函数计算,其中n+1
就足够了。
实际上,您可以计算
f' = f(+1) - f(-1)
f'' = f'(+1) - f'(-1) = f(+2) - f(0) - f(0) + f(-2)
f''' = f''(+1) - f''(-1) = f(+3) - f(+1) - f(+1) + f(-1) - f(+1) + f(-1) + f(-1) - f(-3)
...
当二项式公式告诉你
f' = f(+1) - f(-1)
f'' = f(+2) - 2f(0) + f(-2)
f''' = f(+3) - 3f(+1) + 3f(-1) - f(-3)
...
https://stackoverflow.com/questions/39252556
复制相似问题