【发布时间】:2016-12-30 06:46:28
【问题描述】:
我编写了一个代码,用于使用 scipy 最小化与 r 和 alpha 相关的某个函数。但是,我在将参数传递给函数时遇到了问题。
#!/usr/bin/env python
import numpy as np
from scipy.integrate import quad
import scipy.optimize as opt
def integrand(t, alpha, r):
return np.exp(-alpha*(t-r))**2
def my_function(parameters, rho):
alpha = parameters[0]
r = parameters[1]
return quad(integrand, 0, rho, args=(alpha, r))[0]
alpha_0 = 1
r_0 = 1
rho = 5.0
vec_expint = np.vectorize(my_function)
res = opt.minimize(my_function(rho), np.asarray([alpha_0, r_0]), method='CG', tol=1.e-2, options={'gtol': 0.01, 'maxiter': 5})
print(res)
我想将积分边界作为变量。我添加了一个变量 rho,我收到以下错误消息:
res = opt.minimize(my_function(rho), np.asarray([alpha_0, r_0]), method='CG', tol=1.e-2, options={'gtol': 0.01, 'maxiter': 5})
TypeError: my_function() takes exactly 2 arguments (1 given)
谁能解释我如何以正确的方式传递参数,好吗?
【问题讨论】: