(Note: This is intended to be a community Wiki.)
Suppose I have a set of points xi = {x0,x1,x2,...xn} and corresponding function values fi = f(xi) = {f0,f1,f2,...,fn}, where f(x) is, in general, an unknown function. (In some situations, we might know f(x) ahead of time, but we want to do this generally, since we often don't know f(x) in advance.) What's a good way to approximate the derivative of f(x) at each point xi? That is, how can I estimate values of dfi == d/dx fi == df(xi)/dx at each of the points xi?
Unfortunately, MATLAB doesn't have a very good general-purpose, numerical differentiation routine. Part of the reason for this is probably because choosing a good routine can be difficult!
So what kinds of methods are there? What routines exist? How can we choose a good routine for a particular problem?
There are several considerations when choosing how to differentiate in MATLAB:
- Do you have a symbolic function or a set of points?
- Is your grid evenly or unevenly spaced?
- Is your domain periodic? Can you assume periodic boundary conditions?
- What level of accuracy are you looking for? Do you need to compute the derivatives within a given tolerance?
- Does it matter to you that your derivative is evaluated on the same points as your function is defined?
- Do you need to calculate multiple orders of derivatives?
What's the best way to proceed?