用symfi用两个模型函数拟合两个不同的数据集

2024-05-15 14:02:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用两个模型函数来拟合两个数据集。我试着用symfit来做。代码如下:

from symfit import variables, parameters, Fit, cos, sin, pi, sqrt, asin
import numpy as np
n0 = 1.5

data = np.genfromtxt('some data')
data = 1000 * data

pos=[]
for j in range( len(data) ): 
    pos.append( np.arcsin( np.sin( np.deg2rad( data[j,0]/1000 ) )/1.5 ) )

m1=[]
for j in range( len(data) ): 
    m1.append( data[j,1] ) 

p1=[]
for j in range( len(data) ): 
    p1.append(data[j,3]) 

zero=[]
for j in range( len(data) ): 
    zero.append(data[j,5]) 

y0, lam, d0, deltan, per = parameters('y0, lam, d0, deltan, per')
theta, rM1, rP1 = variables('theta, rM1, rP1')

model_dict={
    rM1: y0+((pi*deltan*d0)/(lam*cos(theta)))**2.*sin(sqrt(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(-asin(lam/(2*per*n0))-theta))/per)**2.))**2./(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(-asin(lam/(2*per*n0))-theta))/per)**2.),
    rP1: y0+((pi*deltan*d0)/(lam*cos(theta)))**2.*sin(sqrt(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(asin(lam/(2*per*n0))-theta))/per)**2.))**2./(((pi*deltan*d0)/(lam*cos(theta)))**2.+((pi*d0*(asin(lam/(2*per*n0))-theta))/per)**2.)
 }


fit = Fit(model_dict, theta=pos, rM1=m1, rP1=p1)
fit_result = fit.execute()
print(fit_result)

但是,我得到以下错误:

AttributeError: 'Variable' object has no attribute 'cos'

如果我删除cos函数,那么我将得到另一个关于sqrtsin等的类似错误。我无法找出代码的错误!你知道吗

附言: 在使用symfit.cos等之后,我得到以下结果:

/anaconda2/lib/python2.7/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in arcsin

以及

/anaconda2/lib/python2.7/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in sqrt

输出为:

Parameter Value        Standard Deviation
d0        1.727015e+00 nan
deltan    1.880867e-02 3.534201e-03
lam       3.890715e-01 nan
per       6.123022e-01 nan
y0        -1.686541e-03 4.810316e-03
Fitting status message: Desired error not necessarily achieved due to precision loss.
Number of iterations:   105
Regression Coefficient: 0.100163679536

加上标准差后,我得到:

Parameter Value        Standard Deviation
d0        nan nan
deltan    nan nan
lam       nan nan
per       nan nan
y0        nan nan
Fitting status message: Desired error not necessarily achieved due to precision loss.
Number of iterations:   112
Regression Coefficient: nan 

Tags: indatanppisqrtsincosnan