In the
mathematical
Mathematics is an area of knowledge that includes the topics of numbers, formulas and related structures, shapes and the spaces in which they are contained, and quantities and their changes. These topics are represented in modern mathematics ...
field of
numerical analysis
Numerical analysis is the study of algorithms that use numerical approximation (as opposed to symbolic manipulations) for the problems of mathematical analysis (as distinguished from discrete mathematics). It is the study of numerical methods th ...
, monotone cubic interpolation is a variant of
cubic interpolation that preserves
monotonicity of the
data set being interpolated.
Monotonicity is preserved by
linear interpolation
In mathematics, linear interpolation is a method of curve fitting using linear polynomials to construct new data points within the range of a discrete set of known data points.
Linear interpolation between two known points
If the two known po ...
but not guaranteed by
cubic interpolation.
Monotone cubic Hermite interpolation
Monotone interpolation can be accomplished using
cubic Hermite spline with the tangents
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
indexed in sorted order for
.
# Compute the slopes of the
secant lines between successive points:
for
.
# 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,
for
.
For the endpoints, use one-sided differences:
.
If
and
have opposite signs, set
.
# For
, where ever
(where ever two successive
are equal),
set
as the spline connecting these points must be flat to preserve monotonicity.
Ignore steps 4 and 5 for those
.
# Let
.
If either
or
is negative, then the input data points are not strictly monotone, and
is a local extremum. In such cases, ''piecewise'' monotone curves can still be generated by choosing
if
or
if
, although strict monotonicity is not possible globally.
# To prevent
overshoot and ensure monotonicity, at least one of the following three conditions must be met:
::(a) the function
, or
::(b)
, or
::(c)
.
::Only condition (a) is sufficient to ensure strict monotonicity:
must be positive.
::One simple way to satisfy this constraint is to restrict the vector
to a circle of radius 3. That is, if
, then set
,
and rescale the tangents via
.
::Alternatively it is sufficient to restrict
and
. To accomplish this if
, then set
.
Cubic interpolation
After the preprocessing above, evaluation of the interpolated spline is equivalent to
cubic Hermite spline, using the data
,
, and
for
.
To evaluate at
, find the index
in the sequence where
, lies between
, and
, that is:
. Calculate
:
then the interpolated value is
:
where
are the basis functions for the
cubic Hermite spline.
Example implementation
The following
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
implementation takes a data set and produces a monotone cubic spline interpolant function:
/*
* Monotone cubic spline interpolation
* Usage example listed at bottom; this is a fully-functional package. For
* example, this can be executed either at sites like
* https://www.programiz.com/javascript/online-compiler/
* or using nodeJS.
*/
function DEBUG(s)
var j = 0;
var createInterpolant = function(xs, ys) ;
/*
Usage example below will approximate x^2 for 0 <= x <= 4.
Command line usage example (requires installation of nodejs):
node monotone-cubic-spline.js
*/
var X = , 1, 2, 3, 4
var F = , 1, 4, 9, 16
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
var f = createInterpolant(X,F);
var N = X.length;
console.log("# BLOCK 0 :: Data for monotone-cubic-spline.js");
console.log("X" + "\t" + "F");
for (var i = 0; i < N; i += 1)
console.log(" ");
console.log(" ");
console.log("# BLOCK 1 :: Interpolated data for monotone-cubic-spline.js");
console.log(" x " + "\t\t" + " P(x) " + "\t\t" + " dP(x)/dx ");
var message = '';
var M = 25;
for (var i = 0; i <= M; i += 1)
console.log(message);
References
*
*{{cite journal
, last = Dougherty
, first = R.L.
, author2=Edelman, A.
, author3=Hyman, J.M.
, title = Positivity-, monotonicity-, or convexity-preserving cubic and quintic Hermite interpolation
, journal =
Mathematics of Computation
''Mathematics of Computation'' is a bimonthly mathematics journal focused on computational mathematics. It was established in 1943 as ''Mathematical Tables and other Aids to Computation'', obtaining its current name in 1960. Articles older than f ...
, volume = 52
, number = 186
, pages = 471–494
, date = April 1989
, doi=10.2307/2008477
, doi-access = free
External links
*
GPLv2 licensed
C++ implementation
MonotCubicInterpolator.cppMonotCubicInterpolator.hpp
Interpolation
Splines (mathematics)
Articles with example JavaScript code