natural spline, unmkpp gives 'wrong' answer

5 次查看(过去 30 天)

I have the following data

x=[-3 -2 1 4 6]
y=[2 0 3 1 4]

And I want to calculate the coefficient of the corresponding natural(!) Spline which is given by

 $s(x_j) = a_0 + a_1*(x-x_j) + a_2*(x-x_j)^2 + a_3*(x-x_j)^3$

I obtain

 |    |     a_3 |     a_2 |     a_1 |    a_0 | intv    |
 | s0 |  0.5044 |       0 | -2.5044 | 2.0000 | [-3,-2] |
 | s1 | -0.2831 |  1.5132 | -0.9912 |      0 | [-2,1]  |
 | s2 |  0.2217 | -1.0351 |  0.4430 | 3.0000 | [1,4]   |
 | s3 | -0.1601 |  0.9605 |  0.2193 | 1.0000 | [4,6]   |

However when I am using matlab

clear all

 x=[-3 -2 1 4 6]
 y=[2 0 3 1 4]
 pspl=spline(x,[0 y 0])% a natural spline 
 [breaks,coefs,l,k,d] = unmkpp(pspl) 

I obtain

 |  2.0595 | -4.0595 |       0 | 2.0000 | 
 | -0.3796 |  2.1190 | -1.9405 |      0 |
 |  0.3003 | -1.2976 |  0.5238 | 3.0000 |
 | -0.5387 |  1.4048 |  0.8452 | 1.0000 |

I don't know how to interpret this, the only terms with coincide are that of the right column which must correspond to the terms $a_j$ but for the rest I don't know.

Any help is appreciated.

Thanks

Uwe Brauer

  2 个评论
Stephen23
Stephen23 2017-2-18
@Uwe Brauer: this time I formatted your code for you. In future you can do it yourself by selecting the code text and clicking the {} Code button.
Uwe Brauer
Uwe Brauer 2017-2-18
sorry and thanks that you reformatted the code. I will keep this in mind!

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2017-2-18
编辑:John D'Errico 2017-2-18
Well, it gives the right answer, to a different problem than the one that you think you posed. :)
So first of all, if you read the help for spline:
"However, if Y contains two more values than X has entries, then the first and last value in Y are used as the endslopes for the cubic spline."
That is NOT the definition of a NATURAL spline, although I can see how one might mistake that easily enough. A natural cubic spline has SECOND derivatives zero at the ends.
Next, the coefficients that you see here:
pspl.coefs
ans =
2.0595 -4.0595 0 2
-0.37963 2.119 -1.9405 0
0.30026 -1.2976 0.52381 3
-0.53869 1.4048 0.84524 1
are listed with the constant term LAST, so in decreasing order. The first element in each row is the coefficient of the cubic term (as you show it, with the knot in that interval subtracted off).
So, if we differentiate the spline ONCE, then evaluate it at the end points, we see:
fnval(fnder(pspl,1),[-3 6])
ans =
0 -6.6613e-16
The first derivatives are indeed zero at the ends, although not the second derivatives.
fnval(fnder(pspl,2),[-3 6])
ans =
-8.119 -3.6548
If your goal is to generate a natural cubic spline, I'll need to think for a second. :) A smoothing spline will suffice, if we crank down on tol.
pspl = spaps(x,y,0)
pspl =
struct with fields:
form: 'B-'
knots: [-3 -3 -3 -3 -2 1 4 6 6 6 6]
coefs: [2 1.1652 -2.174 6.1053 -0.99415 2.5731 4]
number: 7
order: 4
dim: 1
It produces a result in B-spline form, but s we see, it is indeed a natural cubic spline.
fnval(fnder(pspl,2),[-3 6])
ans =
-2.6645e-15 -4.4409e-16
I'm pretty sure there is a direct way to generate a natural cubic spline in pp form, but my mind is drawing a blank at the moment. Drat, I almost forgot about csape.
pp = csape(x,y,'var')
pp =
struct with fields:
form: 'pp'
breaks: [-3 -2 1 4 6]
coefs: [4×4 double]
pieces: 4
order: 4
dim: 1
fnval(fnder(pp,2),[-3 6])
ans =
0 0
fnval(fnder(pp,1),[-3 6])
ans =
-2.5044 2.1404
pp.coefs
ans =
0.50439 0 -2.5044 2
-0.28314 1.5132 -0.99123 0
0.22173 -1.0351 0.44298 3
-0.16009 0.96053 0.2193 1
So a cubic spline in pp form, with natural end conditions. Of course, if you were to use my SLM toolbox, a spline with natural end conditions is one of the many options.
  8 个评论
Michael Reshko
Michael Reshko 2019-4-4
Why does "order" property in your cubic splines shows 4? Shouldn't cubic be order 3?
John D'Errico
John D'Errico 2019-4-5
编辑:John D'Errico 2019-4-5
Why? Whats in a name?
pp = spline(rand(1,5),rand(1,5))
pp =
struct with fields:
form: 'pp'
breaks: [0.16679 0.33499 0.46094 0.51324 0.66433]
coefs: [4×4 double]
pieces: 4
order: 4
dim: 1
Blame it on the designers of splines in MATLAB. See that the CUBIC spline created is shown to have order 4.
Really, a name can indicate anything you want it to indicate. Many people use the words order and degree to be synonyms. My choice was to be consistent with that chosen in MATLAB. I did not write spline, but I need to use it and its cousins.
In there we even see it stated that degree of a univariate polynomial in x would essentially be the highest power of x. As well, it states that order was commonly used in the past as a synonym for degree.
However, here we see a statement about the order of a spline as:
"the order of a spline, either the degree+1 of the polynomials defining the spline or the number of knot points used to determine it."
And here:
"If the n polynomials Pi each have degree at most m, then the spline is said to be of degree m (or is of order m+1)."
A name is just a name. But order and degree have been disambiguated, no longer twins.

请先登录,再进行评论。

更多回答(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