试图将一些数据与对数曲线拟合,但是我的输出无效,我不明白是什么导致了错误。这很奇怪,因为这是 minuit 应该做的,而且这是一个简单的例子,所以我不明白问题是什么。
from iminuit import Minuit
from math import pi
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def logis(x,a,b): #function to fit
return (1+x*np.log(x/b))*a
xlat=np.array([81.96100485, 64.96427609, 38.15520137, 45.75992993,
27.38344029, 23.41742996, 18.73586921, 18.07486749,
9.20589292, 4.83878931, 72.17070899, 71.08083681,
39.57647386, 31.63373626]) #xdata
lo=logis(xlat,92,30) #ydata
p=np.concatenate((np.array([92,30]),xlat,lo),axis=0) #all parameters
def chi(pa): #chisquare
chis=sum((92-pa[0:1])**2)+sum((30-pa[1:2])**2)+sum((xlat-pa[2:16])**2)+sum((lo-logis(pa[2:16],92,30))**2)
return chis
#minuit part
m = Minuit.from_array_func(chi,p,errordef=1)
fmin,para=m.migrad()
print(m.values)
print(fmin)
xn=[]
xtra=np.sort(xlat)
for i in range(2,16):
xn.append(m.values[i])
xr=np.array(xn)
#plot part
xp=np.linspace(min(xr),max(xr),14)
plt.figure(figsize=(14,8.5))
plt.plot(xp,logis(xp,m.values[0],m.values[1]))
plt.show