Search code examples
pythonscipyminimize

Minimizing function with 2 arguments


I'm trying to minimize a function with 2 arguments:

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)

but, I'm getting the error "TypeError: chi_square_fv() missing 1 required positional argument: 'cv'". But, when I do the following:

print(chi_square_fv(1.,1.))

I get the output

38.8312698786

I'm not understanding this and I'm new to this type of procedure. How do I proceed? OBS: Gamma_gamma is just a constant of the code.


Solution

  • Since you didn't provide us with all the variable values in your code, I would have to guess.

    I think the problem is about how you pass in the parameters. x0 = [1.,1.] specifies x0 as a list with 2 values, which is ONE entity. However, in your chi_square_fv, the inputs are two separate values rather than a list.

    You can try to change your chi_square_fv function:

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