Scipy interp1d 和 matlab interp1

10
以下是我插值的输入内容:
x = [-1.01, 5.66, 5.69, 13.77, 20.89]

y = [0.28773, 1.036889, 1.043178, 1.595322, 1.543763]

new_x = [0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20]

matlabinterp1scipy.interpolateinterp1d 的结果不同。
结果如下。

new_y_scipy=[0.401171, 0.625806, 0.850442, 1.062384, 1.186291, 1.248244, 1.310198, 1.372152, 1.434105, 1.496059, 1.545429, 1.55267, 1.559911, 1.567153, 1.574394, 1.588877,]

new_y_matlab=[0.401171, 0.625806, 0.850442, 1.064362, 1.201031, 1.269366, 1.3377, 1.406035, 1.47437, 1.542704, 1.593656, 1.586415, 1.579174, 1.571932, 1.564691, 1.550208]

显然,matlab似乎比scipy获得更好的结果。这其中的根本区别是什么?


1
我的scipy结果与你的matlab结果相匹配:np.allclose(new_y_matlab, interp1d(x,y)(new_x)) True - askewchan
嗨@askewchan,np.allclose是什么?不使用它有什么区别吗? - Sri
np.allclose 只是检查两个数组是否相同,这里返回 True。我的观点是,我认为您在使用 scipy 时犯了一个错误,因为当我使用 scipy 时,我得到的结果与您在 matlab 中得到的结果相同,请参阅我的答案以获取更多信息。 - askewchan
1
我写的代码是 'set_intep=interp1d(x,y,kind='linear') new_y=set_intep(new_x)',但我得到了不同的值。为什么? - Sri
这对我来说仍然是相同的东西。你确定你的 xy 数组匹配吗?从我做的图中,看起来你的 y 数组中的最后两个值几乎被交换了:P - askewchan
1个回答

11

我认为你从scipy获取的数据可能出了些问题,因为我无法复现你的问题。对我来说,scipy的结果与你从matlab得到的结果完全相符。请看下面的演示:

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

x = [-1.01, 5.66, 5.69, 13.77, 20.89]
y = [0.28773, 1.036889, 1.043178, 1.595322, 1.543763]

new_x = [0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20]
new_y_scipy=[0.401171, 0.625806, 0.850442, 1.062384, 1.186291, 1.248244, 1.310198, 1.372152, 1.434105, 1.496059, 1.545429, 1.55267, 1.559911, 1.567153, 1.574394, 1.588877,]
new_y_matlab=[0.401171, 0.625806, 0.850442, 1.064362, 1.201031, 1.269366, 1.3377, 1.406035, 1.47437, 1.542704, 1.593656, 1.586415, 1.579174, 1.571932, 1.564691, 1.550208]

askewchan = interp1d(x,y)(new_x)

# 'linear' has no effect since it's the default, but I'll plot it too:
set_interp = interp1d(x, y, kind='linear')
new_y = set_interp(new_x)

plt.plot(x, y, 'o', new_x, new_y_scipy, '--', new_x, new_y_matlab, ':', new_x, askewchan, '.', new_x, new_y, '+')
plt.legend(('Original','OP_scipy', 'OP_matlab', 'askewchan_scipy', 'OP style scipy'), loc='lower right')

np.allclose(new_y_matlab, interp1d(x,y)(new_x))
#True

在此输入图片描述


1
嘿,谢谢。原始数组中存在一些舍入误差,这就是错误累积的原因。 - Sri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接