Numerical Differentiation (1-indexed)
For a given set, it may be important to determine the rates of change - or higher derivatives - of the information in the set. Unless you have a symbolic formula for the system in question, you will need to approximate these derivatives numerically. The following page looks at two ways to find formulas for numerical approximations to derivatives: using polynomial interpolations and using Taylor series approximations. This page is 1-indexed; for the 0-indexed version, see Numerical Differentiation
Contents
Polynomial Interpolation
One method for determining the numerical approximation to a derivative is to generate a symbolic representation of the data points and then take the analytical derivative of that representation. The following sections demonstrate this process for data sets containing two or three points.
Two Point Approximations
Assume you have two data points, where \(x\) represent the independent values and \(y\) represent the dependent values:
You can write a first-order interpolating polynomial using Newton Polynomials as:
Taking the first derivative yields:
which is the same for any \(x\) between \(x_1\) and \(x_2\).
Generalizing this, you can use any two points in your data set to calculate a derivative using this formula. You must first decide if you are going to use the point you are at and the point behind you or the the point you are on and the point ahead of you. Also, if the independent values are evenly spaced, you can make a simplification in that \(x_j-x_{j-1}=\Delta x\) for all \(j\). In summary:
Note that the formulas cannot be applied at particular points. Specifically, the backwards-looking first derivative does not work at the first point and the forwards-looking first derivative does not work at the last point. In those cases, you will basically use the other formula to produce a result.
Note that two-point models can only yield approximations to the first derivative; the approximation to the second derivative requires more points.
Three Point Approximations
Assume you have three data points, where \(x\) represent the independent values and \(y\) represent the dependent values:
You can write a second-order interpolating polynomial using Newton Polynomials as:
While this can be used for unevenly spaced data, it is more common to look at even spacing; replacing the spacings between neighboring \(x\) values yields the more-manageable:
...under constuction...
Taylor Series
Another way to determine the numerical approximation to derivatives is based on the Taylor Series:
where \(f^{(n)}(x)\) represents the \(n\)th derivative of \(f(x)\). Imagine three discrete points next to each other on a line, one at \(x\) and the other two \(\Delta x\) away on either side.
where the Order operator \(O()\) signifies the leading order of the remaining terms.
From this, we can find approximations to various derivatives of \(f(x)\) at \(x\). Assume that the \(n\)th derivative will be some function which is a weighted sum of the three terms above:
To get the first derivative only, solve the coefficients above to zero out the terms you do not want and keep only the first derivative:
Notice that there is no way to keep the first derivative and get rid of the third derivative. This means that a three-point approximation to the first derivative will have some error, the highest order of which coming from the third derivative. It turns out that three-point second derivative approximations have their highest order error in the fourth derivative term. In any event, the solution to the above system of three equations and three unknowns is:
so that the approximation to the first derivative is:
where the \(\Delta x^2\) error comes from the combination of the \(\Delta x^3\) in front of the third derivative and the \(\Delta x^{-1}\) in the \(a\) and \(c\) terms. Proceeding in the same way to get the second derivative yields:
You can get higher order accuracy by including more points, but you increase computational complexity. You can also use two points to get an approximation, but you sacrifice accuracy. For instance, the two-point backward-facing first derivative uses \(f(x)\) and \(f(x-\Delta x)\) to get the first derivative:
This is true when \(b=-a=\frac{1}{\Delta x}\,\!\), but the approximation becomes
which is now only first order accurate. Trying to use two points for the second derivative would be catastrophic since the error term would end up being larger than the derivative term!