Plot Natural Cubic Spline

9 次查看(过去 30 天)
Does Matlab have a built in code to plot a 'Natural' Cubic spline? I was using pp = spline(... , but have realised this is not a 'natural spline. I have also tried pp = csape(x,y,'second'), where x and y are my nodes, but im not sure this is right either.

采纳的回答

John D'Errico
John D'Errico 2018-3-8
编辑:John D'Errico 2018-3-8
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.
  4 个评论
JJ
JJ 2018-3-8
Yes, thank you. I have it now. Plotting the default one along side the Natural one helped me see what was going on. Thanks again
Joaquín Vidal Peña
BRO MANY THANKS I WAS LOOKING FOR THIS
I wanted to corroborate my work of Cubic Natural Spline by hand, and the command 'spline' didn't seem to be the same.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by