Fitting two different data sets by two model functions using symfit

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 11:18:57

问题


I am trying to fit two data sets by two model functions. I tried to do so using symfit. Here the code:

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)

But, I get the following error:

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

If I remove the cos function, then I will get other similar error concerning sqrt and sin etc. I cannot figure out what is wrong with the code!

PS: After using symfit.cos etc., I get the following results:

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

as well as

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

and the output is:

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

And after adding standard deviations, I got:

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 

来源:https://stackoverflow.com/questions/52146955/fitting-two-different-data-sets-by-two-model-functions-using-symfit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!