I was trying to solve a cubic equation and to fit with my experimental data set. But there is some problem in my code regarding curve_fit
. Though both function f
and del_y
defined perfectly (checked using values of parameters ), curve_fit is not working and showing me the error :
ValueError: setting an array element with a sequence.
Can somebody help me out of this ?
import numpy as np
from scipy.optimize import curve_fit
def f(G0,H0,k1,k2):
a=(k1+2*k1*k2*H0-k1*k2*G0)/(k1*k2)
b=(1+k1*H0-k1*G0)/(k1*k2)
c=-G0/(k1*k2)
cf=[1,a,b,c]
k=np.roots(cf)
return abs(k[2])
def del_y(G0,H0,A,k1,k2,n):
return A*(0.04*k1*f(G0,H0,k1,k2)**n)
popt, pcov = curve_fit(del_y,x_data,y_data)