# How to compute the derivative of a curve?

Sorry for the naivety of the question, my field is not mathematics. I am writing a script to calculate the derivative of a curve given by a set of (x,y) points as

(x,y)
1,3
2,2
3,7
4,7
5,8
6,12


I calculate the dy/dx for each point from the slope over a unit of x as y(n+1)-y(n) to produce

(x,dy/dx)
2,-1
3,5
4,0
5,1
6,4


Is it mathematically correct? Do I calculate dy/dx correctly?

The given curve is not a mathematical function but an empirical set of data (say x=time, y=temperature). Is it the correct way to calculate the dy/dx for the available set of data?

EDIT: Probably, it is meaningless to calculate the slope between two given points. Instead, it is better to draw a curve based on the best fit. Then, we will have a better approximation of the data points where the slope at each point is meaningful.

Am I right? Should I recalculate the y values based on a best-fit approximation before trying to calculate the slope?

• There are infinitely many curves that pass through your given points $(x,y)$, with wildly varying slopes, so this information is not enough to "determine" $\frac{dy}{dx}$. Perhaps you should explain what you mean to accomplish by calculating the derivative. – symplectomorphic Aug 9 '17 at 21:01
• Include any information you have about the function $y$. What does it represent, what properties does it have? – Carl Christian Aug 9 '17 at 21:03
• This is just a typical set of data, and the script aims to calculate dy/dx for empirical data given to the script. Is there any other way to calculate the derivative for a given set of data? – Googlebot Aug 9 '17 at 21:09
• Are you looking for the discrete derivative? – user121330 Aug 9 '17 at 21:10
• See also this question and numerical differentiation. – Karlo Aug 9 '17 at 21:17

The way to do this is to fit a cubic spline through the data, and then calculate the derivative.

Then use the .Derivative() method to get what you want.

• Great suggestion. I was looking at SciPy for curve fitting. – Googlebot Aug 9 '17 at 22:29

I will assume that the underlying function $y$ is several times differentiable for all $x$ in an interval which covers your range of values. This is a reasonable assumption, as temperatures rarely vary abruptly and certainly not on a microscopic scale.

If this assumption is satisfied, then you can certainly approximate the derivative of $y$ at each of your points using finite difference approximations. It simplify matters that the data points are equidistant on the $x$ axis. Let $h$ denote their constant separation. In your case $h=1$.

If $x$ is an internal point, i.e., both $x-h$ and $x+h$ are data points, then the standard space central approximation of $y'(x)$ is $$y'(x) \approx \frac{y(x+h) - y(x-h)}{2h}$$ The absolute value of the error is $O(h^2)$, i.e. it is bounded by a constant times $h^2$.

If $x$ is an endpoint, say the left end point, then you can either use $$y'(x) \approx \frac{y(x+h)-y(x)}{h},$$ where the error is $O(h)$ or a one side approximation, such as $$y'(x) \approx \frac{-3 y(x) + 4y(x+h) - y(x+2h)}{2h},$$ where the error is $O(h^2)$.

More elaborate approximations are possible and precise error estimates can also be derived, but we either need more information about the properties of $y$ or more data points so that information can be inferred from the data.

• I edited the question. I believe that my problem is that the raw data points are not reliable and should be polished before calculating the derivative. – Googlebot Aug 9 '17 at 21:43
• @All You are quite right. Divided differences are very vulnerable to errors in the input. Fitting a curve to your data may very well be your best option. However, which class of curve to use is very problem dependent. Relevant keywords are: numerical differentiation with noisy data/perturbed data. – Carl Christian Aug 9 '17 at 22:06
• I remember reading that at the molecular level temperature is quantized. – user301988 Aug 20 '17 at 8:06