[英]Error with finding the index of value closest to 0
我的代碼有問題。 我試圖第一次找到我的 y 等於(或接近)0 的 x 值。
以下代碼計算不同參數的 x 和 y 值。 我不明白的是,當我逐行嘗試代碼時,我為我的 val 數組獲得了一個最小值,但是使用 np.where 不起作用,因為它找不到它剛剛獲得的這個值。我認為這可能與 min_value 的存儲方式有關,並且它以某種方式將其四舍五入,使其不等於以前的值。 我嘗試使用 `idx = np.where(np.isclose(val,0)) 代替,但我無法使其工作。 我怎么解決這個問題?
D = np.logspace(21,27,7)
x = np.logspace(0,7,500)
V0 = 1.17e+13
alpha = ((4*D)/((3300-1000)*9.81))**(1/4)
fig, axs = plt.subplots(2,4, figsize=(15, 6), facecolor='w', edgecolor='k')
fig.subplots_adjust(hspace = .5, wspace=.001)
axs = axs.ravel()
for i in range(0,len(D)):
val = np.zeros((len(x)))
val = -(V0*(alpha[i]**3))/(8*D[i]) * np.exp(-x/alpha[i]) * (np.cos(x/alpha[i]) + np.sin(x/alpha[i]))
# Find the index for which val crosses the x axis for the first time
min_val = np.min(np.abs(val))
idx = np.where(val == min_val)
idx_first = idx[0][0]
pos = x[idx_first]
這是因為你的最小值是負數,所以當你取
min_val = np.min(np.abs(val))
min_val 不是您的數組的成員。 但它是np.abs(val)
的成員,所以我建議:
idx = np.where(np.abs(val) == min_val)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.