3

如果我用两个数组定义一个函数,例如这样:

from numpy import *
x = arange(-10,10,0.1)
y = x**3

如何提取 y(5.05) 的值,对两个更接近的点 y(5) 和 y(5.1) 的值进行插值?现在,如果我想找到那个值,我使用这个方法:

y0 = y[x>5][0]

我应该获得yfor的值x=5.1,但我认为存在更好的方法,并且可能它们是正确的方法。

4

2 回答 2

4

numpy.interp,如果线性插值就足够了:

>>> import numpy as np
>>> x = np.arange(-10, 10, 0.1)
>>> y = x**3
>>> np.interp(5.05, x, y)
128.82549999999998
>>> 5.05**3
128.787625

并且有很多scipy用于插值的工具[docs]

>>> import scipy.interpolate
>>> f = scipy.interpolate.UnivariateSpline(x, y)
>>> f
<scipy.interpolate.fitpack2.LSQUnivariateSpline object at 0xa85708c>
>>> f(5.05)
array(128.78762500000025)
于 2013-03-24T13:17:47.600 回答
4

在 numpy/scipy 中有一个功能。

import numpy as np
np.interp(5.05, x, y)
于 2013-03-24T13:18:31.407 回答