I am creating an implementation of a cubic hermite spline in Python. One feature I would like to add is a method to compute the slope (IE the derivative) for a given T value.

Currently, I can do it numerically with the following code:

dt = .00001
def derivative(t):
    dx = my_spline.get_value(t + dt) - my_spline.get_value(t)
    return dx/dt

I would like to do it analytically though - it seems like it should be possible.

Wikipedia defines four basis functions which are multiplied by constants - my first intuition was that I could simply take the derivative of each basis function and leave the constants the same, since they won't affect the derivatives. This doesn't work in practice, and it makes sense why: the first and third constants are based on position, and we don't want position data, we want the derivative of position.

And at this point I'm lost. I've taken a few guesses but none of them have given me the results I expected. How do I do this?


If you're using the curve equation from the wikipedia page you cited: $$ \mathbf{p}(t) = h_{00}(t)\mathbf{p}_0 + h_{10}(t)\mathbf{m}_0 + h_{01}(t)\mathbf{p}_1 + h_{11}(t)\mathbf{m}_1 $$ then it is certainly true that $$ \mathbf{p}'(t) = h_{00}'(t)\mathbf{p}_0 + h_{10}'(t)\mathbf{m}_0 + h_{01}'(t)\mathbf{p}_1 + h_{11}'(t)\mathbf{m}_1 $$ If things are not working for you, it's probably because you have some confusion over which parameter is used in the differentiations.

Look up near the top of the Wikipedia page. They discuss interpolation over the interval $[0,1]$ versus interpolation over a general interval $[t_k, t_{k+1}]$. For these two situations, the Hermite blending functions are different.

If you want to compute the derivative numerically, then you should use

dx = 0.5*( my_spline.get_value(t+dt) - my_spline.get_value(t-dt) )
  • $\begingroup$ Better late than never: The problem must have been something else because this is correct. $\endgroup$ – Elliott Jan 10 '14 at 19: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.