Chapter 5
Interpolation
The Chapter 5 companion programs show how several interpolation methods discussed in the chapter can be implemented in MATLAB, Python, C++, and Fortran. The resources below connect these examples with widely used numerical libraries and provide test problems, practical guidance for choosing an interpolation method, and selected references.
Software and library resources
The table below gives representative routines and libraries for the interpolation tasks discussed in Chapter 5. Different packages may use different boundary conditions, extrapolation rules, or algorithmic details. Check the linked documentation before choosing a routine for a particular problem.
On a narrow screen, scroll horizontally to view all language columns.
| Numerical task | MATLAB | Python | C++ | Fortran |
|---|---|---|---|---|
| One-dimensional local interpolation Linear and low-order interpolation for tabulated data. |
interp1
|
numpy.interp
for linear interpolation;scipy.interpolate
for higher-order methods
|
GSL interpolation
(C library, callable from C++)
|
NAG E01
interpolation routines
|
| Global polynomial interpolation A single polynomial through all selected data points; companion Lagrange implementations are available in all four languages. |
Chebfun
for stable Chebyshev-based polynomial approximation
|
BarycentricInterpolator
|
gsl_interp_polynomial
(C library, callable from C++)
|
NAG E01
polynomial interpolation
|
| Cubic spline interpolation Piecewise cubic interpolation with continuous first and second derivatives. |
splineinterp1(...,'spline')
|
CubicSplinemake_interp_spline
|
gsl_interp_cspline
|
FMM SPLINE/SEVALPPPACKNAG E01
|
| Shape-preserving piecewise cubic interpolation Useful when overshoot or loss of monotonicity is undesirable. |
pchipmakima
|
PchipInterpolatorAkima1DInterpolator
|
gsl_interp_steffen
or gsl_interp_akima;Boost.Math pchip
or makima
|
Netlib PCHIPNAG E01
monotonic interpolation
|
Spline defaults differ among libraries: MATLAB spline and
SciPy CubicSpline use not-a-knot conditions by default,
GSL cspline is natural, and the companion program uses
FMM-type endpoint conditions based on the first and last four data
points. These choices can produce visible differences near the ends
of an interval.
Test functions and reference behavior
These examples are useful for checking a new interpolation program, comparing methods, or testing changes to the companion codes. Because the underlying functions are known, the interpolation error can be evaluated directly at many points between the data nodes.
| Test problem | Reference behavior | Why it is useful |
|---|---|---|
| p(x) = 1 − 2x + 3x2 − x3 | Any correct polynomial interpolant of degree 3 or higher should reproduce the function to roundoff when four or more distinct exact data points are used. | An exact-reproduction test for polynomial interpolation and a diagnostic for spline endpoint conditions: not-a-knot and FMM-type conditions reproduce the cubic, whereas natural conditions generally do not. |
| f(x) = cos(x), 0 ≤ x ≤ π | The exact function is available everywhere, so pointwise and maximum interpolation errors can be calculated directly. | A smooth test for comparing local polynomials and splines without accidentally matching natural-spline endpoint conditions. |
| f(x) = 1/[1 + 25x2], −1 ≤ x ≤ 1 | With high-degree global interpolation, equally spaced nodes develop strong endpoint oscillations and the maximum error eventually grows; with Chebyshev nodes the interpolants converge as the degree increases. | The classic Runge-function test for studying the effect of node placement. |
| f(x) = 1/[1 + ex/0.05], −0.5 ≤ x ≤ 0.5 | This Fermi-like profile is smooth and monotonic but changes rapidly near x = 0. Use a coarse grid to make overshoot easy to see. | A demanding comparison of ordinary cubic splines with PCHIP or Steffen interpolation, which preserves monotonicity. |
| f(x) = |x|, −1 ≤ x ≤ 1 | The function is continuous but has a derivative discontinuity at x = 0. | Shows that increasing interpolation order does not remove the difficulty created by limited smoothness. |
Choosing an interpolation method
| Problem | Good method to try | Notes |
|---|---|---|
| Only a few neighboring points are needed | Local linear, quadratic, or cubic polynomial interpolation | Low-order local interpolation is simple, inexpensive, and usually more stable than a high-degree global polynomial. |
| Many points describe a smooth function, or smooth derivatives are needed | Cubic spline | A general-purpose, twice continuously differentiable interpolant. Boundary conditions should be chosen with the application in mind. |
| The data are monotonic or overshoot must be avoided | PCHIP or Steffen for guaranteed monotonicity; Akima or makima when reduced overshoot is sufficient | PCHIP and Steffen preserve monotonicity. Akima-type methods usually reduce oscillations but do not provide the same guarantee. |
| A global polynomial is required and node locations can be chosen | Chebyshev nodes with a stable polynomial representation | Clustering nodes near the interval endpoints greatly reduces the endpoint oscillations that can occur with equally spaced points. |
| Many equally spaced points would produce a high-degree global polynomial | Prefer local interpolation or a spline | Adding points does not automatically improve a global polynomial; endpoint oscillations may become worse. |
| Data contain measurement noise or random fluctuations | Curve fitting or smoothing rather than exact interpolation | Exact interpolation follows the noise. Use fitting or smoothing methods when the data are uncertain. |
| A value is required outside the data interval | Extrapolate only with strong physical justification | Extrapolation is much less reliable than interpolation and should be kept close to the known data whenever possible. |
Selected references and documentation
- G. E. Forsythe, M. A. Malcolm, and C. B. Moler, Computer Methods for Mathematical Computations, Prentice-Hall, 1977. The companion spline program follows the SPLINE/SEVAL approach from this work, including its FMM-type endpoint conditions.
- C. de Boor, A Practical Guide to Splines, revised edition, Springer, 2001. A classic reference on spline construction and B-spline representations.
- J.-P. Berrut and L. N. Trefethen, “Barycentric Lagrange Interpolation,” SIAM Review 46 (2004), 501–517. A useful reference on stable evaluation of polynomial interpolants.
- L. N. Trefethen, Approximation Theory and Approximation Practice, extended edition, SIAM, 2019. Includes a modern treatment of polynomial approximation and Chebyshev points.
- F. N. Fritsch and R. E. Carlson, “Monotone Piecewise Cubic Interpolation,” SIAM Journal on Numerical Analysis 17 (1980), 238–246. The original monotone piecewise cubic interpolation algorithm underlying PCHIP.
- M. Steffen, “A Simple Method for Monotonic Interpolation in One Dimension,” Astronomy and Astrophysics 239 (1990), 443–450. A monotonic piecewise cubic method designed to avoid spurious extrema.
- H. Akima, “A New Method of Interpolation and Smooth Curve Fitting Based on Local Procedures,” Journal of the ACM 17 (1970), 589–602. The original local Akima interpolation method.
- MATLAB interpolation documentation
- SciPy interpolation documentation
- GNU Scientific Library: Interpolation
- NAG Library E01: Interpolation
- Netlib SLATEC PCHIP routines