I implemented a C++ code to numerically solve the n-th derivative of a function in a point x_0:
double n_derivative( double ( *f )( double ), double x_0, int n )
{
if( n == 0 ) return f( x_0 );
else
{
const double h = pow( __DBL_EPSILON__, 1/3 );
double x_1 = x_0 - h;
double x_2 = x_0 + h;
double first_term = n_derivative( f, x_2, n - 1 );
double second_term = n_derivative( f, x_1, n - 1);
return ( first_term - second_term ) / ( 2*h );
}
}
I was wondering if this is for you a good implementation or if there can be a way to better write it in C++. The problem is that I noticed that the n-th derivative diverges for values of n higher than 3. Do you know how to solve this?