在此处为我的练习表输入图像描述,我需要编写一个函数,该函数在没有任何库函数的情况下找到函数(称为 Maxwell_Boltzmann)的给定 y 值的 x 值。所以我尝试定义函数(称为 most_probable_speed),它首先找到 Maxwell_Boltzmann 的 y 值的最大值,然后找到达到最大值的 x(在我的代码中定义为 v)。我用for和if循环尝试打印v,但它没有打印出达到最大值的参数v,而是给了我整个向量v。有谁知道我如何打印参数v,达到 y 了吗?
我的代码是:
import numpy as np
import matplotlib.pyplot as plt
import scipy
import scipy.constants as cs
v= np.linspace(-100,10000,1000) #this are my x values
#this is my function, which prints y value in fonction of x
def 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 value
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 a in Maxwell_Boltzmann(v, m, T):
if a == highest_probability:
print(v)
else:
continue
return highest_probability;
m_oxy = 15.99 * cs.u
most_probable_speed(v, m = m_oxy, T = 273)
UYOU
给的代码中已有参数x
y 有定义var x
y 是否重复?
在python里面找不到函数
相关分类