我有同样的问题,我对函数的输入有正确的顺序。但由于某种原因,它只适合我的非线性数据的线性线。我期待拟合模型中的某种曲率!我不知道如何解决这个问题。任何帮助,将不胜感激。这是我的代码:
from matplotlib import pyplot
from scipy.optimize import curve_fit
import scipy.optimize as optimize
import numpy as np
x1= [0 , 27.7527, 76.0898, 116.2636, 157.7859]
y1= [91.1904, 83.2582, 68.8431, 38.7545, 17.5318]
# define the true objective function
def objective(x, a0, a1, a2, k2, k4):
return a0 + a1 * np.arctan((x/ k2)) + a2 * np.arctan((x/ k4))
# curve fit
popt, _ = curve_fit(objective, x1, y1 , method='lm' )
# summarize the parameter values
a0, a1, a2, k2, k4 = popt
print(popt)
# define a sequence of inputs between the smallest and largest known inputs
x_line = np.arange(min(x1), max(x1), 1)
# calculate the output for the range
y_line = objective(x_line, a0, a1, a2, k2, k4)
# create a line plot for the mapping function
pyplot.plot(x_line, y_line, '--', color='red')
pyplot.scatter(x1, y1)
pyplot.show()
正确的拟合应该如下所示: