Menu Close

Cubic Spline Method | Python

The cubic spline method is a type of interpolation method that is commonly used in numerical analysis to estimate a function (guess the function) that passes through a set of data points.

The method involves constructing a piecewise cubic polynomial that matches the values and first derivatives of the function at the data points, as well as satisfies some continuity conditions between adjacent polynomials.

In Python, we can use the CubicSpline function from the scipy.interpolate module to perform cubic spline interpolation.

The CubicSpline function takes as input two arrays, x and y, that represent the x-coordinates and y-coordinates of the data points, respectively.

It then returns a CubicSpline object that can be used to estimate the value of the function at any point within the interval defined by the data points.

 

Cubic Spline Method in Python:

				
					import numpy as np
from scipy.interpolate import CubicSpline

#PROGRAM BY BOTTOM SCIENCE

# Define the data points
x = np.array([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
y = np.array([1.0000, 1.2214, 1.4918, 1.8221, 2.2255, 2.7183])

# Create a CubicSpline object with the data
cs = CubicSpline(x, y)

# Define the point where we want to estimate the function
x_new = 0.5

# Use the CubicSpline object to estimate the value of the function at x_new
Pn = cs(x_new)

# Print the estimated value of the function at x_new
print(f"Pn({x_new}) = {Pn}")

#Output - Pn(0.5) = 1.6604399999999984


				
			

More Related Stuff