Interpolation Functions

Prev Next

Function Names

linear interpolation, nearest neighbor interpolation, nearest right neighbor interpolation, spline interpolation, polynomial interpolation

Description

These functions calculate a value based on known points in a 2-dimensional space by applying one of the following interpolation schemes:

Function name Description
linear interpolation Straight lines are drawn between the points
nearest neighbor interpolation Staircase pattern: Y-value takes value of nearest neighbor. If both distances are equal, then the nearest left neighbor is chosen.
nearest right neighbor interpolation Same as above, nearest right neighbor chosen if distances are equal.
spline interpolation Natural cubic spline interpolation
polynomial interpolation Creates n-th order polynomial (n = number of points - 1) which passes through all specified points.

All interpolation functions ensure that the calculated lines cross the specified points. If discontinuities are undesirable, use spine or polynomial interpolation. Note that the polynomial interpolation create some oscillation effects between the outer 2-3 points at the beginning and at the end. The spline interpolation does not have this problem.

See interpolation examples with visualized charts.

Call as: function

Restrictions

Indirect parameter passing is disabled

Parameter count

3-5

Parameters

No.TypeDescription
1
input
numeral
set of numerals
X-values

One or more X-values provided in order to calculate one or more Y-values

2
input
set of numerals X-coordinates of points

The list of known points consist of X-coordinates provided in this function parameter and Y-coordinates in the next function parameter.

3
input
set of numerals X-coordinates of points

The list of known points consist of X-coordinates provided in the previous function parameter and Y-coordinates in this function parameter. The X-values must be in ascending order.

Opt. 4
input
string Extrapolation options beyond left side

Extrapolation option left side (and also applicable to beyond the right side if the 5th parameter is not specified). Following options exist:

reject Assert an error if the X value lies outside the interpolation range
flat Continue with flat value (extended with a horizontal line)
continue Continue with using same formula for extrapolation:
nearest (right) neighbor interpolation: flat (horizontal line)
linear interpolation: continue with same slope
spline / polynomial interpolation: use same formula.
Note: spline and polynomial interpolation will result in over / under-shooting effects outside the interpolation points. Use them carefully.
zero Zero returned
numeral Apply line with specific slope. Using 0 has same effect as flat (and not zero).

Default value: reject
Opt. 5
input
string Extrapolation options beyond right side

Extrapolation option right side. Same value selection as defined for the 4th aprameter (extrapolation options beyond left side).

Default value: same as specified in 4th parameter, otherwise reject

Return value

TypeDescription
numeral
set of numerals
Result

Calculated Y-value (or Y-values if set with multiple numerals are passed into 1st function parameter).

Exceptions

Rejected extrapolation
X-values not in ascending order
Empty sets or sets of different lengths provided for X- and/or Y-coordinates

Examples

               x[] = {-1 .. 12 };
               y[] = linear interpolation( x[], {0, 4, 8, 10}, {0, 8, 5, 0}, flat, continue );
               // Note: Extrapolation is done for for x[] = -1, 11 and 12.

               echo(y[]);

Output

{0,0,2,4,6,8,7.25,6.5,5.75,5,2.5,0,-2.5,-5}
Try it yourself: Open LIB_Function_linear_interpolation.b4p in B4P_Examples.zip. Decompress before use.

See also

Interpolation Examples