How to compute the derivative of a curve?

1.3k Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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

See scipy.interpolate.CubicSpline

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

3
On

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.