# Interpolation functions

## Contents

These three functions interpolate across arrays. Given arrays «y» and «x» with a common index «i», these functions interpolate a value for «y» corresponding to value «v» along the «x» axis.

LinearInterp() and CubicInterp() use these variables:
`Index Index_a :=`
 a b c
`Index Index_b :=`
 1 2 3
`Variable Array_a :=`
Index_b ▶
Index_a: ▼ 1 2 3
a 7 -3 1
b -4 -1 6
c 5 0 -2

## Stepinterp(x, y, v, i)

Returns the element or slice of array «y» for which «v» has the smallest value less than or equal to «x». «x» and «y» must both be indexed by «i», and «x» must be increasing along index «i». If «v» is greater than all values of «x», it returns the element of «y» for which «x» has the largest value.

When an optional parameter, «leftLookup», is specified as True, it returns the element or slice of «y» corresponding to the largest value in «x» that is less than or equal to «v».

If «v» is an atom (scalar value), the result is an array indexed by all indexes of «a» except «x»’s index. If «v» is an array, the result is also indexed by the indexes of «v».

If the first parameter «x» is an index of «y», the fourth parameter is optional. Stepinterp(x, y, v) is similar to y[x = v] except that y[x = v] selects based on «v» being equal to «x», while Stepinterp(x, y, v) selects based on «v» being greater than or equal to «x».

Stepinterp() can be used to perform table lookup.

Library: Array

Examples: To see the values in `Car_prices` corresponding to `Years >= 2007.5`:

`Stepinterp(Years, Car_prices, 2007.5, Years) →`
VW Honda BMW
19K 22K 30K

Here v is a list of two values:

`Stepinterp(Years, Car_prices, [2007, 2008], Years) →`
VW Honda BMW
2007 18K 20K 28K
2008 19K 22K 30K

## Linearinterp(d, r, x, i, extrapolationMethod)

Returns linearly interpolated values of «x», given «d» and «r» representing an arbitrary piecewise linear function. «d» and «r» must both be indexed by «i», and «d» must be increasing along «i». «r» is an array of the corresponding output values for the function (not necessarily increasing and might be more than one dimension). «x» might be probabilistic and/or an array.

For each value of «x», Linearinterp() finds the nearest two values from «d» and interpolates linearly between the corresponding values from «r». By default, if «x» is less than the minimum value in «d», it returns the first value in «r». If «x» is greater than the maximum value in «d», it returns the last value in «r».

Library: Array

Example:

`Linearinterp(Index_b, Array_a, 1.5, Index_b) →`
Index_a ▶
a b c
2 -2.5 2.5

Extrapolation: You can set the optional «extrapolationMethod» parameter to alter how extrapolation is handled when «x» is outside the range of «d» values. The possible values for «extrapolationMethod» are

• 1 = (default) Return «r» value of nearest point.
• 2 = Disable extrapolation. Return null if outside «d»'s range.
• 3 = Return «r» value of nearest point, but disable extrapolation when solving an LP or QP optimization problem. See the Analytica Optimization manual.
• 4 = Extrapolate using the slope of the first or last segment.
• 5 = Extrapolate using the slope of the first or last segment, but disable extrapolation when solving an LP or QP optimization problem. See the Analytica Optimization manual.

## Cubicinterp(x, y, v, i)

Returns the natural cubic spline interpolated values of «y» along «x», interpolating for values of «v». «x» and «y» must both be indexed by «i», and «x» must be increasing along «i».

For each value of «v», Cubicinterp() finds the nearest values from «x», and using a natural cubic spline between the corresponding values of «y», computes the interpolated value. If «v» is less than the minimum value in «x», it returns the first value in «y»; if «v» is greater than the maximum value in «x», it returns the last value for «y».

Library: Array

Example:

`Cubicinterp(Index_b, Array_a, 1.5, Index_b) →`
Index_a ▶
a b c
0.6875 -2.875 2.219