Natural Cubic Spline Simulator Back
Numerical Analysis Simulator

Natural Cubic Spline Interpolation Simulator

Build a natural cubic spline through 6 data points in real time. Compare it with linear interpolation and a theoretical curve, and see why a tridiagonal system produces such smooth curves.

Parameters
y_1 (value at x=1)
y_2 (value at x=2)
y_3 (value at x=3)
y_4 (value at x=4)

Fixed: x_i = 0,1,2,3,4,5, y_0 = 0, y_5 = 25. The defaults lie on y = x², so you can compare against the theoretical curve.

Results
S(2.5) interpolated
Error at x=2.5
M_2 = S''(2)
RMSE over [0,5]
Data points and interpolation curves

Yellow dots = data / blue dashed = linear interpolation / red solid = natural cubic spline / green solid = theoretical y = x² (shown only with default values)

Theory & Key Formulas

On each interval $[x_i, x_{i+1}]$ the spline is a cubic polynomial:

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

Continuity of value, first and second derivative plus the natural condition $S''(x_0)=S''(x_n)=0$ yield a tridiagonal system for $M_i = S''(x_i)$ (with $h_i = x_{i+1} - x_i$):

$$h_{i-1}M_{i-1} + 2(h_{i-1}+h_i)M_i + h_i M_{i+1} = 6\!\left(\frac{y_{i+1}-y_i}{h_i} - \frac{y_i-y_{i-1}}{h_{i-1}}\right)$$

Once the $M_i$ are known, the per-interval coefficients follow:

$$a_i = y_i,\quad c_i = \tfrac{M_i}{2},\quad d_i = \tfrac{M_{i+1}-M_i}{6h_i},\quad b_i = \tfrac{y_{i+1}-y_i}{h_i} - \tfrac{h_i(2M_i+M_{i+1})}{6}$$

Only three diagonals are nonzero, so the system is solved in $O(n)$ by the Thomas algorithm.

What is a natural cubic spline simulator?

🙋
If I just want to connect data points smoothly, why isn't drawing straight lines between them enough?
🎓
Roughly speaking, the linear interpolation (blue dashed line) just connects neighbours with straight segments and develops a sharp corner at every knot. A natural cubic spline (red solid) uses a cubic on each interval and matches value, first derivative and second derivative at each knot, so the curve looks visibly smoother. If you compare the blue dashes and the red curve in the plot above, you can clearly see those corners on the linear one.
🙋
Why does the second derivative matter so much?
🎓
It's a big deal in mechanical design. For cam profiles or automotive body surfaces, a discontinuous second derivative — that is, curvature — produces sudden jumps in acceleration or streaks in light reflections. CAD engineers call this "C² continuity", and natural cubic splines are the simplest class that satisfies it. That's why they show up in every textbook and CAD course as the canonical first example.
🙋
With the defaults, S(2.5) is really close to 6.25. That matches y = 2.5² = 6.25 exactly!
🎓
Sharp eye. Actually it isn't exact — there's a tiny error. A natural cubic spline imposes the artificial condition $S''=0$ at both ends, which doesn't match y=x² (whose true second derivative is 2 everywhere). The "M_2" stat is close to the theoretical 2.0, but the natural boundary distorts the curve slightly near the ends. That's where the name "natural" comes from.
🙋
When I drag y_3 way up, even the far-away parts of the curve move!
🎓
Exactly — that's the global nature of natural cubic splines. Moving one knot recomputes every M_i through the tridiagonal system, so all intervals shift in unison. In CAD that's often a problem, which is why B-splines and Bézier curves (with local control) are preferred when you only want to edit one region of the curve. It's a question of choosing the right tool for the job.

Frequently Asked Questions

The natural condition sets the second derivative to zero at both ends, which makes the math simple. But if the true function has non-zero second derivative at the endpoints (such as y=x²), error grows near the boundaries. Alternatives include the clamped condition (set the first derivative at both ends), the periodic condition (for cyclic data), and not-a-knot (require third-derivative continuity at the first and last interior knots). MATLAB's spline function uses not-a-knot by default.
It is a specialised Gaussian elimination for tridiagonal systems. The forward sweep zeroes out the subdiagonal and produces an upper-triangular form, and the backward sweep recovers the solution. Whereas general Gauss elimination is O(n³), the Thomas algorithm runs in O(n) by exploiting the matrix structure. It also shows up in implicit time integrators for heat conduction (Crank–Nicolson) and many other CAE workflows.
CAD curve and surface modelling, boundary curves in finite-element meshing, interpolation of experimental data, gap-filling in time series, robot trajectory planning, and the description of optical aspheric surfaces all rely on cubic splines. Airfoil sections (such as NACA profiles) and Class-A automotive surfaces use spline-based representations extensively.
A natural cubic spline goes through every point, so noisy data make the curve oscillate. If you want to smooth out measurement noise, use a smoothing spline (which trades exact passage for smoothness) or fit a B-spline by least squares. Whether or not the curve must pass through every point depends entirely on the goal.

Real-world applications

CAD and CAM curve design: Cubic splines are the most fundamental class of smooth curves for mechanical parts and automotive surfaces. NURBS and B-splines, the workhorses of modern CAD, are direct generalisations of the cubic spline and are essential for free-form surface modelling, interference checking and tool-path generation for 5-axis machining.

Interpolating experimental data: Cubic splines are the go-to method for estimating values between a finite set of sensor measurements. Wind-tunnel pressure distributions, 3D reconstruction from CT slices, and temperature profiles all use spline interpolation. The technique is built into MATLAB's spline, SciPy's CubicSpline, Excel's smooth-curve fit and countless other tools.

Motion and trajectory planning: Splines are used to generate smooth time-varying control signals for robot joint angles, CNC tool paths and drone flight trajectories. Because the second derivative is continuous, sudden changes in acceleration — and the shocks and vibrations they cause — are avoided. The same idea drives keyframe interpolation in animation.

Finance and scientific curve fitting: Constructing yield curves in finance, pharmacokinetic concentration profiles, climate time series and many other applications need a continuous curve from discrete observations. Cubic splines are valued for being simple, smooth and predictable, and they often serve as the baseline for more elaborate models.

Common misconceptions and pitfalls

The biggest pitfall is assuming that "interpolation through every point is always best". A spline goes through every data point faithfully, which means it also faithfully reproduces noise and outliers, producing unnatural wiggles. In practice you first have to decide whether interpolation (pass through every point) or approximation (stay close to every point) is appropriate. For noisy observational data, a smoothing spline or least-squares fit is usually the better tool.

A second misconception is that the "natural" boundary condition is universally suitable. The name sounds reassuring, but it imposes the strong constraint that curvature (the second derivative) vanishes at both endpoints. Interpolating a function such as $y=x^2$, whose true curvature is non-zero at the ends, leads to noticeable error near the boundaries. The simulator's RMSE is non-zero even though every default point lies exactly on $y=x^2$ for exactly this reason. MATLAB's default not-a-knot, or the clamped condition with known endpoint slopes, are often more accurate.

Finally, do not assume that cubic splines have local control. If you push y_3 (the middle knot) up and down, the change is not confined to the central region — it propagates all the way to the ends. That is because the Thomas algorithm solves a global linear system, so one perturbation affects every $M_i$. When you need a local edit (for instance, "move one CAD control point without disturbing the rest"), a B-spline or Bézier curve with local basis functions is the right choice. Spline interpolation and spline-based curve editing are different problems.