Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) (2024)

Piecewise Cubic Hermite Interpolating Polynomial (PCHIP)

collapse all in page

Syntax

p = pchip(x,y,xq)

pp = pchip(x,y)

Description

example

p = pchip(x,y,xq) returnsa vector of interpolated values p correspondingto the query points in xq. The values of p aredetermined by shape-preserving piecewise cubicinterpolation of x and y.

example

pp = pchip(x,y) returnsa piecewise polynomial structure for use with ppval andthe spline utility unmkpp.

Examples

collapse all

Data Interpolation with spline, pchip, and makima

Open Live Script

Compare the interpolation results produced by spline, pchip, and 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 spline, pchip, and 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')

Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) (1)

In this case, pchip and 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, spline and makima capture the movement between points better than pchip, which is aggressively flattened near local extrema.

Interpolation with Piecewise Polynomial Structure

Open Live Script

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])

Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) (3)

Input Arguments

collapse all

xSample points
vector

Sample points, specified as a vector. The vector x specifiesthe points at which the data y is given. The elementsof x must be unique.

Data Types: single | double

yFunction values at sample points
vector | matrix | array

Function values at sample points, specified as a numeric vector,matrix, or array. x and y musthave the same length.

If y is a matrix or array, then the valuesin the last dimension, y(:,...,:,j), are takenas the values to match with x. In that case, thelast dimension of y must be the same length as x.

Data Types: single | double

xqQuery points
scalar | vector | matrix | array

Query points, specified as a scalar, vector, matrix, or array. The points specified in xq are the x-coordinates for the interpolated function values yq computed by pchip.

Data Types: single | double

Output Arguments

collapse all

p — Interpolated values at query points
scalar | vector | matrix | array

Interpolated values at query points, returned as a scalar, vector, matrix, or array. The size of p is related to the sizes of y and xq:

  • If y is a vector, then p has the same size as xq.

  • If y is an array of size Ny = size(y), then these conditions apply:

    • If xq is a scalar or vector, then size(p) returns [Ny(1:end-1) length(xq)].

    • If xq is an array, then size(p) returns [Ny(1:end-1) size(xq).

pp — Piecewise polynomial
structure

Piecewise polynomial, returned as a structure. Use this structurewith the ppval function toevaluate the interpolating polynomials at one or more query points.The structure has these fields.

FieldDescription
form

'pp' for piecewise polynomial

breaks

Vector of lengthL+1withstrictly increasing elements that represent the start and end of eachofLintervals

coefs

L-by-k matrix witheach rowcoefs(i,:)containing the localcoefficients of an orderkpolynomialon theith interval,[breaks(i),breaks(i+1)]

pieces

Number of pieces, L

order

Order of the polynomials

dim

Dimensionality of target

Since the polynomial coefficients in coefs arelocal coefficients for each interval, you must subtract the lowerendpoint of the corresponding knot interval to use the coefficientsin a conventional polynomial equation. In other words, for the coefficients [a,b,c,d] onthe interval [x1,x2], the corresponding polynomialis

f(x)=a(xx1)3+b(xx1)2+c(xx1)+d.

More About

collapse all

Shape-Preserving Piecewise Cubic Interpolation

pchip interpolates usinga piecewise cubic polynomial P(x) withthese properties:

  • On each subinterval xkxxk+1,the polynomial P(x) is a cubic Hermiteinterpolating polynomial for the given data points with specifiedderivatives (slopes) at the interpolation points.

  • P(x) interpolates y,that is, P(xj)=yj, and the first derivative dPdx is continuous. The second derivative d2Pdx2 is probably not continuous sojumps at the xj are possible.

  • The cubic interpolant P(x) is shape preserving. The slopesat the xj are chosen in such a way that P(x) preserves the shape of the dataand respects monotonicity. Therefore, on intervals where the datais monotonic, so is P(x), and at pointswhere the data has a local extremum, so does P(x).

Note

If y is a matrix, P(x) satisfies these properties foreach row of y.

Tips

  • spline constructs S(x) in almost the same way pchip constructs P(x). However, spline choosesthe slopes at the xj differently, namelyto make even S(x) continuous. This differencehas several effects:

    • spline produces a smoother result,such that S(x) is continuous.

    • spline produces a more accurateresult if the data consists of values of a smooth function.

    • pchip has no overshoots and lessoscillation if the data is not smooth.

    • pchip is less expensive to setup.

    • The two are equally expensive to evaluate.

References

[1] Fritsch, F. N. and R. E. Carlson. "MonotonePiecewise Cubic Interpolation." SIAM Journal on NumericalAnalysis. Vol. 17, 1980, pp.238–246.

[2] Kahaner, David, Cleve Moler, Stephen Nash. NumericalMethods and Software. Upper Saddle River, NJ: PrenticeHall, 1988.

Extended Capabilities

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

See Also

interp1 | spline | makima | ppval

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) (4)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) (2024)
Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 6299

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.