如何找到非常接近零的值的索引



如何找到除函数[0]之外的值的索引?

import numpy as np
border1 = 0
border2 = 0.5
h = 0.03
N = 1000
t = np.linspace(border1, border2, N)
f = 20
function = h*np.sin(t*f)
index1, = np.where(function == -4.65146680e-05)
index2, = np.where(function == 9.30292241e-05)
print(index1)
print(index2)
print(function)
eps = 1e-1
index, = np.where((function[1:].all() > -eps) and (function[1:].all() < eps))
print(index)

您可以使用np.argmin找到最接近您要查找的值的索引。

val1 = -4.65146680e-05
index1 = np.argmin(np.abs(function - val))

最新更新