2013-02-24 260 views
2

我正在将MatLab程序转换为Python,而且我遇到问题,理解为什么scipy.interpolate.interp2d(线性)给出的结果与MatLab interp2不同(线性)。 我知道scipy.interpolate.rectbivariatespline给出与matlab interp2(立方体)相同的结果。但在线性方法中给出了不同的结果。 最好的问候,并感谢您,泽纳布 蟒蛇:SciPy interp2d(线性)结果不同于MatLab interp2(线性)

from numpy import * 
from scipy import interpolate 

att=array([[ 239.044127,236.38616,231.891308,224.461261,212.628196,194.850501,170.332448,140.179075,107.822158,77.686597,52.989905,34.704514,22.112333,13.845297,8.577347,5.280345,3.238684,1.982192,1.211678,0.740152],[ 279.480169,276.646675,271.848543,263.89899,251.189969,231.976788,205.21918,171.832314,135.280008,100.344172,70.805784,48.14555,31.927007,20.837779,13.469048,8.656136,5.544346,3.544295,2.263191,1.444215],[ 326.756261,323.76423,318.690817,310.265908,296.745218,276.177018,247.251256,210.633036,169.730239,129.60991,94.611588,66.792291,46.097975,31.361771,21.150511,14.190114,9.491439,6.33744,4.227221,2.818012],[ 382.029446,378.906692, 373.604492, 364.77947, 350.562265, 328.799039, 297.892154, 258.195183, 212.95352, 167.411105, 126.421205, 92.660904,66.558802, 47.200839, 33.212749, 23.262033, 16.248519, 11.331774,7.895667, 5.49862 ],[ 446.652489, 443.440837, 437.980353, 428.871037, 414.139452, 391.447518,358.905095, 316.497136, 267.183987 ,216.237154, 168.925618, 128.548413,96.101273 , 71.039332 , 52.154139, 38.133744, 27.816052 , 20.261982,14.747643 , 10.729132],[ 522.206988, 518.966226, 513.448831 , 504.22346 , 489.246854 , 466.032868,432.414435 ,387.964004, 335.224713 , 279.303496 ,225.720556 , 178.335132,138.756323 ,106.917309 , 81.897894 , 62.513127 , 47.618665 , 36.2298, 27.545866 ,20.935121],[ 610.542076 ,607.354851 , 601.921297 , 592.815265, 577.975567 ,554.82951,520.979629, 475.5685 , 420.592603, 360.763363 , 301.610673 ,247.404216,200.34404 , 160.915237 , 128.604656 ,102.478559 , 81.519018  ,64.781346,51.450576 , 40.849466]]) 

att_r=logspace(0,log10(200),20) 
att_m=linspace(4.5,7.5,7) 
[r_grid,m_grid] = meshgrid(att_r,att_m) 
rr=16 
mm=4.6 

att_cubic = interpolate.RectBivariateSpline(att_m,att_r,att) 
Att_cubic = att_cubic(mm,rr) 
print Att_cubic 

att_linear = interpolate.interp2d(r_grid,m_grid,att,kind='linear') 
Att_linear = att_linear(rr,mm) 
print Att_linear 

MATLAB:

att=[239.044127 236.386160 231.891308 224.461261 212.628196 194.850501 170.332448 140.179075 107.822158 77.686597 52.989905 34.704514 22.112333 13.845297 8.577347 5.280345 3.238684 1.982192 1.211678 0.740152  
279.480169 276.646675 271.848543 263.898990 251.189969 231.976788 205.219180 171.832314 135.280008 100.344172 70.805784 48.145550 31.927007 20.837779 13.469048 8.656136 5.544346 3.544295 2.263191 1.444215 
326.756261 323.764230 318.690817 310.265908 296.745218 276.177018 247.251256 210.633036 169.730239 129.609910 94.611588 66.792291 46.097975 31.361771 21.150511 14.190114 9.491439 6.337440 4.227221 2.818012 
382.029446 378.906692 373.604492 364.779470 350.562265 328.799039 297.892154 258.195183 212.953520 167.411105 126.421205 92.660904 66.558802 47.200839 33.212749 23.262033 16.248519 11.331774 7.895667 5.498620 
446.652489 443.440837 437.980353 428.871037 414.139452 391.447518 358.905095 316.497136 267.183987 216.237154 168.925618 128.548413 96.101273 71.039332 52.154139 38.133744 27.816052 20.261982 14.747643 10.729132 
522.206988 518.966226 513.448831 504.223460 489.246854 466.032868 432.414435 387.964004 335.224713 279.303496 225.720556 178.335132 138.756323 106.917309 81.897894 62.513127 47.618665 36.229800 27.545866 20.935121 
610.542076 607.354851 601.921297 592.815265 577.975567 554.829510 520.979629 475.568500 420.592603 360.763363 301.610673 247.404216 200.344040 160.915237 128.604656 102.478559 81.519018 64.781346 51.450576 40.849466]; 

r=logspace(0,log10(200),20); 
m=[4.5:0.5:7.5]'; 
[r_grid,m_grid]=meshgrid(r,m); 
rr=16; 
mm=4.6; 
att_cubic=interp2(r_grid,m_grid,att,rr,mm,'cubic') 
att_linear=interp2(r_grid,m_grid,att,rr,mm) 

输出(MATLAB):

att_cubic =57.4565 
att_linear =58.2270 

输出(蟒蛇):

Att_cubic=[[ 57.45649122]] 
Att_linear=[ 56.13548995] 
+2

你能否包含一个简单的例子来显示matlab和python中的输入和输出? – 2013-02-24 21:25:44

回答

1

我没有回答这个(对不起,没有足够的信誉发表评论),但是,当我运行Python代码,我得到以下警告:

Warning:  No more knots can be added because the number of B-spline coefficients 
    already exceeds the number of data points m. Probably causes: either 
    s or m too small. (fp>s) 
    kx,ky=1,1 nx,ny=19,11 m=140 fp=2.359644 s=0.000000 

我不完全清楚的什么是怎么回事,但这里有一些可能会帮助你的调查链接:

  1. Problem with 2D interpolation in SciPy, non-rectangular grid
  2. https://groups.google.com/forum/#!topic/scipy-user/eJZKWvvR86Y
  3. http://mail.scipy.org/pipermail/scipy-user/2007-October/014130.html
  4. https://github.com/scipy/scipy/issues/1599

让我知道这是否有帮助和/或你已经知道了这一点。