Cubic Spline Interpolation is a method used to draw a smooth curve through a set of given data points. Instead of connecting the points with straight lines or a single curve, it fits a series of cubic polynomials between each pair of points. These polynomials join smoothly, making the curve look natural and continuous. This technique is especially useful when we want smooth and accurate results from scattered data.
What is Interpolation
We estimate f(x) for arbitrary x, by drawing a smooth curve through the xi. If the desired x is between the largest and smallest of the xi then it is called interpolation, otherwise, it is called Extrapolation.
Different Types of Interpolation
Linear Interpolation
Linear Interpolation is a way of curve fitting the points by using linear polynomial such as the equation of the line. This is just similar to joining points by drawing a line b/w the two points in the dataset.
Polynomial Interpolation
Polynomial Interpolation is the way of fitting the curve by creating a higher degree polynomial to join those points.
Spline Interpolation
Spline interpolation similar to the Polynomial interpolation x' uses low-degree polynomials in each of the intervals and chooses the polynomial pieces such that they fit smoothly together. The resulting function is called a spline.
Cubic Spline Interpolation
Given data points
S_i(x) = a_i + b_i(x - x_i) + c_i(x - x_i)^2 + d_i(x - x_i)^3
Let
Which leads to the spline equation:
\mu_i M_{i-1} + 2M_i + \lambda_i M_{i+1} = d_i
Where:
\mu_i = \frac{h_i}{h_i + h_{i+1}}, \lambda_i = 1 - \mu_i d_i = \frac{6}{h_i + h_{i+1}} \left( \frac{y_{i+1} - y_i}{h_{i+1}} - \frac{y_i - y_{i-1}}{h_i} \right)
This forms a tridiagonal matrix system to solve for M0, M1, ..., Mn. Once second derivatives Mi are known, the spline on each interval is:
S_i(x) = \frac{M_{i+1}(x - x_i)^3}{6h_i} + \frac{M_i(x_{i+1} - x)^3}{6h_i} + A(x - x_i) + B(x_{i+1} - x)
Where constants
Cubic spline Interpolation Implementation in Python
We will be using the Scipy to perform the linear spline interpolation. We will be using Cubic Spline and interp1d function of scipy to perform interpolation of function
#imports
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import CubicSpline, interp1d
plt.rcParams['figure.figsize'] =(12,8)
x = np.arange(-10,10)
y = 1/(1+x**2)
# apply cubic spline interpolation
cs = CubicSpline(x, y)
# Apply Linear interpolation
linear_int = interp1d(x,y)
xs = np.arange(-10, 10)
ys = linear_int(xs)
# plot linear interpolation
plt.plot(x, y, 'o', label='data')
plt.plot(xs,ys, label="S", color='green')
plt.legend(loc='upper right', ncol=2)
plt.title('Linear Interpolation')
plt.show()
# plot cubic spline interpolation
plt.plot(x, y, 'o', label='data')
plt.plot(xs, 1/(1+(xs**2)), label='true')
plt.plot(xs, cs(xs), label="S")
plt.plot(xs, cs(xs, 1), label="S'")
plt.plot(xs, cs(xs, 2), label="S''")
plt.plot(xs, cs(xs, 3), label="S'''")
plt.ylim(-1.5, 1.5)
plt.legend(loc='upper right', ncol=2)
plt.title('Cubic Spline Interpolation')
plt.show()
Output:
Advantages of Cubic Spline Interpolation
- Smooth and Continuous Curves: Produces a smooth curve with continuous first and second derivatives, ensuring natural transitions between points.
- Accurate Approximation: Provides a better fit than linear or quadratic interpolation, especially when modeling curved data.
- Local Control: Each spline segment only depends on a few data points, so changes in one part don't drastically affect the entire curve.
Challenges of Cubic Spline Interpolation
- Computational Complexity: Requires solving a system of equations, which may be computationally expensive for large datasets.
- Boundary Conditions Sensitivity: Different choices of boundary conditions (natural, clamped, etc.) can affect the shape of the spline.
- Overfitting Risk: If data contains noise, the spline may fit the noise too closely, reducing generalization accuracy.
Applications of Cubic Spline Interpolation
- Computer Graphics and Animation: Used for smooth curve modeling and motion paths in design and animation.
- Engineering and CAD: Helps in designing smooth mechanical components, surfaces and motion trajectories.
- Scientific and Statistical Data Fitting: Ideal for creating smooth curves from experimental or observed data in physics, biology, or economics.