Piecewise Cubic Hermite Interpolating Polynomial (PCHIP)
Compare the interpolation results produced by
makima for two different data sets. These functions all perform different forms of piecewise cubic Hermite interpolation. Each function differs in how it computes the slopes of the interpolant, leading to different behaviors when the underlying data has flat areas or undulations.
Compare the interpolation results on sample data that connects flat regions. Create vectors of
x values, function values at those points
y, and query points
xq. Compute interpolations at the query points using
makima. Plot the interpolated function values at the query points for comparison.
x = -3:3; y = [-1 -1 -1 0 1 1 1]; xq1 = -3:.01:3; p = pchip(x,y,xq1); s = spline(x,y,xq1); m = makima(x,y,xq1); plot(x,y,'o',xq1,p,'-',xq1,s,'-.',xq1,m,'--') legend('Sample Points','pchip','spline','makima','Location','SouthEast')
In this case,
makima have similar behavior in that they avoid overshoots and can accurately connect the flat regions.
Perform a second comparison using an oscillatory sample function.
x = 0:15; y = besselj(1,x); xq2 = 0:0.01:15; p = pchip(x,y,xq2); s = spline(x,y,xq2); m = makima(x,y,xq2); plot(x,y,'o',xq2,p,'-',xq2,s,'-.',xq2,m,'--') legend('Sample Points','pchip','spline','makima')
When the underlying function is oscillatory,
makima capture the movement between points better than
pchip, which is aggressively flattened near local extrema.
Create vectors for the
x values and function values
y, and then use
pchip to construct a piecewise polynomial structure.
x = -5:5; y = [1 1 1 1 0 0 1 2 2 2 2]; p = pchip(x,y);
Use the structure with
ppval to evaluate the interpolation at several query points. Plot the results.
xq = -5:0.2:5; pp = ppval(p,xq); plot(x,y,'o',xq,pp,'-.') ylim([-0.2 2.2])
x— Sample points
Sample points, specified as a vector. The vector
the points at which the data
y is given. The elements
x must be unique.
y— Function values at sample points
Function values at sample points, specified as a numeric vector,
matrix, or array.
have the same length.
y is a matrix or array, then the values
in the last dimension,
y(:,...,:,j), are taken
as the values to match with
x. In that case, the
last dimension of
y must be the same length as
xq— Query points
Query points, specified as a scalar, vector, matrix, or array. The points
xq are the x-coordinates
for the interpolated function values
yq computed by
p— Interpolated values at query points
Interpolated values at query points, returned as a scalar, vector, matrix,
or array. The size of
p is related to the sizes of
y is a vector, then
the same size as
y is an array of size
size(y), then these conditions apply:
xq is a scalar or vector, then
xq is an array, then
pp— Piecewise polynomial
Piecewise polynomial, returned as a structure. Use this structure
ppval function to
evaluate the interpolating polynomials at one or more query points.
The structure has these fields.
Vector of length
Number of pieces,
Order of the polynomials
Dimensionality of target
Since the polynomial coefficients in
local coefficients for each interval, you must subtract the lower
endpoint of the corresponding knot interval to use the coefficients
in a conventional polynomial equation. In other words, for the coefficients
[x1,x2], the corresponding polynomial
pchip interpolates using
a piecewise cubic polynomial with
On each subinterval , the polynomial is a cubic Hermite interpolating polynomial for the given data points with specified derivatives (slopes) at the interpolation points.
interpolates y, that is, , and the first derivative is continuous. The second derivative is probably not continuous so jumps at the are possible.
The cubic interpolant is shape preserving. The slopes at the are chosen in such a way that preserves the shape of the data and respects monotonicity. Therefore, on intervals where the data is monotonic, so is , and at points where the data has a local extremum, so does .
If y is a matrix, satisfies these properties for each row of y.
spline constructs in almost the same way
pchip constructs . However,
the slopes at the differently, namely
to make even continuous. This difference
has several effects:
spline produces a smoother result,
such that is continuous.
spline produces a more accurate
result if the data consists of values of a smooth function.
pchip has no overshoots and less
oscillation if the data is not smooth.
pchip is less expensive to set
The two are equally expensive to evaluate.
 Fritsch, F. N. and R. E. Carlson. "Monotone Piecewise Cubic Interpolation." SIAM Journal on Numerical Analysis. Vol. 17, 1980, pp.238–246.
 Kahaner, David, Cleve Moler, Stephen Nash. Numerical Methods and Software. Upper Saddle River, NJ: Prentice Hall, 1988.
Usage notes and limitations:
x must be strictly increasing.
Code generation does not remove
If you generate code for the
pp = pchip(x,y) syntax,
then you cannot input
pp to the
in MATLAB®. To create a MATLAB
pp structure created by the code generator:
In code generation, use
return the piecewise polynomial details to MATLAB.
In MATLAB, use
mkpp to create