【问题标题】:Curve fit equation not properly fitting curve曲线拟合方程未正确拟合曲线
【发布时间】:2015-03-02 23:24:50
【问题描述】:

我正在使用此代码来曲线拟合一些数据:

def extract_parameters(Ts, ts):
    def model(t, Ti, Ta, c):
        return (Ti - Ta)*math.e**(-t / c) + Ta
    popt, pcov = cf(model, Ts, ts, p0 = (10, 6, 7))
    Ti, Ta, c = popt
    maxx = max(Ts)
    xfine = np.linspace(0, maxx, 101)
    print "xfine: ", xfine
    yfitted = model(xfine, *popt)
    print "yfittted", yfitted
    pl.plot(Ts, ts, 'o', label = 'data point')
    pl.plot(xfine, yfitted, label = 'fit')
    pylab.legend()
    pylab.show()
    return Ti, Ta, c

当我进入时:

 extract_parameters([1,2,3,4,5,6],[100,60,50,40,45,34])

我很适合

但是当我进入时:

extract_parameters([1,2,3,4,5,6,7],[100,80,70,65,60,58,56])

我得到了这个

谁能明白为什么?曲线拟合急剧变化?

【问题讨论】:

    标签: python


    【解决方案1】:

    curve_fit 这样的优化器试图找到最佳匹配,但并不总是成功。 注意extract_parameters([1,2,3,4,5,6],[100,60,50,40,45,34]) 返回

    (196.85292746741234, 38.185643828689777, 1.0537367332516778)
    

    这意味着它以p0 = (10, 6, 7) 的初始参数猜测开始 并找到了通往完全不同的位置的路。

    您可以通过选择更接近最优值的初始猜测来帮助优化器。只需将其更改为

    p0 = (100, 6, 7)
    

    允许

    extract_parameters([1,2,3,4,5,6,7],[100,80,70,65,60,58,56])
    

    要找到更合适的:(131.71232607048836, 54.894539483338022, 1.8503931318517444)

    【讨论】:

    • 完美,看起来不错。 p0 实际上做了什么?它猜测的是什么值?谢谢。
    • p0 是参数的初始猜测,Ti, Ta, c
    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 2021-01-19
    • 1970-01-01
    • 2013-09-04
    • 2013-11-08
    • 2017-04-21
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多