1 回答

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
由于 y 的每個(gè)元素對(duì)應(yīng)于同一索引中的 v 元素(這是您想要的 x 值),因此您可以使用enumerate給您每個(gè)值的索引并打印出相應(yīng)的x而不是整個(gè)向量:
def most_probable_speed(v,m,T):
'''determine the most probable speed of a given mass and temperature'''
x = Maxwell_Boltzmann(v, m, T) #put the y values of Maxwel_B for all x in an array named x
highest_probability = np.amax(x) #Return the maximum value of y
# I want to print the value of v for whcih Maxwell_Boltzmann(v, m, T)= highest_probability
for idx, a in enumerate(Maxwell_Boltzmann(v, m, T)):
if a == highest_probability:
print(v[idx])
else:
continue
但是,您調(diào)用 Maxwell_Boltzmann 函數(shù)兩次。如果您只是想找到對(duì)應(yīng)于最高 y 值的 x,您可以更有效地執(zhí)行此操作,如下所示:
def most_probable_speed(v,m,T):
'''determine the most probable speed of a given mass and temperature'''
x = Maxwell_Boltzmann(v, m, T) #put the y values of Maxwel_B for all x in an array named x
highest_probability_idx = np.argmax(x) # Return the index of the maximum value of y
print(v[highest_probability_idx])
在這里,np.argmax返回返回?cái)?shù)組中最大值的索引,然后您可以使用它來(lái)訪(fǎng)問(wèn)v向量中相應(yīng)的 x
添加回答
舉報(bào)