Cubic spline interpolation — natural, clamped
WHAT is a cubic spline?
WHY these three conditions? Condition 1 keeps each piece simple. Condition 2 forces the curve through the data. Condition 3 is the magic: matching value, slope and curvature removes visible kinks and makes the curve "fair."
HOW we count the unknowns (the bookkeeping)
There are intervals, each cubic has 4 coefficients → unknowns.
Constraints:
- Interpolation at both ends of each piece: equations.
- Continuity of at interior nodes: .
- Continuity of at interior nodes: .
Total equations.
Deriving the system from scratch (the / moment method)
We work with the second derivatives (called moments). Let .
Step 1 — On , is linear (since is cubic, is degree 1). Why this step? A cubic's second derivative is linear, and a line through is forced:
Step 2 — Integrate twice to recover . Why? Integrating a known gives up to two constants, which we pin down using , :
Why this exact form? The cubic terms integrate ; the linear terms are the integration constants chosen so the values at the endpoints come out to (check: at the cubic terms and cancel leaving ).
Step 3 — Differentiate and impose slope continuity . Why? This is condition 3, the only place the pieces "talk" to each other. Working it out gives the master recurrence:
That's equations for moments . Two more needed → boundary conditions.
The two boundary types

Worked Example 1 — Natural spline, 3 points
Data: . Equal spacing .
Step 1. Natural ⇒ . Why? Natural condition zeroes end curvatures, leaving only unknown.
Step 2. Use the interior equation at : Plug numbers: So Why this step? It's the master recurrence; RHS is (right slope left slope) .
Step 3. Build on with :
= -\tfrac{1}{2}x^3 + 0 + \Big(1+\tfrac12\Big)x = -\tfrac12 x^3+\tfrac32 x.$$ *Why?* Substituting into the Step-2 formula with $h=1$. Check: $S_0(0)=0$✓, $S_0(1)=-\tfrac12+\tfrac32=1$✓, $S_0''(0)=0$✓ (natural). --- ## Worked Example 2 — Clamped spline, same points Data $(0,0),(1,1),(2,0)$, clamped with $f_0'=0,\ f_2'=0$ (flat ends). **Step 1.** Two boundary equations + one interior equation give a $3\times3$ system in $M_0,M_1,M_2$. Left boundary: $2h_0M_0+h_0M_1=6(\frac{y_1-y_0}{h_0}-f_0')=6(1-0)=6.$ Right boundary: $h_1M_1+2h_1M_2=6(f_2'-\frac{y_2-y_1}{h_1})=6(0-(-1))=6.$ Interior: $M_0+4M_1+M_2=-12$ (from Example 1). **Step 2.** Solve the $3\times3$ system: - Left boundary: $2M_0+M_1=6$. - Right boundary: $M_1+2M_2=6$. - Interior: $M_0+4M_1+M_2=-12$. By symmetry of the data, guess $M_0=M_2$. From the left boundary $M_0=(6-M_1)/2$. Substitute $M_0=M_2=(6-M_1)/2$ into the interior equation: $$\frac{6-M_1}{2}+4M_1+\frac{6-M_1}{2}=-12 \;\Rightarrow\; (6-M_1)+4M_1=-12 \;\Rightarrow\; 6+3M_1=-12 \;\Rightarrow\; M_1=-6.$$ Then $M_0=M_2=(6-(-6))/2=6.$ *Why?* Clamping forces extra curvature at the ends ($M_0=M_2=6$) to bend the slope to zero — contrast with natural where $M_0=0$. The clamped curve is "stiffer" at the ends and matches the imposed zero slope exactly. --- > [!recall]- Active recall — cover the answers > - Why not one big polynomial? ::: Runge oscillation; splines stay local and smooth. > - How many extra conditions do we need and why? ::: 2; because $4n$ unknowns vs $4n-2$ equations. > - What does natural impose? ::: $M_0=M_n=0$ (zero end curvature). > - What does clamped impose? ::: Given end **slopes** $S'(x_0),S'(x_n)$. > - What quantity does the tridiagonal system solve for? ::: The moments $M_i=S''(x_i)$. --- ## Common mistakes (Steel-man + fix) > [!mistake] "Clamped means I fix the end *values*." > **Why it feels right:** "Clamp" sounds like nailing the height down. **The truth:** values are *always* fixed by interpolation. Clamped fixes the **first derivative (slope)** at the ends. *Fix:* remember clamp = held at an angle. > [!mistake] "Natural spline gives the most accurate curve." > **Why it feels right:** "natural" sounds physically ideal. **Truth:** natural is best only when nothing is known about boundary slopes; if true slopes are available, **clamped** is more accurate near the ends and removes boundary error. *Fix:* use info you have. > [!mistake] Forgetting the $\frac{M h}{6}$ correction in the linear terms. > **Why it feels right:** the linear interpolant alone "looks done." **Truth:** without subtracting $M h/6$ the formula won't reproduce $y_i$ at the nodes. *Fix:* verify $S_i(x_i)=y_i$ every time. --- > [!recall]- Feynman (explain to a 12-year-old) > Imagine bending a thin flexible stick so it touches a row of pins (your data points). The stick can't kink — it has to curve smoothly from one pin to the next. Each little stretch between two pins is described by a smooth cubic curve, and where two stretches meet, the heights, the tilt, and the *bendiness* all match so you never see a corner. A **natural** stick is left straight at its two free ends. A **clamped** stick is held at a fixed tilt at each end, like gripping the ends with your fingers. > [!mnemonic] Remember > **"Natural = No curvature ($M$=0); Clamped = Controlled slope ($S'$ given)."** > Both start with the same letter as the thing they set to a known value (Natural→zero, Clamped→slope... *C for Controlled derivative*). --- ## #flashcards/maths What property makes spline pieces $C^2$? ::: Value, first derivative, and second derivative all continuous at interior nodes. Define the moment $M_i$ in spline interpolation. ::: $M_i = S''(x_i)$, the second derivative at node $i$. Write the natural boundary condition. ::: $M_0 = 0$ and $M_n = 0$. Write the clamped boundary condition. ::: $S'(x_0)=f_0'$ and $S'(x_n)=f_n'$ (fixed end slopes). Why is the spline linear system tridiagonal? ::: Each interior equation links only $M_{i-1},M_i,M_{i+1}$. RHS of the interior spline equation represents what? ::: $6\times$ (difference of adjacent secant slopes) ≈ curvature. How many equations short are the smoothness conditions, requiring boundary conditions? ::: 2 ($4n$ unknowns vs $4n-2$ equations). Why prefer splines over a single high-degree polynomial? ::: They avoid Runge oscillation and stay local. On $[x_i,x_{i+1}]$ why is $S''$ linear? ::: Because $S$ is cubic so its second derivative is degree 1. When is clamped more accurate than natural? ::: When the true boundary slopes are known. --- ## Connections - [[Runge phenomenon]] — the problem splines solve. - [[Lagrange interpolation]] — global polynomial alternative. - [[Tridiagonal systems & Thomas algorithm]] — how to solve the moment system fast. - [[Hermite interpolation]] — also uses derivative data like clamped splines. - [[Finite differences]] — RHS curvature term is a second difference. - [[B-splines]] — basis-function generalization. ## 🖼️ Concept Map ```mermaid flowchart TD R[Runge oscillation] -->|motivates| PW[Piecewise cubics] PW -->|defines| CS[Cubic spline S x] CS -->|interpolates| INT[S xi equals yi] CS -->|smoothness| C2[C2 continuity of S S' S''] INT -->|gives| EQ[4n minus 2 equations] C2 -->|gives| EQ PW -->|has| UNK[4n unknowns] UNK -->|short by 2| BC[2 boundary conditions] BC -->|choice yields| NAT[Natural spline] BC -->|choice yields| CLA[Clamped spline] C2 -->|derived via| MOM[Moment method Mi equals S'' xi] MOM -->|slope continuity| TRI[Tridiagonal system] ``` ## 🔊 Hinglish (regional understanding) > [!intuition]- Hinglish mein samjho > Dekho, spline ka idea bahut simple hai. Agar tum saare data points ko ek hi bade polynomial se jodne ki koshish karoge, to curve beech-beech mein pagal ho jaata hai (Runge oscillation). Iske badle, hum har do consecutive points ke beech ek alag chhota cubic banate hain, aur unko aise jodte hain ki joint pe height, slope aur curvature — teeno match karein. Isi smoothness ki wajah se curve ekdum smooth flexible ruler jaisa dikhta hai, bina kisi kink ke. > > Maths mein hum second derivatives ko $M_i$ kehte hain (moments). Inko ek tridiagonal system se solve karte hain, jiska right-hand side basically do adjacent slopes ka difference hota hai — yani curvature ka approximation. Lekin yahan ek twist hai: humare paas $4n$ unknowns hain par sirf $4n-2$ equations. Matlab 2 conditions kam pad gayi. Yahi 2 boundary conditions decide karti hain ki spline **natural** hai ya **clamped**. > > **Natural** ka matlab: ends pe curvature zero, yani $M_0=M_n=0$. Ruler ko ends pe free chhod diya, seedha. Yeh tab use karo jab boundary ki slope ke baare mein kuch nahi pata. **Clamped** ka matlab: ends pe slope fix kar di ($S'$ given). Ruler ko ends pe ek fixed angle pe pakad liya. Agar tumhe true slope pata hai, to clamped zyada accurate hota hai, especially boundary ke paas. > > Common galti: log sochte hain clamped matlab end ki value fix karna — galat! Value to interpolation se hamesha fix hoti hai; clamped slope fix karta hai. Bas yeh yaad rakho: **Natural = No curvature, Clamped = Controlled slope.** Exam mein moment method se tridiagonal banao, boundary conditions lagao, solve karo, bas ho gaya. ![[audio/4.8.12-Cubic-spline-interpolation-—-natural,-clamped.mp3]]