scipy 插值给出不同的结果
scipy interpolate gives varying results
我试图在使用 scipy.interpolate.interp1d 读取 Python 中的插值函数时得到相同的答案,但是当我更改 x linspace 的大小时,我得到不同的结果。
下面是一个简化的例子,我给插值函数提供了不同的半径,它们 return 截然不同的结果。我无法弄清楚为什么会发生这种情况,因此将不胜感激任何帮助。
from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
M_centre = 2e30
G = 1.67e-11
m_test = 6e24
radius = np.linspace(5,1e2,1000)
radius2 = np.linspace(5,1e21,1000)
V_circ = np.sqrt(G*M_centre/radius)
V_circ2 = np.sqrt(G*M_centre/radius2)
velocities_circ = interp1d(radius,V_circ)
test_r = velocities_circ(50)
print(test_r)
velocities_circ2 = interp1d(radius2,V_circ2)
test_r2 = velocities_circ2(50)
print(test_r2)
Out:
817312853.7629617
2584569596.664017
我考虑过,也许 linspace 的步长会导致插值函数的读数发生变化,但它肯定不会有一个数量级的变化,对吗?
编辑:我也使用 numpy.interp 尝试过这种方法,但它给出的结果与上面相同。
只是为了对比而减少的问题的快速说明:
from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt
M_centre = 2e30
G = 1.67e-11
m_test = 6e24
radius1 = np.linspace(5,1e3,10)
radius2 = np.linspace(5,1e2,10)
V_circ1 = np.sqrt(G*M_centre/radius1)
V_circ2 = np.sqrt(G*M_centre/radius2)
velocities_circ1 = interp1d(radius1,V_circ1)
test_r1 = velocities_circ1(50)
print(test_r1)
velocities_circ2 = interp1d(radius2,V_circ2)
test_r2 = velocities_circ2(50)
print(test_r2)
plt.plot(radius1, V_circ1, "ro", label = "radius1")
plt.plot(radius2, V_circ2, "bx", label = "radius2")
plt.plot(radius1, velocities_circ1(radius1), "r")
plt.plot(radius2, velocities_circ2(radius2), "b")
plt.legend()
plt.xlim(0, 400)
plt.show()
我认为输出不同的原因很明显。
相同范围 (5, 1e2) 但点数不同 (3 vs 10) 的等效图:
我试图在使用 scipy.interpolate.interp1d 读取 Python 中的插值函数时得到相同的答案,但是当我更改 x linspace 的大小时,我得到不同的结果。
下面是一个简化的例子,我给插值函数提供了不同的半径,它们 return 截然不同的结果。我无法弄清楚为什么会发生这种情况,因此将不胜感激任何帮助。
from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
M_centre = 2e30
G = 1.67e-11
m_test = 6e24
radius = np.linspace(5,1e2,1000)
radius2 = np.linspace(5,1e21,1000)
V_circ = np.sqrt(G*M_centre/radius)
V_circ2 = np.sqrt(G*M_centre/radius2)
velocities_circ = interp1d(radius,V_circ)
test_r = velocities_circ(50)
print(test_r)
velocities_circ2 = interp1d(radius2,V_circ2)
test_r2 = velocities_circ2(50)
print(test_r2)
Out:
817312853.7629617
2584569596.664017
我考虑过,也许 linspace 的步长会导致插值函数的读数发生变化,但它肯定不会有一个数量级的变化,对吗?
编辑:我也使用 numpy.interp 尝试过这种方法,但它给出的结果与上面相同。
只是为了对比而减少的问题的快速说明:
from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt
M_centre = 2e30
G = 1.67e-11
m_test = 6e24
radius1 = np.linspace(5,1e3,10)
radius2 = np.linspace(5,1e2,10)
V_circ1 = np.sqrt(G*M_centre/radius1)
V_circ2 = np.sqrt(G*M_centre/radius2)
velocities_circ1 = interp1d(radius1,V_circ1)
test_r1 = velocities_circ1(50)
print(test_r1)
velocities_circ2 = interp1d(radius2,V_circ2)
test_r2 = velocities_circ2(50)
print(test_r2)
plt.plot(radius1, V_circ1, "ro", label = "radius1")
plt.plot(radius2, V_circ2, "bx", label = "radius2")
plt.plot(radius1, velocities_circ1(radius1), "r")
plt.plot(radius2, velocities_circ2(radius2), "b")
plt.legend()
plt.xlim(0, 400)
plt.show()
我认为输出不同的原因很明显。
相同范围 (5, 1e2) 但点数不同 (3 vs 10) 的等效图: