Curriculum
Course: SCIPY
Login
Text lesson

Spline Interpolation

In 1D interpolation, the points are fitted to a single curve, while in spline interpolation, the points are fitted to a piecewise function defined by polynomials called splines.

The UnivariateSpline() function takes xs and ys as input and produces a callable function that can be used with new xs values.

A piecewise function is a function that has different definitions for different intervals or ranges.

Example

Perform univariate spline interpolation for the range 2.1, 2.2, …, 2.9 using the following nonlinear points.

from scipy.interpolate import UnivariateSpline
import numpy as np

xs = np.arange(10)
ys = xs**2 + np.sin(xs) + 1

interp_func = UnivariateSpline(xs, ys)

newarr = interp_func(np.arange(2.130.1))

print(newarr)

Result:

[5.62826474 6.03987348 6.47131994 6.92265019 7.3939103 7.88514634

8.39640439 8.92773053 9.47917082]