# pchip

Piecewise Cubic Hermite Interpolating Polynomial (PCHIP)

## Syntax

`yi = pchip(x,y,xi)pp = pchip(x,y)`

## Description

`yi = pchip(x,y,xi)` returns vector `yi` containing elements corresponding to the elements of `xi` and determined by piecewise cubic interpolation within vectors `x` and `y`. The vector `x` specifies the points at which the data `y` is given, so `x` and `y` must have the same length. If `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 `x`. If `y` has `n` dimensions, then output `yi` is of size ```[size(y,1) size(y,2) ... size(y,n-1) length(xi)]```. For example, if `y` is a matrix, then `yi` is of size ```[size(y,1) length(xi)]```.

`pp = pchip(x,y)` returns a piecewise polynomial structure for use by `ppval`. `x` can be a row or column vector. `y` is a row or column vector of the same length as `x`, or a matrix with `length(x)` columns.

`pchip` finds values of an underlying interpolating function $P\left(x\right)$ at intermediate points, such that:

• On each subinterval ${x}_{k}\le x\le {x}_{k+1}$, is the cubic Hermite interpolant to the given values and certain slopes at the two endpoints.

• $P\left(x\right)$ interpolates y, i.e., $P\left({x}_{j}\right)={y}_{j}$, and the first derivative $\frac{dP}{dx}$ is continuous. The second derivative $\frac{{d}^{2}P}{d{x}^{2}}$ is probably not continuous; there may be jumps at the ${x}_{j}$.

• The slopes at the ${x}_{j}$ are chosen in such a way that preserves the shape of the data and respects monotonicity. This means that, on intervals where the data are monotonic, so is ; at points where the data has a local extremum, so does .

 Note   If y is a matrix, satisfies the above for each column of y.

## Examples

collapse all

### Data Interpolation Using `spline` and `pchip`

```x = -3:3; y = [-1 -1 -1 0 1 1 1]; t = -3:.01:3; p = pchip(x,y,t); s = spline(x,y,t); plot(x,y,'o',t,p,'-',t,s,'-.') legend('data','pchip','spline',4) ```

collapse all

### Tips

`spline` constructs $S\left(x\right)$ in almost the same way `pchip` constructs . However, `spline` chooses the slopes at the ${x}_{j}$ differently, namely to make even ${S}^{″}\left(x\right)$ continuous. This has the following effects:

• `spline` produces a smoother result, i.e. ${S}^{″}\left(x\right)$ 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 are not smooth.

• `pchip` is less expensive to set up.

• The two are equally expensive to evaluate.

## References

[1] Fritsch, F. N. and R. E. Carlson, "Monotone Piecewise Cubic Interpolation," SIAM J. Numerical Analysis, Vol. 17, 1980, pp.238-246.

[2] Kahaner, David, Cleve Moler, Stephen Nash, Numerical Methods and Software, Prentice Hall, 1988.