曲线拟合方程不正确拟合曲线

Curve fit equation not properly fitting curve

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

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])

我明白了

谁能看出为什么?曲线拟合急剧变化?

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

(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)