Monotone cubic interpolation

In the mathematical field of numerical analysis, monotone cubic interpolation is a variant of cubic interpolation that preserves monotonicity of the data set being interpolated.

Monotonicity is preserved by linear interpolation but not guaranteed by cubic interpolation.

Monotone cubic Hermite interpolation
Monotone interpolation can be accomplished using cubic Hermite spline with the tangents $$m_i$$ modified to ensure the monotonicity of the resulting Hermite spline.

An algorithm is also available for monotone quintic Hermite interpolation.

Interpolant selection
There are several ways of selecting interpolating tangents for each data point. This section will outline the use of the Fritsch–Carlson method. Note that only one pass of the algorithm is required.

Let the data points be $$(x_k,y_k)$$ indexed in sorted order for $$k=1,\,\dots\,n$$.


 * 1) Compute the slopes of the secant lines between successive points:"$\delta_k =\frac{y_{k+1}-y_k}{x_{k+1}-x_k}$" for $$k=1,\,\dots\,n-1$$.
 * 2) These assignments are provisional, and may be superseded in the remaining steps. Initialize the tangents at every interior data point as the average of the secants,"$m_k = \frac{\delta_{k-1}+\delta_k}{2}$" for $$k=2,\,\dots\,n-1$$.  For the endpoints, use one-sided differences: "$m_1 = \delta_1 \quad \text{ and } \quad m_n = \delta_{n-1}\,$."If $$\delta_{k-1}$$ and $$\delta_k$$ have opposite signs, set $$m_k = 0 $$.
 * 3) For $$k=1,\,\dots\,n-1$$, where ever $$\delta_k = 0$$ (where ever two successive $$y_k=y_{k+1}$$ are equal), set $$m_k = m_{k+1} = 0,$$ as the spline connecting these points must be flat to preserve monotonicity. Ignore steps 4 and 5 for those $$k\,$$.
 * 4) Let "$\alpha_k = m_k/\delta_k \quad \text{ and } \quad \beta_k = m_{k+1}/\delta_k$."If either $$\alpha_k$$ or $$\beta_k$$ is negative, then the input data points are not strictly monotone, and $$(x_k,\,y_k)$$ is a local extremum. In such cases, piecewise monotone curves can still be generated by choosing $$m_{k}=0\,$$ if $$\alpha_k < 0$$ or $$m_{k+1}=0\,$$ if $$\beta_k < 0$$, although strict monotonicity is not possible globally.
 * 5) To prevent overshoot and ensure monotonicity, at least one of the following three conditions must be met:
 * (a) the function "$\phi_k = \alpha_k - \frac{(2 \alpha_k + \beta_k - 3)^2}{3(\alpha_k + \beta_k - 2)} > 0\,$, or"
 * (b) $$\alpha_k + 2\beta_k - 3 \le 0\,$$, or
 * (c) $$2\alpha_k + \beta_k - 3 \le 0\,$$.
 * Only condition (a) is sufficient to ensure strict monotonicity: $$\phi_k$$ must be positive.
 * One simple way to satisfy this constraint is to restrict the vector $$(\alpha_k,\,\beta_k)$$ to a circle of radius 3. That is, if $$\alpha_k^2 + \beta_k^2 > 9\,$$, then set "$\tau_k = \frac{3}{\sqrt{\alpha_k^2 + \beta_k^2}}\,$,|undefined" and rescale the tangents via "$m_k = \tau_k\, \alpha_k \,\delta_k \quad \text{ and } \quad m_{k+1} = \tau_k\, \beta_k\, \delta_k\,$."
 * Alternatively it is sufficient to restrict $$\alpha_k \le 3$$ and $$\beta_k \le 3\,$$. To accomplish this, if $$\alpha_k > 3\,$$, then set $$m_k = 3 \, \delta_k\,$$, and if $$\beta_k > 3\,$$, then set $$m_{k+1} = 3 \, \delta_k\,$$.

Cubic interpolation
After the preprocessing above, evaluation of the interpolated spline is equivalent to cubic Hermite spline, using the data $$x_k$$, $$y_k$$, and $$m_k$$ for $$k=1,\,\dots\,n$$.

To evaluate at $$x$$, find the index $$k$$ in the sequence where $$x$$, lies between $$x_k$$, and $$x_{k+1}$$, that is: $$x_k \leq x \leq x_{k+1}$$. Calculate
 * $$\Delta = x_{k+1}-x_k \quad \text{ and } \quad t = \frac{x - x_k}{\Delta}$$

then the interpolated value is
 * $$f_\text{interpolated}(x) = y_k\cdot h_{00}(t) + \Delta\cdot m_k\cdot h_{10}(t) + y_{k+1}\cdot h_{01}(t) + \Delta\cdot m_{k+1}\cdot h_{11}(t)$$

where $$h_{ii}$$ are the basis functions for the cubic Hermite spline.

Example implementation
The following JavaScript implementation takes a data set and produces a monotone cubic spline interpolant function: