Mathematica has two ways to integrate:
But what about
Derivative are for symbolic differentiation.
How can I differentiate a function numerically?
There are two rather different scenarios for numerical derivatives:
For scenario 1, here is an example function and its derivative:
For scenario 2, here I discretize the above function to get a table of values in some interval:
Now the derivative can be taken using
Here, I took the list
After doing the derivative, I remove the padding by using a negative argument in
As others mentioned, there's the
It's a bit less widely known that
Let's create a numerical black box function:
Update: Please also see a discussion by @acl concluding that the difference between
Both approaches are easily generalized; the Lanczos method can be generalized to arbitrary integer-order derivatives, and the Cauchy method can be generalized to arbitrary complex-order derivatives. I won't be discussing these generalizations further in this answer, tho.
It's not too hard to implement the Richardsonian method I alluded to in my MO answer, but the routine is somewhat longer; I'll edit this answer to include it if there's interest.
There is another possibility, and that is to differentiate an
Here is an example of a rather poorly behaving function whose derivative we can recover by this technique. The transfer function of a Butterworth filter looks like
The symbolic derivative of this thing, which is group delay for the filter, bogusly produces
We can get a better-behaved function by adding
But now we have a "dimple" where we had the branch cut, and, sure enough,
Build an interpolation function from it:
and differentiate that (notice carefully the tick mark in the following expression, a convenient shorthand for derivatives of functions of single variables):
I present here another approach to numerically differentiating a function $f(x)$. While the methods in my other answer approximate $f^\prime(x)$ at a single specified value, the following method constructs an approximate function $p(x)$ such that $p(x)\approx f(x)$ within a given interval $[a,b]$, and then proceeds to directly generate $p^\prime(x)$.
The key here is to replace the function with its Chebyshev series, and then differentiate that series. This is similar to what is done in the Chebfun package for MATLAB, and previously presented in a number of answers by Michael E2 and yours truly.
To recall, this is how one might derive the coefficients of the Chebyshev series of a function over a given interval (as previously shown here):
Then, one can either take the dot product of the Chebyshev coefficients
Now, one might be tempted to use
Now to generate the Chebyshev coefficients of the first derivative, do this:
This approach is easily extensible to arbitrary order derivatives, just by multiplying