Hi everyone,
thank you for the support, I have read the guide you had linked, it was very helpful.
After some troubles I arrived to this:
def f(x, y, a, b, c, d):
return (a*y**3+(b**2-x**2)*y)**2+(y*c*x)**2-d
df = pd.read_csv(Datafile)
x= df['Frequency'][:] #Frequency along x
y= df['Amplitude'][:] #Average of amplitude
mod = lmfit.Model(f, independent_vars=['x','y']) #model declared
#Initial values and contrains for the fitting coefficients
mod.set_param_hint('a',value=1)
mod.set_param_hint('b',value=np.mean(x), min=0)
mod.set_param_hint('c',value=100, min=0)
mod.set_param_hint('d',value=1, min=0)
pars = mod.make_params(verbose=True) #Make parameters from the model
out = mod.fit(y, pars, x=x, y=y, verbose=True) #First fitting
print(mod.print_param_hints(colwidth=10)) #show the parameters
print(out.fit_report())
#Re-fitting the model
result=out.fit(y,params=out.params,x=x, y=y) #Second fitting
print(out.fit_report()) #Print results
#Evaluation of residuals
evaluation = out.eval() #Saving those numbers in name_file_eval
uncert = out.eval_uncertainty()
#calculate R-squared
lmfit_Rsquared = '%.5f'%(1 - out.residual.var()/np.var(y))
print("The R-square value is: ", lmfit_Rsquared)
print(out.ci_report())
out.params.pretty_print(colwidth=10, fmt='g')
Which gives the right results, but with the problem (I guess) that the model try to fit every single point in x and y data.
While it should just use 'x' as independent variable and minimize the residuals with 'y'.
Thanks to Matt I also implemented the minimize function:
params = Parameters()
params.add('a', value=1)
params.add('b', value=np.mean(x), min=0)
params.add('c', value=100, min=0)
params.add('d', value=1, min=0)
result = minimizer(f, params, fcn_args=(x, y))
report_fit(result)
But in this case it gives the error message:
TypeError: 'module' object is not callable
There are attached the output response