I'd like to fit a spherical formula with 2 structures, but I cant find a way to do it. "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" on the last line.
My goal is to fit experimental data with the curve_fit function of scipy, or other libraries. My code:
import numpy as np
from scipy.optimize import curve_fit
xdata = np.asarray(df['exp_x'])
ydata = np.asarray(df['exp_y'])
def Spherical(x, comp1, sill_comp2, range_comp2, sill_comp3, range_comp3):
if x <= range_comp2:
comp2 = sill_comp2 * (1.5 * (x / range_comp2) - 0.5 * (x / range_comp2) ** 3)
else:
comp2 = sill_comp2
if x < range_comp3:
c3 = sill_comp3 * (1.5 * (x / range_comp3) - 0.5 * (x / range_comp3) ** 3)
else:
c3 = sill_comp3
y = comp1 + comp2 + comp3
return y
parameters, covariance = curve_fit(Spherical, xdata, ydata)
parameters
Thanks !