pythonmatlablinear-interpolation

MATLAB Interp2 equivalent in SciPy python not working


Original MATLAB Statement is -

Output = (interp2(X,Y,Tbl',Z,ThrtlPrcnt,'linear'));

Where parameter values (in Python) are

X = array([0, 550, 600, 700, 800, 874, 900, 950, 1000, 1100, 1200, 1300, 1400,
       1500, 1600, 1700, 1730, 2000, 2100, 2200, 2500, 3000], dtype=object)
 
Y =array([0, 1, 10, 20, 30, 40, 50, 70, 80, 100], dtype=object)
 
Tbl = 
array([[-75, -226, -239, -271, -375, -453, -701, -759, -818, -997],
       [-1269, -1716, -1967, -2028, -2056, -2104, -2106, -2124, -2130,
        -2137],
       [-2156, -2680, 463, -114, -166, -271, -375, -453, -701, -759],
       [-818, -997, -1269, -1716, -1967, -2028, -2056, -2104, -2106,
        -2124],
       [-2130, -2137, -2156, -2680, 954, 285, 285, 285, 285, 167],
       [125, 44, -32, -161, -290, -402, -521, -660, -800, -925],
       [-970, -1318, -1426, -1534, -1857, -2679, 1106, 535, 535, 535],
       [535, 417, 375, 294, 218, 89, -32, -152, -271, -391],
       [-510, -637, -670, -969, -1080, -1191, -1524, -2331, 1258, 860],
       [860, 860, 860, 809, 792, 759, 726, 648, 570, 500],
       [400, 300, 185, 81, 46, -271, -389, -506, -858, -1634],
       [1410, 1285, 1285, 1285, 1285, 1222, 1200, 1155, 1112, 1037],
       [965, 890, 800, 700, 600, 500, 470, 150, 0, -150],
       [-525, -1286, 1592, 1592, 1592, 1592, 1592, 1576, 1568, 1563],
       [1554, 1518, 1483, 1432, 1381, 1329, 1261, 1157, 1119, 776],
       [649, 522, 140, -590, 1867, 1944, 1937, 1924, 1910, 1901],
       [1897, 1895, 1893, 1887, 1849, 1815, 1781, 1747, 1620, 1516],
       [1477, 1125, 995, 864, 473, -242, 2019, 2144, 2137, 2124],
       [2130, 2145, 2150, 2150, 2150, 2150, 2150, 2150, 2150, 2120],
       [2009, 1880, 1850, 1550, 1420, 1290, 900, 200, 2383, 2536],
       [2550, 2578, 2606, 2626, 2634, 2648, 2648, 2648, 2648, 2648],
       [2620, 2520, 2369, 2234, 2193, 1823, 1686, 1549, 1139, 455]],
      dtype=object)

Z = 
array([600, 700, 800, 874, 900, 950, 1000, 1100, 1200, 1300, 1400, 1500,
       1600, 1700, 1730], dtype=object)
       
ThrtlPrcnt = 100

My Expected output is -

               np.array([[2550],
                         [2578],
                         [2606],
                         [2626],
                         [2634],
                         [2648],
                         [2648],
                         [2648],
                         [2648],
                         [2648],
                         [2620],
                         [2520],
                         [2369],
                         [2234],
                         [2193]])

I have tried scipy RectBivariateSpline, bisplrep and bisplev but no luck. Trying it from since last week but no luck. In 1D interpolation, i.e. np.interp we need to shuffle last parameter from MATLAB to first in Python. Not sure how it goes in 2D. What I am missing?

for RectBivariateSpline getting error - raise ValueError('x dimension of z must have same number of ' ValueError: x dimension of z must have same number of elements as x.


Solution

  • Below code worked for me perfectly -

    bivariateObj= RectBivariateSpline(X,Y,Tbl,[np.min(X),np.max(X),np.min(Y),np.max(Y)])
    
    TrqLmtbyThrtl = bivariateObj(Z,ThrtlPrcnt)