使用Minuit拟合多个变量的曲线

时间:2019-02-25 12:59:05

标签: python curve-fitting

我正在尝试拟合一些数据,但是我一直感到困难。我之前提到的问题是该函数是应该拟合的两组数据的函数。我最近一直在尝试使用Minuit拟合te曲线,但我只是得到一条直线。我以前没有使用Minuit,因此如果有人能看到我的错误,将不胜感激。这是代码。

import numpy as np
from math import pi
from math import sqrt
from math import log
from scipy import optimize
import scipy.optimize
import matplotlib.pyplot as plt
import iminuit
from iminuit import Minuit

mud=np.array([ 62.3034458 ,  49.38321903,  29.00404314,  34.78485066,
        20.8157854 ,  17.80098451,  14.24225111,  13.73978429,
         6.99794801,   3.67825221,  54.86125828,  54.03278146,
        30.08443709,  24.04668874,  23.30165563,  20.1486755 ,
        14.69503311,  10.22781457,   2.94139073,  52.2731221 ,
        50.77358578,  29.54086553,  29.35163833,  29.38020093,
        12.638949  ,  12.34618238,   6.96570325,  39.89929348,
        39.49570652,  18.13994565,  18.10559783,   9.45853261]) #xdata
delmstrue=np.arrayarray([ 129508.06468325,  115404.78134078,   94431.44014335,
     23118.79379281,   10488.4884635 ,  128103.87729583,
     75506.38728095,    1085.25015196,   -5755.45781264,
     56711.12140265,  156843.91712081,   24203.4827539 ,
    142825.9045775 ,  132046.2531587 ,    5020.15570684,
    130649.0456678 ,  123687.34398805,  122870.98681074,
      1447.33556737,  974160.54532006,   44595.21729319,
     47749.74765446,   25043.27741612,  -55892.58301689,
     17371.63824926,  -71190.53982145,    7150.48008018,
     73879.28456366,    2960.05672287,   59436.60445444,
     -9125.27198958,   -6266.69185936]) #sdata

Fr=np.array([ 127.31527434,  122.72790265,  110.26449558,  112.75717699,
        104.81830088,  104.35746903,  101.32016814,  100.54513274,
         96.87942478,   92.98330088,  124.9736053 ,  122.52414305,
        112.47114172,  108.74591788,  107.34258013,  108.00597616,
        102.18850331,  100.04522384,   91.47210596,  128.18641113,
        122.15516847,  108.23229985,  109.85263369,  107.69218856,
         99.14042658,   98.0902102 ,   99.1104204 ,  112.47678261,
        110.39126087,   98.373     ,   98.97391304,   95.01495652])#ydata
dFr=np.array([  972.45503744,   950.56823614,   890.18024392,   902.26140931,
         863.19146598,   860.89934086,   845.61846393,   841.7802125 ,
         823.25930603,   803.30233495,  1110.53060342,  1097.31750128,
        1042.33199071,  1021.61696855,  1013.76699146,  1017.50919056,
         984.68738931,   972.47173607,   922.7338377 ,  1279.08318053,
        1242.90431731,  1157.41627741,  1167.51119501,  1154.09094116,
        1099.91468262,  1093.12777974,  1099.71194357,  1352.9713629 ,
        1338.43820154,  1252.88647812,  1257.21770186,  1228.38236923])#ydata error

mudrm = np.linspace(1,max(mud),32)
mudrms = np.linspace(min(delmstrue),max(delmstrue),32) #used in plot


afij=np.array([  1.09440000e+00,  -1.00000000e+00,  -4.73000000e-02,
        -1.90580000e+00,  -1.25000000e+00,  -2.44535000e+02,
        -1.54989000e+01,  -9.39460000e+00,  -3.45830000e+00]) #coefficients

def Ffitr(X,s,k,B=2.61,Fc=92.8,mu=770): #fit curve
    temp1 = (2*B*X)/(4*pi*Fc)**2
    temp2 = temp1*(afij[0]+afij[1]*np.log((2*B*X)/mu**2))
    temp3 = temp1**2*(afij[2]+afij[3]*np.log((2*B*X)/mu**2)+\
                   afij[4]*(np.log((2*B*X)/mu**2))**2)
    temp4 = temp1**3*(afij[5]+afij[6]*np.log((2*B*X)/mu**2)+\
                      afij[7]*(np.log((2*B*X)/mu**2))**2+\
                      afij[8]*(np.log((2*B*X)/mu**2))**3)
    return Fc*(1+k*s)*(1+temp2+temp3+temp4)

#minuit part
def least_squares(k):
    yvar = dFr
    return sum(((Fr - Ffitr(mud, delmstrue,k))/dFr)** 2)

m=minuit.Minuit(least_squares,k=-2.494309898854109e-07,errordef=1)

fminf,paramf=m.migrad()
plt.figure(figsize=(14,8.5))
plt.plot(mud,Fr,"b^")
plt.plot(mudrm, Ffitr(mudrm,mudrms,6.952427712300677e-07))

这就是我得到的:Data points and curve from my fit.

0 个答案:

没有答案