Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Recipe

Construct a natural cubic spline

Use when: you have a table of (x, y) data points and want one smooth, piecewise-cubic curve through all of them, with no extra information available about the slope at either end

Goal: every coefficient a_j,b_j,c_j,d_j of every piece, assembled into the full piecewise definition of S(x)

Taught in Natural boundary conditions

  1. 1

    Arrange the nodes x_0<x_1<\cdots<x_n with their values y_i. Compute each interval width h_j=x_{j+1}-x_j.

    Example

    For (1,3), (2,1), (4,3), (5,10): h_0=2-1=1, h_1=4-2=2, h_2=5-4=1.

    Why: every later formula uses these widths directly. Getting the nodes ordered correctly first is what makes the rest of the recipe mechanical, not error-prone.

  2. 2

    For j=1,\ldots,n-1, compute \alpha_j=\dfrac3{h_j}(y_{j+1}-y_j)-\dfrac3{h_{j-1}}(y_j-y_{j-1}) -- how much the two secant slopes meeting at node j disagree.

    Example

    \alpha_1=\dfrac32(3-1)-\dfrac31(1-3)=3-(-6)=9; \alpha_2=\dfrac31(10-3)-\dfrac32(3-1)=21-3=18.

    Why: this packages exactly the mismatch the spline's curvature has to absorb at each interior node. The bigger the disagreement between nearby slopes, the more bend the curve needs there.

  3. 3

    Write h_{j-1}c_{j-1}+2(h_{j-1}+h_j)c_j+h_jc_{j+1}=\alpha_j for each interior node. Then substitute the natural condition's c_0=0 and c_n=0 directly into the first and last of these equations.

    Example

    j=1: c_0+6c_1+2c_2=9 \;\to\; 6c_1+2c_2=9 (since c_0=0). j=2: 2c_1+6c_2+c_3=18 \;\to\; 2c_1+6c_2=18 (since c_3=0).

    Why: this is Section 3's equation (5), closed by the specific information (or lack of it) this section supplies. Every other boundary choice would close the same interior equations differently.

  4. 4

    Solve the resulting system for c_1,\ldots,c_{n-1}. For a small system, use direct elimination; for a larger one, use the general Thomas algorithm from Section 7. c_0 and c_n are already 0.

    Example

    From 6c_1+2c_2=9 and 2c_1+6c_2=18: solving gives c_1=\dfrac9{16}, c_2=\dfrac{45}{16} (and c_0=c_3=0).

    ⚡ Shortcut

    For only two or three interior unknowns, plain substitution is faster: solve one equation for one variable, then substitute into the other. That's quicker than setting up the full Thomas-algorithm bookkeeping Section 7 introduces — save that machinery for a genuinely long chain of nodes.

    Why: c_j is exactly S''(x_j)/2 — the curvature the spline must carry at each node. It keeps both neighboring pieces' slope and curvature matching there.

  5. 5

    Using every now-known c_j, compute b_j=\dfrac{y_{j+1}-y_j}{h_j}-\dfrac{h_j}3(2c_j+c_{j+1}) and d_j=\dfrac{c_{j+1}-c_j}{3h_j} for each j.

    Example

    b_0=\dfrac{1-3}1-\dfrac13\left(0+\tfrac9{16}\right)=-2-\tfrac3{16}=-\tfrac{35}{16}; similarly b_1=-\tfrac{13}8, b_2=\tfrac{41}8. d_0=\dfrac{9/16-0}{3}=\tfrac3{16}; similarly d_1=\tfrac38, d_2=-\tfrac{15}{16}.

    Why: these formulas came directly from Section 3's equations (3) and (4). Once every c_j is known, no further system-solving is needed to get b_j and d_j.

  6. 6

    Write S_j(x)=a_j+b_j(x-x_j)+c_j(x-x_j)^2+d_j(x-x_j)^3 on [x_j,x_{j+1}] for each j, using a_j=y_j.

    Example

    S(x)=\begin{cases} 3-\tfrac{35}{16}(x-1)+\tfrac3{16}(x-1)^3, & 1\le x\le2\\[2pt] 1-\tfrac{13}8(x-2)+\tfrac9{16}(x-2)^2+\tfrac38(x-2)^3, & 2\le x\le4\\[2pt] 3+\tfrac{41}8(x-4)+\tfrac{45}{16}(x-4)^2-\tfrac{15}{16}(x-4)^3, & 4\le x\le5 \end{cases}

    Why: the spline is genuinely three separate formulas, one per subinterval. Evaluating S anywhere means finding which piece x falls in first, then using only that piece's own coefficients.

Common mistakes

  • Leaving c_0 and c_n as unknowns instead of substituting 0 for them in Step 3. The natural condition is what closes the system — skipping it leaves two equations short, same as Section 3 ended.
  • Using the wrong h in a formula -- h_j is always the width of the interval STARTING at x_j (i.e. x_{j+1}-x_j), never the interval ending there.
  • Evaluating a piece at the wrong local variable -- (x-x_j) must use THAT piece's own left endpoint, never x by itself and never a different piece's node.
  • Assuming natural means the curvature is zero everywhere, not just at the two ends. c_1,\ldots,c_{n-1} are generally nonzero — that's the whole point of solving for them.