我是 python 新手,正在尝试使用 lmfit 拟合数据。我在这里关注 lmfit 教程:http://lmfit.github.io/lmfit-py/parameters.html 这 是我的代码(基于上面链接中解释的代码):
import numpy as np
import lmfit
import matplotlib.pyplot as plt
from numpy import exp, sqrt, pi
from 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
问题:它返回 line return model-y 的语法错误
如果你能让我朝着正确的方向前进,我将不胜感激。