简体   繁体   English

用Scipy插入不连续的函数

[英]Interpolate a discontinuous function with Scipy

I am having problems interpolating some data points using Scipy. 我在使用Scipy插值一些数据点时遇到问题。 I guess that it might depend on the fact that the function I'm trying to interpolate is discontinuous at x roughly 4. 我想这可能取决于我要插入的函数在x大约为4时不连续的事实。

Here is the code I'm using to interpolate: 这是我用来插值的代码:

  from scipy import *
  y_interpolated = interp1d(x,y,buonds_error=False,fill_value=0.,kind='cubic')
  new_x_array = arange(min(x),max(x),0.05)
  plot(new_x_array,x_interpolated(new_x_array),'r-')

The error I get is 我得到的错误是

  File "<stdin>", line 2, in <module>
  File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 357, in __call__
    out_of_bounds = self._check_bounds(x_new)
  File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 415, in _check_bounds
    raise ValueError("A value in x_new is above the interpolation "
ValueError: A value in x_new is above the interpolation range.

These are my data points: 这些是我的数据点:

  1.56916432074 -27.9998263169
  1.76773750527 -27.6198430485
  1.98360238449 -27.2397962268
  2.25133982943 -26.8596491107
  2.49319293195 -26.5518194791
  2.77823462692 -26.1896935372
  3.07201297519 -25.9540514619
  3.46090507092 -25.7362456112
  3.65968688527 -25.6453922172
  3.84116464506 -25.53652509
  3.97070419447 -25.3374215879
  4.03087127145 -24.8493356465
  4.08217147954 -24.0540196233
  4.12470899596 -23.0960856364
  4.17612639206 -22.4634289328
  4.19318305992 -22.1380894034
  4.2708234589 -21.902951035
  4.3745696768 -21.9027079759
  4.52158254627 -21.9565591238
  4.65985875536 -21.8839570732
  4.80666329863 -21.6486676004
  4.91026629192 -21.4496126386
  5.05709528961 -21.2685401725
  5.29054655428 -21.2860476871
  5.54129211534 -21.3215908912
  5.73174988353 -21.6645019816
  6.06035782465 -21.772138994
  6.30243916407 -21.7715483093
  6.59656410998 -22.0238656166
  6.86481948673 -22.3665921479
  7.01182409559 -22.4385289076
  7.17609125906 -22.4200564296
  7.37494987052 -22.4376476472
  7.60844044988 -22.5093814451
  7.79869207061 -22.5812017094
  8.00616642549 -22.5445612485
  8.17903446593 -22.4899243886
  8.29141325457 -22.4715846981

What version of scipy are you using? 您正在使用什么版本的scipy

The script you posted has some syntax errors (I assume due to wrong copy and paste). 您发布的脚本存在一些语法错误(我认为是由于错误的复制和粘贴)。 This script works, with scipy.__version__ == 0.9.0 . 此脚本的工作方式为scipy.__version__ == 0.9.0 结果图片在这里 .

import sys
from scipy import *
from scipy.interpolate import *
from pylab import plot

x = []
y = []
for line in sys.stdin:
   a, b = line.split()
   x.append(float(a))
   y.append(float(b))

y_interpolated = interp1d(x,y,bounds_error=False,fill_value=0.,kind='cubic')
new_x_array = arange(min(x),max(x),0.05)
plot(new_x_array,y_interpolated(new_x_array),'r-')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM