问题
I am getting some errors when interpolating with RBF. Here is an example in 1D. I think that it has to do with how close my y values are to each other. Is there any fix for this?
import numpy as np
from scipy.interpolate import Rbf, interp1d
import matplotlib.pyplot as plt
x = np.array([0.77639752, 0.8136646, 0.85093168, 0.88819876, 0.92546584, 0.96273292, 1.])
y = np.array([0.97119742, 0.98089758, 0.98937066, 0.99540737, 0.99917735, 1., 0.99779049])
xi = np.linspace(min(x),max(x),1000)
fig = plt.figure(1)
plt.plot(x,y,'ko', label='Raw Data')
#RBF
rbfi = Rbf(x,y, function='linear')
plt.plot(xi,rbfi(xi), label='RBF (linear)')
rbfi = Rbf(x,y, function='cubic')
plt.plot(xi,rbfi(xi), label='RBF (cubic)')
#1D
f = interp1d(x,y, kind='cubic')
plt.plot(xi,f(xi), label='Interp1D (cubic)')
plt.plot(x,y,'ko', label=None)
plt.grid()
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.tight_layout()
plt.savefig('RBFTest.png')
回答1:
Indeed, when implemented properly, RBF interpolation using the polyharmonic spline r^3 in 1D coincides with the natural cubic spline, and is a "smoothest" interpolant.
Unfortunately, the scipy.interpolate.Rbf, despite the name, does not appear to be a correct implementation of the RBF methods known from the approximation theory. The error is around the line
self.nodes = linalg.solve(self.A, self.di)
They forgot the (linear) polynomial term in the construction of the polyharmonic RBF! The system should have been (2).
Now, one shouldn't trust interp1d
blindly either. What algorithm used in interp1d function in scipy.interpolate suggests that it may not be using natural cubic spline but a different condition. No mentioning of it in the help page: one needs to go into the python source, and I'm afraid of what we will find there.
Is there a fix for this?
If it's a serious work, make your own implementation of the RBF interpolation algorithm. Or, if you want to try a different implementation in python, there is apparently one from the University of Michigan: https://rbf.readthedocs.io. If you do, could you post your findings here? If not, you've already did a good service by demonstrating an important SciPy error -- thank you!
来源:https://stackoverflow.com/questions/56820251/unexpected-results-from-scipy-interpolate-rbf