Wolfram Function Repository
Instantuse addon functions for the Wolfram Language
Function Repository Resource:
Find a numerical approximation to a function derivative near the specified point
ResourceFunction["NDerivative"][expr,x,x_{0}] gives a numerical approximation to the derivative of expr with respect to x at the point x_{0}. 

ResourceFunction["NDerivative"][expr,{x,n},x_{0}] gives a numerical approximation to the n^{th} derivative of expr. 
Take the numerical derivative of an exponential:
In[1]:= 

Out[1]= 

Take the numerical derivative of a trig power:
In[2]:= 

Out[2]= 

The expression and evaluation point may be complex valued:
In[3]:= 

Out[3]= 

In[4]:= 

Out[4]= 

NDerivative threads over its first argument:
In[5]:= 

Out[5]= 

Use the default Method→"EulerSum" when expr is not analytic in the neighborhood of x_{0}:
In[6]:= 

Out[6]= 

Check:
In[7]:= 

Out[7]= 

An incorrect answer is obtained with Method→NIntegrate:
In[8]:= 

Out[8]= 

Here is a derivative where the default method works poorly:
In[9]:= 

Out[9]= 

The correct answer is:
In[10]:= 

Out[10]= 

In this case the expression is analytic, so Method→NIntegrate will work well:
In[11]:= 

Out[11]= 

Use Scale→s to capture the region of variation:
In[12]:= 

Out[12]= 

The scale of variation is around .01:
In[13]:= 

Out[13]= 

A value of Scale→s that is too large can be compensated by increasing the number of terms:
In[14]:= 

Out[14]= 

Use Scale to specify directional derivatives. The left and right derivatives of the nonanalytic function x:
In[15]:= 

Out[15]= 

In[16]:= 

Out[16]= 

Check:
In[17]:= 

Out[17]= 

Complex directions may also be specified:
In[18]:= 

Out[18]= 

Check:
In[19]:= 

Out[19]= 

Use the option Scale to avoid regions of nonanalyticity when the method used is NIntegrate:
In[20]:= 

Out[20]= 

Shrinking the radius avoids the essential singularity at x⩵1:
In[21]:= 

Out[21]= 

Check:
In[22]:= 

Out[22]= 

Increasing the number of terms may improve accuracy. Here is a somewhat inaccurate approximation:
In[23]:= 

Out[23]= 

Check:
In[24]:= 

Out[24]= 

Increasing the number of terms produces a more accurate answer:
In[25]:= 

Out[25]= 

Increasing the number of terms further can produce nonsense due to numerical instability:
In[26]:= 

Out[26]= 

Combining an increase in the number of terms with a higher working precision often will reduce the error:
In[27]:= 

Out[27]= 

Highorder derivatives with Method→"EulerSum" experience significant subtractive cancellation:
In[28]:= 

Out[28]= 

Using a higher working precision and additional terms produces an accurate answer:
In[29]:= 

Out[29]= 

For this problem, Method→NIntegrate with default options produces a correct answer:
In[30]:= 

Out[30]= 

Higherorder derivatives will again experience numerical instability:
In[31]:= 

Out[31]= 

Increasing WorkingPrecision will improve the accuracy:
In[32]:= 

Out[32]= 

An alternative is to increase the radius of the contour of integration:
In[33]:= 

Out[33]= 

NDerivative is useful for differentiating functions that are only defined numerically. Here is such a function:
In[34]:= 

Here is the derivative of f[a,b][t] with respect to b evaluated at {a,b,t}={1,2,1}:
In[35]:= 

Out[35]= 

NDerivative can be used as an aid in developing and testing a more robust function for finding the derivative:
In[36]:= 

Check:
In[37]:= 

Out[37]= 

The option Method→NIntegrate uses Cauchy's integral formula to compute derivatives:
In[38]:= 

Out[38]= 

The equivalent computation can be performed using NResidue:
In[39]:= 

Out[39]= 

The Wolfram Language has builtin code to compute derivatives of numerical functions:
In[40]:= 

The builtin numerical derivative code can be used. However, it is unable to capture the rapid oscillations:
In[41]:= 

Out[41]= 

The correct answer:
In[42]:= 

Out[42]= 

Using NDerivative with the appropriate options can compute an accurate derivative:
In[43]:= 

Out[43]= 

In[44]:= 

Out[44]= 

With Method→"EulerSum", NDerivative must be able to evaluate expr at the point x_{0}:
In[45]:= 

In[46]:= 

Out[46]= 

Adding an additional definition for f allows NDerivative to compute the derivative:
In[47]:= 

Out[47]= 

In[48]:= 

Out[48]= 

Check:
In[49]:= 

Out[49]= 

In this case, Method→NIntegrate produces a more accurate answer:
In[50]:= 

Out[50]= 

Some fractional and complex derivatives can be computed:
In[51]:= 

Out[51]= 

In[52]:= 

Out[52]= 

Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License