我想傳遞參數給一個函數,我認爲我做的正確,但它仍然給出錯誤: TypeError:p_vinet()需要2位置參數,但4給出了TypeError:p_vinet()需要2個位置參數,但4個給出
這裏是我的代碼的第一部分,然後我會給部分給出的錯誤。
volumeMgO被先前計算的陣列,其中包括:
array([ 7.64798549, 7.67153344, 7.67153344, 7.8068763 , 7.97288941,
8.14781986, 8.33321177, 8.53118834, 8.74433596, 8.97545339,
9.22826581, 9.50740563, 9.81962839])
params_MgO是這樣的:
params_MgO = [11.244, 160., 4.0]
的VINET功能是:
def p_vinet(v, params):
"""
This function will calculate pressure from Vinet equation.
Parameters
==========
v = volume
params = [V0, K0, K0']
Returns
=======
Pressure calculated from Vinet equation
"""
f_v = np.power(v/params[0], 1./3.)
eta = 1.5 * (params[2] - 1.)
P = 3 * params[1] * ((1 - f_v)/np.power(f_v, 2)) * np.exp(eta * (1 -
f_v))
return P
最後,斜坡函數僅是採取衍生物的簡單方法:
def slope(func, x, h, args=()):
"""
find a slope for function f at point x
Parameters
=========
f = function
x = independent variable for f
h = distance along x to the neighboring points
Returns
=======
slope
"""
rise = func(x+h, *args) - func(x-h, *args)
run = 2.*h
s = rise/run
return s
現在問題來了。當我鍵入:
BulkModulus_MgO = np.zeros(volumeMgO.size)
for i in range(volumeMgO.size):
BulkModulus_MgO[i] = slope(p_vinet, volumeMgO[i], volumeMgO[i]*0.0001,
args=(params_MgO))
我得到這個錯誤:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-116-60467d989bbc> in <module>()
1 BulkModulus_MgO = np.zeros(volumeMgO.size)
2 for i in range(volumeMgO.size):
----> 3 BulkModulus_MgO[i] = slope(p_vinet, volumeMgO[i],
volumeMgO[i]*0.0001, args=(params_MgO))
<ipython-input-100-618f25e85d34> in slope(func, x, h, args)
15 """
16
---> 17 rise = func(x+h, *args) - func(x-h, *args)
18 run = 2.*h
19
TypeError: p_vinet() takes 2 positional arguments but 4 were given
我不明白這一點。 p_vinet需要參數v和參數,我通過斜率函數中的x + h和x-h提供v,並且參數是包含p_vinet解包的3個條目的列表。這是2個參數。爲什麼它說我提供4?
對不起,如果它有點混淆我如何呈現代碼。我在jupyter筆記本中編碼,所有功能都是分開的。 volumeMgO與以前的代碼分開計算,沒有問題。