1 回答

TA貢獻1878條經驗 獲得超4個贊
由于 y 的每個元素對應于同一索引中的 v 元素(這是您想要的 x 值),因此您可以使用enumerate給您每個值的索引并打印出相應的x而不是整個向量:
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
但是,您調用 Maxwell_Boltzmann 函數兩次。如果您只是想找到對應于最高 y 值的 x,您可以更有效地執行此操作,如下所示:
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返回返回數組中最大值的索引,然后您可以使用它來訪問v向量中相應的 x
添加回答
舉報