# import libraries
import numpy as np
from scipy.interpolate import RegularGridInterpolator

# define the known data as a 2D matrix
data = np.array([
   # todo: include the column and row headers in this array (for readability)
   #       and parse them out later (e.g., with pandas, or manually)
   # 60, 120, 240, 360
    [71, 100, 142, 173],  #  4
    [106, 150, 212, 260], #  6
    [142, 200, 283, 347], # 08
    [177, 250, 354, 433], # 10
    [212, 300, 425, 520], # 12
    [248, 350, 495, 607], # 14
    [283, 400, 566, 693], # 16
    [318, 450, 637, 780]  # 18
])

# define the x and y coordinates of the data points
xs = np.array([4,6,8,10,12,14,16,18])
ys = np.array([60,120,240,360])

# create an interpolation function using RegularGridInterpolator
fun = RegularGridInterpolator((xs,ys), data, bounds_error=False, fill_value=None)

# define a function that takes x and y coordinates as arguments and returns the interpolated value
def interp(x,y):
# only applicable when bounds_error=True (default)
#    # check if the coordinates are within the data range
#    if x < min(xs) or x > max(xs) or y < min(ys) or y > max(ys):
#        return None # return None if out of range
#    else:
    return fun((x,y)) # return the interpolated value

print(interp(6,120)) # 150
print(interp(6,90)) # between 106 and 150
print(interp(5.5,180)) # ~ 166
print(interp(9.5,300)) # ~ 375
print(interp(20.0,400)) # ~ 920

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: