gpt4 book ai didi

python - 最小化带有 2 个参数的函数

转载 作者:行者123 更新时间:2023-12-01 09:30:50 25 4
gpt4 key购买 nike

我正在尝试最小化具有 2 个参数的函数:

def c_Gamma_gamma_fv(cf, cv):
return np.abs((4 * eta_gamma * charges**2 * a_q * cf).sum() + 4.* cf *a_tau/3. + a_w * cv)**2/Gamma_gamma

def mu_fv(cf, cv):
return np.array([cf**4, cf**2 * cv**2, cf**2 *
c_Gamma_gamma_fv(cf, cv), cv**2 * c_Gamma_gamma_fv(cf, cv), cf**4, cv**2 * cf**2, cf**2 * cv**2,
cv**4, cv**2 * cf**2, cv**4])

def chi_square_fv(cf, cv):
return ((mu_fv(cf, cv) - mu_data) @ inv_cov @ (mu_fv(cf, cv) - mu_data))

x0 = [1., 1.]
res_fv = minimize(chi_square_fv, x0)

但是,我收到错误“TypeError:chi_square_fv() 缺少 1 个必需的位置参数:'cv'”。但是,当我执行以下操作时:

print(chi_square_fv(1.,1.))

我得到了输出

38.8312698786

我不明白这一点,而且我对此类程序不熟悉。我该如何继续? OBS:Gamma_gamma只是代码中的一个常量。

最佳答案

由于您没有向我们提供代码中的所有变量值,所以我不得不猜测。

我认为问题在于你如何传入参数。 x0 = [1.,1.]x0 指定为具有 2 个值的列表,这是一个实体。但是,在您的 chi_square_fv 中,输入是两个单独的值而不是列表。

您可以尝试更改您的chi_square_fv函数:

def chi_square_fv(clist):
cf, cv = clist
return ((mu_fv(cf, cv) - mu_data) @ inv_cov @ (mu_fv(cf, cv) - mu_data))

关于python - 最小化带有 2 个参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49984323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com