问题
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