Last modified: July 26, 2024

This article is written in: πŸ‡ΊπŸ‡Έ

Cubic Spline

Cubic spline interpolation is a refined mathematical tool frequently used within numerical analysis. It's an approximation technique that employs piecewise cubic polynomials, collectively forming a cubic spline. These cubic polynomials are specifically engineered to pass through a defined set of data points, hence striking a balance between overly simple (like linear) and overly intricate (like high-degree polynomial) interpolations.

Conceptual Illustration:

Imagine you have a set of points on a 2D plane. A cubic spline will "thread" through these points, forming a smooth curve that does not exhibit sudden bends or wiggles:

cubic_spline_interpolation

The spline is constructed from piecewise cubic segments that meet at the data points with continuous first and second derivatives, ensuring a visually natural and mathematically smooth interpolation.

Mathematical Formulation

We start with a set of n+1n+1 data points:

(x0,y0),(x1,y1),…,(xn,yn)(x0,y0),(x1,y1),…,(xn,yn)

with x0<x1<β‹―<xnx0<x1<β‹―<xn.

A cubic spline is a function S(x)S(x) defined piecewise on each interval [xi,xi+1][xi,xi+1], i=0,1,…,nβˆ’1i=0,1,…,nβˆ’1:

Si(x)=ai+bi(xβˆ’xi)+ci(xβˆ’xi)2+di(xβˆ’xi)3Si(x)=ai+bi(xβˆ’xi)+ci(xβˆ’xi)2+di(xβˆ’xi)3

where ai,bi,ci,diai,bi,ci,di are the unknown coefficients for the cubic polynomial on the interval [xi,xi+1][xi,xi+1].

Key Requirements:

I. Interpolation Condition:

Each segment must pass through the given data points:

Si(xi)=yi,Si(xi+1)=yi+1.Si(xi)=yi,Si(xi+1)=yi+1.

II. Continuity of the Spline:

The function S(x)S(x) must be continuous at the interior points:

Si(xi+1)=Si+1(xi+1).Si(xi+1)=Si+1(xi+1).

III. Continuity of First Derivative:

The first derivatives of adjacent segments must match:

Sβ€²i(xi+1)=Sβ€²i+1(xi+1).

IV. Continuity of Second Derivative:

Similarly, the second derivatives must also be continuous:

Sβ€³i(xi+1)=Sβ€³i+1(xi+1).

V. Boundary Conditions:

For a natural cubic spline, the second derivatives at the boundaries are set to zero:

Sβ€³0(x0)=0,Sβ€³nβˆ’1(xn)=0.

These conditions lead to a system of linear equations that determine the coefficients ai,bi,ci, and di.

Derivation of the Coefficients

I. Divided Differences and Step Sizes:

Let the spacing between consecutive points be:

hi=xi+1βˆ’xi,i=0,1,…,nβˆ’1

II. Formulation in Terms of Second Derivatives:

Let Mi=Sβ€³(xi). If we can determine Mi for all i, we can write each piece Si(x) as:

$$S_i(x) = M_{i}\frac{(x_{i+1}-x)^3}{6h_i} + M_{i+1}\frac{(x - x_i)^3}{6h_i}

This form ensures the correct boundary conditions and continuity of derivatives once Mi are found.

III. System of Equations for Mi:

Using the conditions for continuity of first and second derivatives, one obtains a tridiagonal system of equations in terms of the unknown second derivatives Mi. For natural splines, we have:

M0=0,Mn=0.

The remaining Mi's (for i=1,…,nβˆ’1) are found by solving this system:

ΞΌiMiβˆ’1+2Mi+Ξ»iMi+1=di, where ΞΌi,Ξ»i,di depend on the data points and hi.

IV. Once Mi are determined, the coefficients ai,bi,ci,di of the cubic spline in the standard form:

Si(x)=ai+bi(xβˆ’xi)+ci(xβˆ’xi)2+di(xβˆ’xi)3

can be computed directly:

ai=yi

bi=yi+1βˆ’yihiβˆ’hi3(2Mi+Mi+1)

ci=Mi

di=Mi+1βˆ’Mi3hi.

Algorithm Steps

I. Data Preparation:

II. Form the Equations:

Set up the linear system to solve for the second derivatives Mi. For a natural spline:

M0=0,Mn=0

For i=1,…,nβˆ’1:

hiβˆ’1Miβˆ’1+2(hiβˆ’1+hi)Mi+hiMi+1=3(yi+1βˆ’yihiβˆ’yiβˆ’yiβˆ’1hiβˆ’1)

III. Solve the Tridiagonal System:

Use an efficient method (like the Thomas algorithm) to solve for M1,M2,…,Mnβˆ’1.

IV. Calculate the Coefficients:

With Mi known, compute:

ai=yi,bi,ci=Mi,di

as described above.

V. Interpolation:

For a given x, find the interval [xi,xi+1] such that xi≀x≀xi+1 and evaluate:

Si(x)=ai+bi(xβˆ’xi)+ci(xβˆ’xi)2+di(xβˆ’xi)3

Example

Consider three points: (0,0),(1,0.5),(2,0).

We form the equation for i=1:

1β‹…M0+2(1+1)M1+1β‹…M2=3((0βˆ’0.5)/1βˆ’(0.5βˆ’0)/1)=3(βˆ’0.5βˆ’0.5)=βˆ’3.

Since M0=M2=0:

4M1=βˆ’3⟹M1=βˆ’0.75

Then:

a0=y0=0,c0=M0=0

b0=(y1βˆ’y0)/h0βˆ’h0(2M0+M1)/3=0.5/1βˆ’(2β‹…0+(βˆ’0.75))/3=0.5+0.25=0.75

d0=(M1βˆ’M0)/(3h0)=(βˆ’0.75βˆ’0)/3=βˆ’0.25

And similarly for i=1.

This gives piecewise polynomials smoothly interpolating the given points.

Advantages

Limitations

Table of Contents

    Cubic Spline
    1. Mathematical Formulation
    2. Derivation of the Coefficients
    3. Algorithm Steps
    4. Example
    5. Advantages
    6. Limitations