I have written the following code in Python:
def f_multi_para(q_sq, alpha_par):
return f_0 / (q_sq * alpha_par)
def dB_dqsq_model2_para(q_sq, V_ub_par, alpha_par):
sec1 = G_F**2 * V_ub_par
sec2 = (1 - m_e**2 / q_sq)**2
sec3 = (q_sq * (1 + m_e**2 / q_sq) * f_multi_para(q_sq, alpha_par)**2)
return sec1 * sec2 * sec3
def chi_sq(params):
V_ub_par, alpha_par = params
return np.sum(((dB_dqsq_arr - np.array([dB_dqsq_model2_para(v, V_ub_par, alpha_par) for v in q_sq_arr])) / dBdqsq_err_arr)**2)
initial_guess = [0.0037, 0.54]
result = optimize.minimize(chi_sq, initial_guess)
if result.success:
fitted_params = result.x
print(fitted_params)
else:
raise ValueError(result.message)
Where q_sq
, V_ub_par
, and alpha_par
are the function parameters, and the rest are stored variables. I am trying to minimize chi_sq(params)
with respect to the parameters V_ub_par
and alpha_par
whilst q_sq
takes several values through a for loop. I found a solution to minimizing a multivariate function using scipy.optimize
here and I followed the same methodology; however, when I run the code, the following error pops up:
ValueError: The user-provided objective function must return a scalar value.
I am not sure what this error means, and how to fix it (for context, I am relatively new to python, and programming in general).
I am using the following libraries:
import numpy as np
import matplotlib.pyplot as plt
import math
from scipy.integrate import quad
import scipy.optimize as optimize
And the variables in the above code are either integers or arrays, given as follows:
f_0 = 0.261
G_F = 1.166 * 10**(-5)
m_e = 0.511 * 10**(-3)
dB_dqsq_arr = np.array([7.2,7.14,6.7,7.56,6.44,7.17,6.67,6.33,6.2,4.32,4.25,3.4,1.17])
dBdqsq_err_arr = np.array([0.70,0.45,0.39,0.43,0.43,0.45,0.47,0.48,0.44,0.43,0.41,0.42,0.26])
q_sq_arr = np.array([1,3,5,7,9,11,13,15,17,19,21,23,25])