2
$\begingroup$

If we numerically differentiate a given time series data consisting of N points by finite forward difference method, we will have N-1 points corresponding to first derivative. If it is a second derivative, we will have N-2 points and so on.

Let us say for the first derivative

$$ \approx\frac{f(x+\Delta x)-f(x)}{\Delta x} $$

I have searched several books and webpages, but no one explicitly describes what is the corresponding x value for the numerically differentiated value if we wish to plot those N-1 values.

In most science and engineering applications, we will not have an exact formula for f(x). One would use a set of data points ($x_1$, $y_1$), ($x_2$, $y_2$), . . . , ($x_n$, $y_n$) available to describe the functional dependence y = f(x). Many users ignore the $x_1$ and use the remaining $x_i$ for plotting N-1 differentiated points.

Others say that the first differentiated value corresponding to the average of $\frac{x_1+x_2}{2}$ i.e., this belongs to the center of $x_1$ and $x_2$.

What is the mathematically and rigorously correct way of dealing with $N-1$ values for the first derivative and $N-2$ values for the second derivative when we have N x-values? If we wish to plot them, how should we modify the x-coordinates?

EDIT: The reason for interest in the x-coordinates is utilitarian. The reason is that in chemical analysis applications, the derivative is used to locate the inflection points of titraton curve or detect a hidden peak in an over lapped spectrum. In such cases the interest is not in the accuracy of the value of the derivative but its corresponding x-coordinate. For example, in a potentiometric titration curve, the end point of titration is located by the first derivative, the inflection point's x-coordinate is the required volume.

Thanks.

$\endgroup$
5
  • 2
    $\begingroup$ Should point out that numerical differentiation is a classic ill-posed problem, meaning that small errors can cause quite large effects. $\endgroup$ Commented Dec 8, 2020 at 18:26
  • 1
    $\begingroup$ Okay, good to know that numerical differentiation is considered to be an old problem among mathematicians. But despite its shortcomings, it is still widely used. If we use the forward difference formula then the first derivative at x is which x? Nobody talks about it clearly. $\endgroup$
    – ACR
    Commented Dec 8, 2020 at 20:28
  • 2
    $\begingroup$ No one talks about it because there is no definitive way here. One can, however, make smoothness assumptions and use Taylor expansion to prove error bounds. This should be in most books on numerical math. Without assumptions on f nothing meaningful can be said. $\endgroup$
    – Dirk
    Commented Dec 10, 2020 at 18:17
  • $\begingroup$ @AllanMacLeod Indeed, it would almost certainly be better to fit some curve to the data points, and then talk about the derivatives of the model. $\endgroup$ Commented Dec 10, 2020 at 22:14
  • $\begingroup$ I'm wondering why the focus in this question is on inherently numerical instable algorithms for numerical differentiation. What is bad with f.i. using cubic splines? I don't understand. $\endgroup$ Commented Dec 11, 2020 at 12:15

2 Answers 2

2
+50
$\begingroup$

The OP asks for a "reputable source", I would think that Press and Teukolsky's Numerical Recipes [section 5.7 in The Book] qualifies as such. As they explain, if you approximate $f'(x)\approx h^{-1}[f(x+h)-f(x)]$ the truncation error (from higher order terms in the Taylor expansion) is of first order in the small increment $h$. You can improve this to a truncation error of second order by symmetrizing, $f'(x)\approx (2h)^{-1}[f(x+h)-f(x-h)]$.

This can be readily generalized to higher order derivatives, just by repeatedly differentiating each term. The second derivative becomes $$f''(x)=h^{-2}\left[f(x+h)+f(x-h)-2f(x)\right].$$

This is equivalent to $\frac{1}{4}h^{-2}[f(x+2h) + f(x-2h) - 2f(h)]$, as discussed at this MSE question.

$\endgroup$
3
  • $\begingroup$ I think the central difference method can reduce some level of ambiguity here. What I wish to confirm is that if we use $forward$ $difference$, it involves x2 and x1. Once we calculate the derivative, to which point should we start plotting the derivative, x2 or x1. Many people say, this derivative belongs to the average of x1 and x2. On the other hand, if we use central difference method, we need three coordinates (x1,y1), (x2,y2), (x3,y3). If we calculate the first derivative, this derivative should be plotted at x2? Right? $\endgroup$
    – ACR
    Commented Dec 10, 2020 at 21:24
  • 1
    $\begingroup$ That is correct. $\endgroup$ Commented Dec 10, 2020 at 22:20
  • 1
    $\begingroup$ I added the second derivative generalization, higher order derivatives follow similarly. $\endgroup$ Commented Dec 11, 2020 at 9:54
2
$\begingroup$

A variant of the argument in Carlo Beenakker's answer: if the $x_i$ are equispaced points with distance $h$ one from the next, then $$\frac{f(x_{i+1})-f(x_i)}{h} - f'(x_i) = O(h),$$

$$ \frac{f(x_{i+1})-f(x_i)}{h} - f'(\frac{x_{i}+x_{i+1}}{2}) = O(h^2) $$ (for a sufficiently regular $f$).

This suggests that the choice that minimizes the error is assigning to each derivative the $x$ coordinate of the midpoint of the grid segment it was calculated on. (But in the end it's a choice, there is no 'right' or 'wrong' here.)

$\endgroup$
1
  • $\begingroup$ I think that could be the origin of why mid-point is usually suggested. Can we generalize this concept for higher derivatives? For example, if we have the second derivative, we have N-2 calculated derivatives for N- x-coordinates. $\endgroup$
    – ACR
    Commented Dec 10, 2020 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .