I'm trying to do something like the following (image extracted from wikipedia)
#!/usr/bin/env python
from scipy import interpolate
import numpy as np
import matplotlib.pyplot as plt
# sampling
x = np.linspace(0, 10, 10)
y = np.sin(x)
# spline trough all the sampled points
tck = interpolate.splrep(x, y)
x2 = np.linspace(0, 10, 200)
y2 = interpolate.splev(x2, tck)
# spline with all the middle points as knots (not working yet)
# knots = x[1:-1] # it should be something like this
knots = np.array([x[1]]) # not working with above line and just seeing what this line does
weights = np.concatenate(([1],np.ones(x.shape[0]-2)*.01,[1]))
tck = interpolate.splrep(x, y, t=knots, w=weights)
x3 = np.linspace(0, 10, 200)
y3 = interpolate.splev(x2, tck)
# plot
plt.plot(x, y, 'go', x2, y2, 'b', x3, y3,'r')
plt.show()
The first part of the code is the code extracted from the main reference but it's not explained how to use the points as control knots.
The result of this code is the following image.
The points are the samples, the blue line is the spline taking into account all the points. And the red line is the one that is not working for me. I'm trying to take into account all the intermediate points as control knots but I just can't. If I try to use knots=x[1:-1]
it just doesn't work. I'd appreciate any help.
Question in short: how do I use all the intermediate points as control knots in the spline function?
Note: this last image is exactly what I need, and it's the difference between what I have (spline passing all the points) and what I need (spline with control knots). Any ideas?
knots = x[2:-2]
. The error code of the underlying fortran function indicates that it might be the Schoenberg-Whitney conditions that are violated, but I couldn't wrap my head around it. You can find the implementation here. It's the curfit.f function and the error code isier = 10
. The fortran function is at least well documented. – Buenabuenaventura