Interpolation with given nodes interpolate.splrep - unexpected result

I cannot figure out how the function works interpolate.splrep

. Simple code throws no errors, but interpolate.splev

returns unexpected results.

degree = 4;
arg = np.linspace(0, 2.0 * np.pi, 1000);
val = np.sin(arg);

m = arg.size - degree - 1;
step = (arg[-1] - arg[0]) / (m + 1);
knots = np.linspace(step, m * step, m);

f = interpolate.splrep(arg, val, k=degree, s=0, t=knots, per=0);

x = 0.123456;
print interpolate.splev(x, f) - np.sin(x);

      

This code prints 2.81341438303e+118

, but if I change the parameter the per

code works very well:

...
f = interpolate.splrep(arg, val, k=degree, s=0, t=knots, per=1);
...

      

Result -1.80411241502e-16

. Can you explain to me what the results are? Not a mistake?

+3


source to share


1 answer


The parameter per

toggles the periodic boundary conditions (pbc). The fit doesn't seem to converge without pbc, but with pbc it works. You can see this also when plotting the data and the spline result: it will only diverge at the boundaries, the rest of the dataset will fit well!

The documentation suggests that having an even degree in combination is not a good idea. You have degree = 4

(even) and s = 0

.

You seem to have a lot of nodes! The B-spline is piecewise fitted. If you have many nodes, each spline function will only be valid for a very small fraction of the points. you have 1000 abscissa points and 995 nodes. The fewer points for which a spline needs to be valid, the more degrees of freedom are left for the function, so if you have multiple points, the spline can be pretty much anything ...

changing the line m = arg.size - degree - 1;

to m = arg.size - degree - 20;

already gives



>>> print interpolate.splev(x, f) - np.sin(x);
3.08053624276e-10

      

without pbc!

So ask yourself why are you counting the number of nodes the way you are doing! Either you stick with it and enter pbc i.e. per=1

, or slightly reduce the number of nodes. Both will stabilize the fit and ensure convergence!

0


source







All Articles