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


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


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?

  • 1
    $\begingroup$ 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. $\endgroup$ – symplectomorphic Aug 9 '17 at 21:01
  • $\begingroup$ Include any information you have about the function $y$. What does it represent, what properties does it have? $\endgroup$ – Carl Christian Aug 9 '17 at 21:03
  • $\begingroup$ 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? $\endgroup$ – Googlebot Aug 9 '17 at 21:09
  • 1
    $\begingroup$ Are you looking for the discrete derivative? $\endgroup$ – user121330 Aug 9 '17 at 21:10
  • $\begingroup$ See also this question and numerical differentiation. $\endgroup$ – 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.

See scipy.interpolate.CubicSpline

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

| cite | improve this answer | |
  • $\begingroup$ Great suggestion. I was looking at SciPy for curve fitting. $\endgroup$ – 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.

| cite | improve this answer | |
  • $\begingroup$ 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. $\endgroup$ – Googlebot Aug 9 '17 at 21:43
  • $\begingroup$ @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. $\endgroup$ – Carl Christian Aug 9 '17 at 22:06
  • $\begingroup$ I remember reading that at the molecular level temperature is quantized. $\endgroup$ – user301988 Aug 20 '17 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.