Cubic spline Interpolation (original) (raw)

Last Updated : 9 Jul, 2025

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.

Random points

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.

Linear Interpolation

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 (x_0, y_0), (x_1, y_1), ..., (x_n, y_n), the goal is to fit piecewise cubic polynomials S_i(x) over each interval [x_i, x_{i+1}], such that:

S_i(x) = a_i + b_i(x - x_i) + c_i(x - x_i)^2 + d_i(x - x_i)^3

Let h_i = x_{i+1} - x_i and define second derivatives at the knots as M_i = S''(x_i). From Taylor approximation and spline properties: \frac{y_{i+1} - y_i}{h_i} = \frac{h_i}{6}M_i + \frac{h_i}{3}M_{i+1} + \text{linear term}

Which leads to the spline equation:

\mu_i M_{i-1} + 2M_i + \lambda_i M_{i+1} = d_i

Where:

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 A and B depend on y_i, y_{i+1} and M_i, M_{i+1}​.

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 f(x) =1/(1+x^2)

Python `

#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:

Linear Interpolation

Cubic Spline interpolation

Advantages of Cubic Spline Interpolation

Challenges of Cubic Spline Interpolation

Applications of Cubic Spline Interpolation

  1. **Computer Graphics and Animation: Used for smooth curve modeling and motion paths in design and animation.
  2. **Engineering and CAD: Helps in designing smooth mechanical components, surfaces and motion trajectories.
  3. **Scientific and Statistical Data Fitting: Ideal for creating smooth curves from experimental or observed data in physics, biology, or economics.