第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在沒(méi)有庫(kù)的python中找到x給定y的函數(shù)

在沒(méi)有庫(kù)的python中找到x給定y的函數(shù)

慕的地6264312 2022-11-09 16:34:26
在此處為我的練習(xí)表輸入圖像描述,我需要編寫(xiě)一個(gè)函數(shù),該函數(shù)在沒(méi)有任何庫(kù)函數(shù)的情況下找到函數(shù)(稱(chēng)為 Maxwell_Boltzmann)的給定 y 值的 x 值。所以我嘗試定義函數(shù)(稱(chēng)為 most_probable_speed),它首先找到 Maxwell_Boltzmann 的 y 值的最大值,然后找到達(dá)到最大值的 x(在我的代碼中定義為 v)。我用for和if循環(huán)嘗試打印v,但它沒(méi)有打印出達(dá)到最大值的參數(shù)v,而是給了我整個(gè)向量v。有誰(shuí)知道我如何打印參數(shù)v,達(dá)到 y 了嗎?我的代碼是:import numpy as npimport matplotlib.pyplot as pltimport scipyimport scipy.constants as csv= np.linspace(-100,10000,1000) #this are my x values#this is my function, which prints y value in fonction of xdef Maxwell_Boltzmann(v, m, T):      ''' calculate the Maxwell-Boltzmann  distribution      for the speed of the chemical elements      3 parameters: -the speed of the particle              -its masse              -its temperature'''      y = np.sqrt(2/np.pi) * (m /( cs.k * T))**(3/2) * v**2 * np.exp(-m*v**2/(2 * cs.k * T))       return y;# this is the function which is suppose to find the x value given the y valuedef 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 a in Maxwell_Boltzmann(v, m, T):           if a == highest_probability:               print(v)           else:               continuereturn highest_probability;m_oxy = 15.99 * cs.u most_probable_speed(v, m = m_oxy, T = 273)  
查看完整描述

1 回答

?
UYOU

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


查看完整回答
反對(duì) 回復(fù) 2022-11-09
  • 1 回答
  • 0 關(guān)注
  • 124 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)