Plot Natural Cubic Spline

10 Ansichten (letzte 30 Tage)
JJ
JJ am 8 Mär. 2018
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.

Akzeptierte Antwort

John D'Errico
John D'Errico am 8 Mär. 2018
Bearbeitet: John D'Errico am 8 Mär. 2018
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 Kommentare
JJ
JJ am 8 Mär. 2018
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
Joaquín Vidal Peña am 15 Nov. 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Spline Postprocessing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by