From: lena37 B on
I'm currently using cubic-spline interpolation via the Curve Fitting Toolbox to obtain a piecewise polynomial to a set of data points. My end goal is to be able to reconstruct these piecewise polynomial functions outside of matlab using the coefficients provided in the coefficient structure. However, trying to do it for a single point proved to be unsuccessful and now I'm questioning whether my interpretation of the coefficients is correct. I've assumed that each row of the coefficient matrix corresponded to a region between two consecutive points. So that if I took the first row of coefficients, this would correspond to the cubic function a1*x^3 + a2*x^2 + a3*x + a4 valid in the region between the first two data points, a region indicated by the first two values in the breaks column. However, this hand-computed value (via matlab) and the value that the matlab piecewise polynomial
spits out are not the same, the latter value being the correct value I'm striving to replicate. Any thoughts on what I might be doing wrong?

Thanks,
Yelena


From: John D'Errico on
"lena37 B" <ybagdasa(a)gmail.com> wrote in message <hi31rj$1qv$1(a)fred.mathworks.com>...
> I'm currently using cubic-spline interpolation via the Curve Fitting Toolbox to obtain a piecewise polynomial to a set of data points. My end goal is to be able to reconstruct these piecewise polynomial functions outside of matlab using the coefficients provided in the coefficient structure. However, trying to do it for a single point proved to be unsuccessful and now I'm questioning whether my interpretation of the coefficients is correct. I've assumed that each row of the coefficient matrix corresponded to a region between two consecutive points. So that if I took the first row of coefficients, this would correspond to the cubic function a1*x^3 + a2*x^2 + a3*x + a4 valid in the region between the first two data points, a region indicated by the first two values in the breaks column. However, this hand-computed value (via matlab) and the value that the matlab piecewise polynomial
> spits out are not the same, the latter value being the correct value I'm striving to replicate. Any thoughts on what I might be doing wrong?
>

Suppose I have two splines.

x1 = 0:.2:1;
x2 = x1 + 1000000;
y = sin(pi*x1);

s1 = spline(x1,y);
s2 = spline(x2,y);

Consider the functions s1(x) and s2(x). Do you agree
that these two curves must have EXACTLY the same
fundamental shape? The only difference is that the
second curve is translated over by some amount along
the x axis.

Now, look at the coefficients in these two curves.

s1.coefs
ans =
-3.4689 -0.72507 3.2227 0
-3.4689 -2.8064 2.5164 0.58779
-8.8818e-15 -4.8878 0.97756 0.95106
3.4689 -4.8878 -0.97756 0.95106
3.4689 -2.8064 -2.5164 0.58779

s2.coefs
ans =
-3.4689 -0.72507 3.2227 0
-3.4689 -2.8064 2.5164 0.58779
-1.3323e-14 -4.8878 0.97756 0.95106
3.4689 -4.8878 -0.97756 0.95106
3.4689 -2.8064 -2.5164 0.58779

See that they are essentially identical. Only the break
points differ.

format long g
s1.breaks
ans =
0 0.2 0.4 0.6 0.8 1

s2.breaks
ans =
1000000 1000000.2 1000000.4 1000000.6 1000000.8 1000001

This is because each polynomial segment is evaluated
AFTER you subtract off the lower endpoint of the
corresponding knot interval. This makes the spline
evaluation robust against numerical problems in the
cubic polynomials. If you did not do this, then the
polynomial segments in the curve s2(x) would be
difficult to evaluate accurately, since you would be
cubing numbers on the order of x = 1e6.

HTH,
John