给出以下数据集:
x=[1.99526231e+08, 7.94328235e+08, 3.16227766e+09, 1.25892541e+10, 5.01187234e+10, 1.99526231e+11, 7.94328235e+11]
y=[0, 0, 0.01, 0.19, 0.09, 0, 0.71]
yerr=[0, 0, 0.00894427, 0.10430724, 0.08049845, 0., 0.171114]
我想要一个双峰对数正态分布函数。这是我的尝试:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
x=np.array([1.99526231e+08, 7.94328235e+08, 3.16227766e+09, 1.25892541e+10, 5.01187234e+10, 1.99526231e+11, 7.94328235e+11])
y=np.array([0, 0, 0.01, 0.19, 0.09, 0, 0.71])
yerr=np.array([0, 0, 0.00894427, 0.10430724, 0.08049845, 0., 0.171114])
h, ax = plt.subplots(figsize=(10,5))
def PDF(x, A1, B1, A2, B2):
return (2*np.pi)**(-1/2) * (B1*x)**(-1) * np.exp(-(np.log(x)-A1)**2/(2*B1**2)) + (2*np.pi)**(-1/2) * (B2*x)**(-1) * np.exp(-(np.log(x)-A2)**2/(2*B2**2))
popt, pcov = curve_fit(PDF, x, y, sigma=yerr, p0 = [10.1, 3e10, 11.9, 7e11], bounds=([9.9, 8e9, 11.7, 3e11], [10.3, 5e10, 12.1, 1e12]), method='trf')
xdata = np.array(np.logspace(8.30, 11.90, 1000))
ax.plot(xdata, PDF(xdata, *popt), 'blue', linestyle = 'solid', label='Best-Fit')
ax.errorbar(x, y, xerr = np.array([0]*7), yerr = yerr, ecolor = 'b', fmt='b.', markersize = '5', elinewidth = 0.25, capsize = 2, linestyle = '')
ax.set_xlim([1e8, 2e12])
ax.set_ylim([-0.03, 0.96])
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("linear", nonposy='clip')
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ax.tick_params(axis="x", which="both", direction="in")
h.subplots_adjust(left=0.11, bottom=0.14, right=0.99, top=0.99, wspace=0.1, hspace=0.)
h.text(0.52,0.02,'$x$', fontsize=12, ha='center')
h.text(0.01, 0.52, r'PDF', fontsize=12, va='center', rotation='vertical')
plt.setp([a.get_xticklabels() for a in h.axes[:-1]], visible=True)
plt.show()
这是我收到的我不理解的错误消息:
/home/username/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py:728: RuntimeWarning: divide by zero encountered in true_divide
transform = 1.0 / sigma
Traceback (most recent call last):
File "data.py", line 14, in <module>
popt, pcov = curve_fit(PDF, x, y, sigma=yerr, p0 = [10.1, 3e10, 11.9, 7e11], bounds=([9.9, 8e9, 11.7, 3e11], [10.3, 5e10, 12.1, 1e12]), method='trf')
File "/home/username/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py", line 762, in curve_fit
**kwargs)
File "/home/username/anaconda3/lib/python3.6/site-packages/scipy/optimize/_lsq/least_squares.py", line 805, in least_squares
raise ValueError("Residuals are not finite in the initial point.")
ValueError: Residuals are not finite in the initial point.