python - Scipy.Optimize.Curve_Fit Not Fitting Cos^2 Well
问题描述
I am trying to fit a curve of the form
acos^2(x+b)+c
using scipy's curve_fit routine but I have not been able to get it to find good parameters. The fit doesn't seem to find the right amplitude or period.
I've tried modifying the fit function and changing the initial guesses to math the data. Below is the fit function and the fitting routine.
def fit(x, a, phi, b):
return (a*abs(math.cos(x+phi)**2))+b
#Vectorizing fit function to accept list input
fit_v = np.vectorize(fit)
params1, covs1 = optimize.curve_fit(fit_v, data_1_x_calibrated, data_1_y, p0=[20,30,90])
The data is plotted using the following code
#Plotting Fit 1
fit_1_y_data = []
for i in range(len(data_1_x)):
fit_1_y_data.append(params1[0]*math.cos(data_1_x_calibrated[i]+params1[1])**2+params1[2])
plt.plot(data_1_x_calibrated, fit_1_y_data, label="Fit")
plt.plot(data_1_x_calibrated, data_1_y, "r", label = "Data")
plt.legend()
The result is the following: Data and Fit Plot
Any help would be appreciated.
解决方案
推荐阅读
- excel - 动态列表中的超链接
- reactjs - 从 Auth0 请求中获取访问令牌
- amazon-web-services - 创建 Azure 策略以防止直接通过订阅访问 Internet
- swagger-ui - 规范中没有定义操作!- 即使设置了招摇并定义了端点,我也会收到此错误
- r - UseMethod 在 R 中的工作方式
- mysql - npm 包中的日期类型转换?
- python - 使用大数据框对 pandas dataframe.to_html() 进行分页
- javascript - 使用多个 url 抓取网页
- azure - 如何在任务执行期间(实时)更新 stdout.txt?
- javascript - Stripe-react-elements 中的条纹国家范围请求