Reputation: 711
I have an issue with my code. I am trying to find the x value for which my y is equal (or close) to 0, for the first time.
The following code computes the values of x and y for different parameters. What I don't understand is that when I try the code line by line, I obtain a min-value for my val array, but using np.where doesn't work as it can't find this value it just obtained.. I thought it was maybe related to how the min_value is stored and that it somehow rounds it up, making it not equal to what it previously was. I tried using `idx = np.where(np.isclose(val,0)) instead but I couldn't make it work. How can I solve this problem ?
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]
Upvotes: 0
Views: 69
Reputation: 897
It's because your minimum value is negative, so when you take
min_val = np.min(np.abs(val))
min_val isn't a member of your array. But it is a member of np.abs(val)
so I'd recommend:
idx = np.where(np.abs(val) == min_val)
Upvotes: 1