MATLAB: Reconstruct multivariate spline from csapi
csapiCurve Fitting ToolboxMATLABmultivariate spline
Related Solutions
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.coefsans = 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: 1fnval(fnder(pp,2),[-3 6])ans = 0 0fnval(fnder(pp,1),[-3 6])ans = -2.5044 2.1404pp.coefsans = 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.
You don't really want to do so. Yes, you think you might. But you don't.
High order polynomials are easy enough to build, polyfit can generate them. Just use one order less than the number of data points. So two points exactly determine a line, 5 points a quartic polynomial, etc.
That is the good news. The bad news is high order polynomials will almost always cause you to be forced to post a new anguished question, like "Why does my high order polynomial interpolant do insane things?"
In fact, depending on your data, even something relatively low order like a cubic polynomial can cause problems. Those problems typically arise from two things. The first is poorly scaled data. So if your independent variable (I'll call it x) varies over the interval [0,1000], then cubing those numbers will result in values on the order of 1e15 for a 5th order polynomial. Now, when you add and subtract numbers that vary by 15 powers of 10, expect to have numerical problems in double precision arithmetic.
High order polynomials say 15 or so, almost always tend to have numerical problems, because now you are raising your numbers to seriously high powers. The linear algebra needed to compute those coefficients often starts to have problems now, because again, it works in double precision arithmetic. So unless you are very careful about issues like scaling and centering of your data, expect problems.
Next, polynomials always SEEM like a good idea. (Hey, I can think of a lot of things I've done that SEEMED like a good idea at the time.) After all, a Taylor series is just a polynomial, and they can represent almost anything. But the fact is, polynomials are squirrelly things. They squirm around, introducing lots of added extrema to data that never did anything bad.
The classic example is seen here:
x = -10:10;y = 1./(1+x.^2);p = polyfit(x,y,20);Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling asdescribed in HELP POLYFIT. > In polyfit at 75 ezplot(@(x) polyval(p,x),[-10,10])hold onplot(x,y,'ro')
It turns out that the squiggles you see are not due to the warning message, although scaling the data would have allowed it to work without generating a warning message.
And NEVER use a polynomial to extrapolate. Ok, linear extrapolation tends to be moderately safe. Stop there though.
Set('soapboxmode','off')
Best Answer