python中通过函数传递变量的问题
我很难理解下面代码中的函数rhs(u,m,r)是如何接收参数m和r的。从代码中可以看出,函数rhs在函数euler\u步骤(u,rhs,dt)中调用,但是参数m和r都不是作为参数传递给函数euler\u步骤,也不是全局变量。因此,有人可以向我解释参数m和u如何到达函数rhspython中通过函数传递变量的问题,python,function,arguments,Python,Function,Arguments,我很难理解下面代码中的函数rhs(u,m,r)是如何接收参数m和r的。从代码中可以看出,函数rhs在函数euler\u步骤(u,rhs,dt)中调用,但是参数m和r都不是作为参数传递给函数euler\u步骤,也不是全局变量。因此,有人可以向我解释参数m和u如何到达函数rhs # model parameters: mpo = 100. # initial mass of the rocket propellant in kg ms = 50. # mass of the rocket
# model parameters:
mpo = 100. # initial mass of the rocket propellant in kg
ms = 50. # mass of the rocket shell in kg
g = 9.81 # gravity in m s^{-2}
rho = 1.091 # average air density in kg/m^{3}
rad = 0.5 # radius of the maximum cross sectional area of the rocket in m
A = numpy.pi*(rad**2)# maximum cross sectional area of the rocket in m^{2}
v_e = 325. # the exhaust speed in m/s
C_D = 0.15 # drag coefficient
rt = 20.0 # propellant burn rate in kg/s
dtp = 5.0 # time interval to empty the propellant in s
### set initial conditions ###
h0 = 0.0 # start at the zero height [m]
v0 = 0.0 # initial speed [m/s]
def rhs(u, m, r):
"""Returns the right-hand side of the phugoid system of equations.
Parameters
----------
u : array of float
array containing the solution at time n.
mp: float
mass of the propellant at time t
mp_rate: float
propellant burn rate
Returns
-------
dudt : array of float
array containing the RHS given u.
"""
print("[m,r]",[m,r])
[h,v] = u.copy()
return numpy.array( [ v, -g + pow((ms+m),-1)*(r*v_e - 0.5*rho*v*abs(v)*A*C_D) ] )
def euler_step(u, rhs, dt):
"""Returns the solution at the next time-step using Euler's method.
Parameters
----------
u : array of float
solution at the previous time-step.
rhs : function
function to compute the right hand-side of the system of equation.
dt : float
time-increment.
Returns
-------
u_n_plus_1 : array of float
approximate solution at the next time step.
"""
return u + dt * rhs(u, m, r)
if __name__ == "__main__":
T = 17.0 # final time
dt = 0.1 # time increment
t = numpy.arange(0.0, T, dt) # time discretization
N = len(t) # number of time-steps
# initialize the array containing the solution for each time-step
u = numpy.zeros((N, 2))
u[0] = numpy.array([h0, v0]) # fill 1st element with initial values
rate = numpy.zeros(N)
mp = numpy.zeros(N)
Np = int(((N)/(T))*dtp) # number of time-steps with propellant burn
rate[0:Np] = rt # propellant burn rate in kg/s
mp[0:Np] = mpo - rt*t[0:Np]
# time loop - Euler method
for n in range(1,N-1):
r = rate[n]
m = mp[n]
print("[R,M]",[r,m])
u[n+1] = euler_step(u[n], rhs, dt)
提前感谢。
m
和n
是全球性的
它可能会令人困惑,因为它可能看起来是一个函数,但它不是。如果在全局作用域上运行,
如果
在全局作用域上运行m
和n
是全局变量
它可能会令人困惑,因为它可能看起来是一个函数,但它不是。如果在全局作用域上运行它们是全局变量。在Python中,if
、while
和for
都创建了一个单独的变量作用域,因此在第一次调用euler\u步骤之前,它们仍然在全局/模块作用域中赋值:
if __name__ == "__main__": # does not start a new variable scope
...
for n in range(1,N-1): # does not start one either
# thus these variables are set in global scope.
r = rate[n]
m = mp[n]
# and euler_step is invoked only here, thus it will see
# r and m being set.
u[n+1] = euler_step(u[n], rhs, dt)
另请参见。它们是全局变量。在Python中,if
、while
和for
都创建了一个单独的变量作用域,因此在第一次调用euler\u步骤之前,它们仍然在全局/模块作用域中赋值:
if __name__ == "__main__": # does not start a new variable scope
...
for n in range(1,N-1): # does not start one either
# thus these variables are set in global scope.
r = rate[n]
m = mp[n]
# and euler_step is invoked only here, thus it will see
# r and m being set.
u[n+1] = euler_step(u[n], rhs, dt)
另请参见。m
和r
在模块级脚本底部附近定义:
r = rate[n]
m = mp[n]
因此:
以下是模块、函数体和类
定义。作用域定义名称在块中的可见性。
如果在块中定义了局部变量,则其范围包括
块如果定义出现在功能块中,则范围将扩展
定义块中包含的任何块,除非包含
块为名称引入了不同的绑定。。。当一个名字是
在代码块中使用时,使用最近的封闭
范围
m
和r
在模块级脚本底部附近定义:
r = rate[n]
m = mp[n]
因此:
以下是模块、函数体和类
定义。作用域定义名称在块中的可见性。
如果在块中定义了局部变量,则其范围包括
块如果定义出现在功能块中,则范围将扩展
定义块中包含的任何块,除非包含
块为名称引入了不同的绑定。。。当一个名字是
在代码块中使用时,使用最近的封闭
范围
euler\u-step
将全局变量m
和r
传递给rhs
函数。euler\u-step
将全局变量m
和r
传递给rhs
函数。其他问题:为什么使用[h,v]=u.copy(),以获取u的值,而不是简单的正常赋值[h,v]=u???另一个问题:为什么使用[h,v]=u.copy(),来获取u的值,而不仅仅是一个普通赋值[h,v]=u???