Plot Natural Cubic Spline

조회 수: 10 (최근 30일)
JJ
JJ 2018년 3월 8일
댓글: Joaquín Vidal Peña 2022년 11월 15일
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
Joaquín Vidal Peña 2022년 11월 15일
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