I need to use a cubic spline (I am mainly interested in higher order derivatives) and I found this example from scipy https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.interpolate.CubicSpline.html (the first one with a sin function). They have this line: plt.plot(xs, cs(xs, 2), label="S''")
, which I assume is supposed to plot the second order derivative. But the second derivative of sin is -sin while their function does look at all as -sin. What am I missing here? And how can I actually get the second order derivative? Thank you!
Asked
Active
Viewed 2,000 times
2

JohanC
- 71,591
- 8
- 33
- 66

JohnDoe122
- 638
- 9
- 23
-
"But the second derivative of sin is -sin while their function does look at all as -sin." ? Do you mean "not at all"? – Joe Nov 17 '19 at 09:28
-
Do you mean because it is partially linear? – Joe Nov 17 '19 at 09:29
-
1They approximate 9 points with piecewise cubic functions. The first derivative will be piecewise quadratic functions. The second derivative will be piecewise linear. The third derivative will be a piecewise step function. The plots illustrate this perfectly. (The fourth derivative would be zero everywhere and undefined at the steps.) – JohanC Nov 17 '19 at 11:30
-
Cubic splines are meant to provide C^2 continuity, which promise the polyline fulfils. Then the 3rd derivative is not continuous (but it was not a promise/goal either) – tevemadar Nov 17 '19 at 18:22
-
To answer to your second question "how to get the actual derivative?". Well, what you see are the actual derivatives of the cubic spline. The actual derivatives of the approximated function is quite hard with only 9 points, especially if there are measuring errors. – JohanC Nov 17 '19 at 18:37
-
If the real question is how to find inflection points, https://stackoverflow.com/a/35851327/12046409 might help – JohanC Nov 17 '19 at 18:38
1 Answers
4
A cubic has the form
y = ax³ + bx² + cx + d
the first derivative is
y' = 3ax² + 2bx + c
and the second derivative
y'' = 6ax + 2b
the third derivative is a constant
y''' = 6a
A cubic spline is composed by joining cubics and this means that the second derivative of a cubic spline will be composed of straight lines.

6502
- 112,025
- 15
- 165
- 265