拟合曲线时添加相关误差

时间:2019-02-19 10:01:14

标签: python error-handling correlation curve-fitting

我正在尝试使用Minimumsq方法将一些数据拟合到曲线上。

我的曲线看起来不像我想要的那样,第五点有很大的下降。我认为我需要添加误差才能获得更平滑的曲线,但是我不知道在拟合曲线时如何添加数据误差。此外,错误是相关的。

import numpy as np
from math import pi
from math import log
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

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

F2=np.array([ 112.47114172,  108.74591788,  107.34258013,  108.03149139,
        102.18850331,  100.04522384,   91.4210755 ]) #ydatapoints
mudr2=np.array([ 40.05298013,  32.10596026,  31.11258278,  26.94039735,
        19.47019868,  13.66887417,   3.81456954]) #xdata points
dmss=array([ 426541.68767453,  437276.93612855,  564193.26435977,
        438509.51364708,  445470.26098272,  446286.50588426,
        567838.01678291]) #s-variables in fit
p0=[0,70] #initial guess

def Ffits(p,x,s,B=2.58,Fc=88,mu=770,Za=0.9632):
    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/Za*(1+p[0]*s)*(1+temp2+temp3+temp4)+p[1]

errfuncfeps = lambda p,x,s,y: (Ffits(p,x,s)-y)
p32s,success32 = scipy.optimize.leastsq(errfuncfeps, p0, args=(mudr2,dmss,F2))
plt.plot(mudr2,F2,"b^")
plt.plot(mudr2,Ffits(p32s,mudr2,dmss))

dmsse=np.array([  6.70820393,   7.61577311,   7.28010989,   6.32455532,
         9.48683298,  10.77032961,  11.18033989]) #error for dmss
mudr2e=np.array([23.0, 18.0, 18.0, 15.0, 12.0, 8.0, 3.0]) #error for xdata
F2e=np.array([ 34.,  39.,  39.,  25.,  37.,  38.,  62.]) #error for ydata

如果有人可以帮助我将xdata和dmss的相关错误添加到合适的位置,我将不胜感激。非常感谢任何帮助!

0 个答案:

没有答案