Home

Awesome

PCHIPInterpolation.jl

Build Status Coverage PkgEval SciML Code Style

PCHIP (Piecewise Cubic Hermite Interpolating Polynomial) spline interpolation of arbitrarily spaced one-dimensional data in Julia. This package is a fork of SimplePCHIP with some extra features.

PCHIP interpolation preserves monotonicity (i.e., it will not over- or undershoot monotonic data points). See this SciPy documentation page for more details.

Summary

Load the package

using PCHIPInterpolation

You will be prompted to install the package if you do not already have it.

Create a PCHIP interpolator

xs = [0.0,  1.2,  2.0,  5.0, 10.0, 11.0]
ys = [2.0,  2.1,  1.0,  0.0,  0.0,  3.0]

itp = Interpolator(xs, ys)

The xs and ys inputs to the Interpolator can be of any subtype of AbstractVector, including the default Julia Vector (as in the example), custom vector types from other packages, and ranges (e.g., 1:2:5).

Evaluate

y = itp(1.5) # At a single point
ys = itp.(xs) # At multiple points

Plot (with Plots)

using Plots

plot(itp, markers=true, label="PCHIP")

Plot

The monotonicity-preserving property of PCHIP interpolation can be clearly seen in the plot.

Extrapolations

We can also using the cubic polynomial at the first and last intervals to extrapolate values outside the domain of itp.xs by setting itp.extrapolate = true (default is false) in the constructor:

itp = Interpolator(xs, ys; extrapolate = true)

If extrapolate = true then plotting the iterpolator will also show extrapolated values, extending the plotted domain by ± maximum(diff(itp.xs)) * 0.5:

plot(itp,markers=true, label="PCHIP w/ extrapolation")

Plot with extrapolation

Compute a definite integral

integral = integrate(itp, 1, 3) # Integral between 1 and 3

Compute a derivative (with ForwardDiff)

using ForwardDiff

dydx = ForwardDiff.derivative(itp, 1.5)

General cubic Hermite spline

A different Interpolator constructor also exists that takes the derivative values at the interpolation points as a third argument. This method will create a generic cubic Hermite spline, which will not preserve monotonicity in general.