INTERP
This function computes a one-dimensional linear interpolation from paired data points. It estimates a value at coordinate x using the two surrounding points in the input sequences.
For neighboring points (x_0, y_0) and (x_1, y_1) with x_0 \le x \le x_1, linear interpolation is:
y = y_0 + (x-x_0)\frac{y_1-y_0}{x_1-x_0}
Optional left and right values can be supplied for out-of-range inputs, or extrapolation can be enabled.
Excel Usage
=INTERP(x, dx, dy, left, right, extrapolate)
x(float, required): X-coordinate of the interpolated value (dimensionless).dx(list[list], required): X-coordinates of the data points (dimensionless).dy(list[list], required): Y-coordinates of the data points (dimensionless).left(float, optional, default: null): Value to return for x below dx range (dimensionless).right(float, optional, default: null): Value to return for x above dx range (dimensionless).extrapolate(bool, optional, default: false): Whether to extrapolate outside of bounds.
Returns (float): Interpolated value (dimensionless).
Example 1: Midpoint interpolation
Inputs:
| x | dx | dy | ||||
|---|---|---|---|---|---|---|
| 2.5 | 1 | 2 | 3 | 3 | 2 | 0 |
Excel formula:
=INTERP(2.5, {1,2,3}, {3,2,0})
Expected output:
1
Example 2: Left-side value override
Inputs:
| x | dx | dy | left | ||||
|---|---|---|---|---|---|---|---|
| 0.5 | 1 | 2 | 3 | 10 | 20 | 30 | 5 |
Excel formula:
=INTERP(0.5, {1,2,3}, {10,20,30}, 5)
Expected output:
5
Example 3: Right-side value override
Inputs:
| x | dx | dy | right | ||||
|---|---|---|---|---|---|---|---|
| 4 | 1 | 2 | 3 | 3 | 2 | 0 | -1 |
Excel formula:
=INTERP(4, {1,2,3}, {3,2,0}, -1)
Expected output:
-1
Example 4: Extrapolate below range
Inputs:
| x | dx | dy | extrapolate | ||||
|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 3 | 2 | 0 | true |
Excel formula:
=INTERP(0, {1,2,3}, {3,2,0}, TRUE)
Expected output:
4
Python Code
Show Code
from ht.insulation import interp as ht_interp
def interp(x, dx, dy, left=None, right=None, extrapolate=False):
"""
Perform one-dimensional linear interpolation.
See: https://ht.readthedocs.io/en/latest/ht.insulation.html
This example function is provided as-is without any representation of accuracy.
Args:
x (float): X-coordinate of the interpolated value (dimensionless).
dx (list[list]): X-coordinates of the data points (dimensionless).
dy (list[list]): Y-coordinates of the data points (dimensionless).
left (float, optional): Value to return for x below dx range (dimensionless). Default is None.
right (float, optional): Value to return for x above dx range (dimensionless). Default is None.
extrapolate (bool, optional): Whether to extrapolate outside of bounds. Default is False.
Returns:
float: Interpolated value (dimensionless).
"""
try:
def to2d(value):
return [[value]] if not isinstance(value, list) else value
def flatten_2d(values, name):
data = to2d(values)
if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
raise ValueError(f"{name} must be a 2D list")
flat = []
for row in data:
for item in row:
try:
flat.append(float(item))
except (TypeError, ValueError):
raise ValueError(f"{name} must contain only numbers")
return flat
dx_flat = flatten_2d(dx, "dx")
dy_flat = flatten_2d(dy, "dy")
if len(dx_flat) != len(dy_flat):
return "Error: dx and dy must have the same length"
x_val = float(x)
left_val = None if left is None else float(left)
right_val = None if right is None else float(right)
result = ht_interp(x_val, dx_flat, dy_flat, left=left_val, right=right_val, extrapolate=extrapolate)
return float(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
X-coordinate of the interpolated value (dimensionless).
X-coordinates of the data points (dimensionless).
Y-coordinates of the data points (dimensionless).
Value to return for x below dx range (dimensionless).
Value to return for x above dx range (dimensionless).
Whether to extrapolate outside of bounds.