model - curve fitting with lmfit python -
i new python , trying fit data using lmfit. following on lmfit tutorial here: http://lmfit.github.io/lmfit-py/parameters.html , code (based on code explained in above link):
import numpy np import lmfit import matplotlib.pyplot plt numpy import exp, sqrt, pi lmfit import minimize,parameters,parameter,report_fit data=np.genfromtxt('test.txt',delimiter=',') x=data[:,][:,0] y=data[:,][:,1] def fcn2fit(params,x,y): """model decaying sine wave, subtract data""" s1=params['s1'].value t0=params['t0'].value t1=params['t1'].value s2=params['s2'].value t2=params['t2'].value model = 1-(1-s1)*exp(-(x-t0)/t1)+s2*(1-exp(-(x-t0)/t2) return model - y params = parameters() params.add('s1', value=0.85, min=0.8, max=0.9) params.add('t0', value=0.05, min=0.01, max=0.1) params.add('t1', value=0.2, min=0.1, max=0.3) params.add('s2', value=0.03, min=0.01, max=0.05) params.add('t2', value=0.3, min=0.2, max=0.4) result = minimize(fcn2fit, params, args=(x,y)) final = y + result.residual report_fit (params) try: import pylab pylab.plot(x,y, 'k+') pylab.plot(x,final, 'r') pylab.show() except: pass
problem: return syntax error line return model-y
i appreciate if please let me right direction.
i think there parenthesis problem in previous line. causes return included in formula. think there's ) missing @ end.
Comments
Post a Comment