Scipy.odr问题尚未完全解决

时间:2019-02-22 14:12:04

标签: python error-handling scipy physics curve-fitting

我在使用scipy.odr时遇到问题,出现错误问题不是解决方案的完整等级,但我的代码中找不到任何问题。因为我的数据有错误,所以我喜欢使用scipy.odr,我真的不明白为什么会出现这种曲线。我曾尝试过更改Beta,但实际上没有任何效果,任何elp都将不胜感激。

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
from scipy.odr import *

mpi=np.array([ 561.10619469,  497.67699115,  381.37168142,  415.64159292,
    319.35840708,  298.2300885 ,  267.80973451,  259.57964602,
    186.10619469,  135.55309735,  526.72847682,  520.60927152,
    393.45695364,  350.14569536,  342.88741722,  320.45033113,
    271.54966887,  228.10596026,  123.07284768,  529.11901082,
    508.00618238,  386.12055641,  385.03863988,  386.05873261,
    252.48840804,  248.74806801,  186.39876352,  445.79710145,
    437.35507246,  298.44202899,  297.06521739,  216.8115942 ]) #xdata points
mpie=np.array([ 63298.50371429,  56143.0692621 ,  43022.63067308,  46888.66811285,
    36026.93912446,  33643.44324536,  30211.78442474,  29283.29075008,
    20994.74617757,  15291.88202765,  79544.75998494,  78620.65895826,
    59418.55650629,  52877.84076112,  51781.72985335,  48393.33219477,
    41008.53938409,  34447.83260932,  18586.10030632,  93243.98747739,
    89523.39709654,  68044.14654876,  67853.41585038,  68033.21100709,
    44494.79098361,  43835.63040252,  32848.1260434 ,  65111.09199701,
    63878.0932953 ,  43589.06961959,  43388.06057156,  31666.51040367])
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([  81228.44136084,  105891.10554268,   72586.52412992,
    170274.69023402,  124303.27959364,  122214.48642154,
    172247.00683902,  140925.25731526,  130453.55121254,
    155457.15232305,   66868.29287197,   69193.61294033,
     86113.69641793,   98516.30378106,   98476.60530537,
     63642.84072177,   95820.25975343,   95761.91867776,
    158873.93790292,  128112.61872256,  148658.77487919,
    166175.01398915,  172169.62519725,  124536.67130365,
    121338.18606547,  151061.59262845,  130248.558622  ,
     96850.39109209,   93315.52646666,  121042.58444862,
    142131.97197699,  135040.07242596]) #ydata error
delms=np.array([-439486.22394175, -453589.50728422, -474562.84848165,
   -545875.49483219, -558505.8001615 , -440890.41132917,
   -493487.90134405, -567909.03847304, -574749.74643764,
   -512283.16722235, -412150.37150419, -544790.8058711 ,
   -426168.3840475 , -436948.0354663 , -563974.13291816,
   -438345.2429572 , -445306.94463695, -446123.30181426,
   -567546.95305763,  405166.25669506, -524399.07133181,
   -521244.54097054, -543951.01120888, -624886.87164189,
   -551622.65037574, -640184.82844645, -561843.80854482,
   -495115.00406134, -566034.23190213, -509557.68417056,
   -578119.56061458, -575260.98048436]) #sdata, X[1]
delmse=np.array([ -7.94232540e+07,  -8.25714371e+07,  -8.73708610e+07,
    -1.04932958e+08,  -1.08280485e+08,  -7.97339870e+07,
    -9.18325124e+07,  -1.10826787e+08,  -1.12709708e+08,
    -9.63981360e+07,  -1.08169526e+08,  -1.55270603e+08,
    -1.12700011e+08,  -1.16244893e+08,  -1.63052864e+08,
    -1.16708389e+08,  -1.19031937e+08,  -1.19305969e+08,
    -1.64536753e+08,   8.58841024e+07,  -1.38663929e+08,
    -1.37596629e+08,  -1.45401303e+08,  -1.76005519e+08,
    -1.48105448e+08,  -1.82402867e+08,  -1.51764921e+08,
    -2.39870492e+08,  -2.92307066e+08,  -2.49907414e+08,
    -3.02124573e+08,  -2.99776036e+08]) #sdata error

bfij=np.array([  -1.0944,    1.    ,   -1.15  ,    4.1388,   -0.25  ,  242.2724,
     28.5703,   -6.7751,   -0.4167]) #coefficients

def Ffiitsso(p,X,B=2.58,Fc=92.2,mu=770,Za=0.9468):
    temp1 = (X[0]/4*pi*Fc)**2
    temp2 = temp1*(bfij[0]+bfij[1]*np.log((X[0]/mu)**2))
    temp3 = temp1**2*(bfij[2]+bfij[3]*np.log((X[0]/mu)**2)+\
               bfij[4]*(np.log((X[0]/mu)**2))**2)
    temp4 = temp1**3*(bfij[5]+bfij[6]*np.log((X[0]/mu)**2)+\
                  bfij[7]*(np.log((X[0]/mu)**2))**2+\
                  bfij[8]*(np.log((X[0]/mu)**2))**3)
    return Fc/Za*(1+p[0]*X[1])/(1+temp2+temp3+temp4)+p[1]

#ODR part
xtot2=np.row_stack( (mpi, delms) )
etot2=np.row_stack( (mpie, delmse) )
fittingr = Model(Ffiitsso)
mydatar = RealData(xtot2, Fr, sx=etot2, sy=dFr)
myodrr = ODR(mydatar, fittingr, beta0=[10**(-8), 10])
myoutputr = myodrr.run()
myoutputr.pprint()
betr=myoutputr.beta

#plot part
mudr22s2 = np.linspace(min(mpi**2),max(mpi**2),100)
mudrd22s2 = np.linspace(min(delms),max(delms),100)
plt.figure(figsize=(14,8.5))
plt.xlim([0, max(mpi**2)])

plt.plot(mpi[0:10]**2,Fr[0:10],"b^")
plt.plot(mpi[10:19]**2,Fr[10:19],"g^")
plt.plot(mpi[19:27]**2,Fr[19:27],"r^")
plt.plot(mpi[27:32]**2,Fr[27:32],"k^")
plt.plot(mudr22s2,Ffiitsso(betr,[mudr22s2,mudrd22s2]))

Figure I get

0 个答案:

没有答案