我不确定在使用 numpy.gradient 时如何指定非均匀间距。
这是 y = x**2 的一些示例代码。
import numpy as np
import matplotlib.pyplot as plt
x = [0.0, 2.0, 4.0, 8.0, 16.0]
y = [0.0, 4.0, 16.0, 64.0, 256.0]
dydx = [0.0, 4.0, 8.0, 16.0, 32.0] # analytical solution
spacing = [0.0, 2.0, 2.0, 4.0, 8.0] #added a zero at the start to get length matching up with y
m = np.gradient(y, spacing)
plt.plot(x, y, 'bo',
x, dydx, 'r-', #analytical solution
x, m, 'ro') #calculated solution
plt.show()
Run Code Online (Sandbox Code Playgroud)
间距数组的长度总是比我想计算梯度的数组少一。添加零以使长度匹配(如在上面的示例代码中)给出了错误的答案,一个点的梯度是无限的。
我无法理解/遵循非均匀间距的 …