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:
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}
-
\left(y_i - \frac{M_i h_i^2}{6}\right)\frac{x_{i+1}-x}{h_i}
-
\left(y_{i+1} - \frac{M_{i+1}h_i^2}{6}\right)\frac{x - x_i}{h_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:
- Sort the data points (xi,yi) in ascending order by xi.
- Compute hi=xi+1βxi for i=0,β¦,nβ1.
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).
- h0=1,h1=1.
- Boundary conditions: M0=M2=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
- Splines ensure smoothness by producing curves with continuous first and second derivatives, leading to a natural and visually appealing fit.
- The method provides controlled behavior, avoiding the large oscillations often observed in high-degree polynomial interpolation.
- Local control means that changing a single data point only impacts the neighboring spline segments, rather than the entire curve.
Limitations
- The method involves complexity, as it requires setting up and solving a linear system of equations, making it more complicated than simpler interpolation techniques.
- Computational cost is higher than methods like linear interpolation, particularly for large datasets with many control points.
- Boundary conditions must be specified (e.g., natural, clamped) to define the behavior at the endpoints, which can influence the overall fit of the spline.