Nielsou Akbrg
Nielsou Akbrg

Reputation: 221

Spline interpolation

I'm having difficulties to perform a spline interpolation on the below set:

import numpy
SOURCE = numpy.array([[1,2,3],[3,4,5], [9,10,11]])
from scipy.interpolate import griddata
from scipy.interpolate import interp1d
input = [0.5,2,3,6,9,15]

The linear interpolation works fine, yet when I replace linear with cubic, I have an error :

f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="linear", axis=0, bounds_error=False)
f(input)

f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="cubic", axis=0, bounds_error=False)
ValueError: The number of derivatives at boundaries does not match: expected 1, got 0+0

How can I perform this cubic interpolation ?

Upvotes: 5

Views: 7318

Answers (1)

Blckknght
Blckknght

Reputation: 104792

Your SOURCE data is too short. A cubic spline needs at least four points to interpolate from, but you're only provide three. If you add one more value to SOURCE, it should work more or less as expected:

>>> SOURCE = numpy.array([[1,2,3],[3,4,5], [9,10,11], [12,13,14]])  # added an extra value
>>> f = interp1d(SOURCE[:,0], SOURCE[:,1:], kind="cubic", axis=0, bounds_error=False)
>>> f(input)
array([[nan, nan],
       [ 3.,  4.],
       [ 4.,  5.],
       [ 7.,  8.],
       [10., 11.],
       [nan, nan]])

Upvotes: 10

Related Questions