MATLAB: Spline Interpolation with derivative condition for knots

curve fittingCurve Fitting Toolboxcurvessplines

I have been using "spline" function in MATLAB to generate splines, although it is only possible to enforce derivative conditions at the first and final knot of the spline like this,
x = -4:4;
y = [0 .15 1.12 2.36 2.36 1.46 .49 .06 0];
cs = spline(x,[0 y 0]);
How can derivative conditions for each knot (x and y) be specified while evaluating the spline?
Any help will be greatly appreciated. 🙂

Best Answer

Technically, this is impossible to accompish, at least in terms of a cubic spline.
A cubic spline is twice continuously differentiable across the breaks. For n data points, we require the spline to pass through the data points, and be continuous across the breaks, as well as have a continuous derivative, but also to have a continuous second derivative. All of this leaves 2 degrees of freedom, allowing you to choose exactly TWO end conditions to resolve the ambiguity. Typically, the best choice is arguably the not-a-knot end conditions, however, a not uncommon choice is the natural end conditions, thus zero second derivatives at the ends. (That is usally not as good a choice as not-a-knot though.)
However, you want to enfore a spline that passes through n points, as well as specifying the first derivative at each knot? The cost will be that you can no longer enforce second derivative continuity. You will no longer have a spline.
As such, the curve will be less smooth than is a classical spline. There are tools in MATLAB that build interpolants of this general form. For example, pchip is one. A pchip interpolant is not twice differentiable across the breaks. It chooses a set of derivatives at the breask to enforce local monotonicity instead.
So you essentially would have two options. The first is to create an interpolant (like pchip) that is piecewise cubic, but lacks second derivative continuity. Or, you can build a quintic (degree 5) spline, which has sufficient flexibility to allow you to choose the first derivatives, and still be smooth. (In either case, I'd probably use mkpp in the end.)
Related Question