简体   繁体   English

使用lmfit python进行曲线拟合

[英]curve fitting with lmfit python

I am new to python and trying to fit data using lmfit. 我是python的新手,正在尝试使用lmfit拟合数据。 I am following on the lmfit tutorial here: http://lmfit.github.io/lmfit-py/parameters.html and this is my code (based on the code explained in the above link): 我在下面的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

Problem: it return syntax error for line return model-y 问题:返回行返回模型-y的语法错误

I appreciate if you could please let me to right direction. 如果可以,请允许我向正确的方向致谢。

You have forgotten a right parenthesis ")" in the previous line (result= ...). 您在上一行中忘记了右括号“)”(结果= ...)。 Opening and closing parentheses are unbalanced causing a syntax error. 括号之间的不平衡导致语法错误。

I think there is a parenthesis problem in the previous line. 我认为在前一行中有一个括号问题。 This causes the return to be included in the formula. 这将使返回值包含在公式中。 I think there's a ) missing at the end. 我认为结尾处缺少)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM