You were close, in that csape can do it. (My SLM toolbox also has a natural cubic as an option, but there is no need to go that far.) The proper choice in csape is:
pp = csape(x,y,'variational');
The flag 'variational' comes from the calculus of variations, I assume. The calculus of variations can also be seen to call those corresponding end conditions the "natural" boundary conditions.
'variational' : set end second derivatives equal to zero
(ignoring given end condition values if any)
A natural cubic spline is the one that has zero second derivatives at the ends.
As a check, does it produce what I expect? So here a function that has decidedly NON-zero second derivatives at the ends.
x = linspace(0,2*pi,10);
y = cos(x);
pp = csape(x,y,'variational');
pp2 = fnder(pp,2);
fnplt(pp2)
grid on
Yet as you see, the second derivatives at the ends are forced to zero. This is the classic example that I always used when teaching about splines, as to why a natural cubic spline might often be a poor choice.
So if we look at the fit that arises, compare the natural cubic to that produced by spline.
fnplt(pp,[0 1])
hold on
pps = spline(x,y);
fnplt(pps,[0 1])
In blue is the natural cubic, whereas the green curve is the result of spline, which uses not-a-knot end conditions, generally a safer choice. If we remember these curves are an approximation to cos(x), the green curve is clearly much better.
So while you can find a natural cubic in MATLAB, you need to look deeply enough, and know what you should be looking for. That is in general a good thing in this regard.
Best Answer