代码之家  ›  专栏  ›  技术社区  ›  najeem

基于后向依赖的scipy曲线拟合

  •  1
  • najeem  · 技术社区  · 7 年前

    即 n个 =吨 +三角洲。

    如果我用 example curve_fit . 会是类似的事情。

    def func(x, a, b, c):
        return a * np.exp(-b * x) + c #+ func(x[n-1], a, b, c) <<< need help here
    
    xdata = np.linspace(0, 4, 50)
    y = func(xdata, 2.5, 1.3, 0.5)
    np.random.seed(1729)
    y_noise = 0.2 * np.random.normal(size=xdata.size)
    ydata = y + y_noise
    popt, pcov = curve_fit(func, xdata, ydata)
    print(popt)
    plt.plot(xdata, ydata, 'b-', label='data')
    plt.plot(xdata, func(xdata, *popt), 'r-',
             label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   MB-F    7 年前

    如果你有一个微分方程,你需要先找到积分,然后再把它与数据拟合。除非数据也是差分的,在这种情况下你可以只拟合。

    delta 是由 a * np.exp(-b * x) + c curve_fit 通行证 全部的 func 希望它能回来 全部的

    def delta_func(x, a, b, c):
        return a * np.exp(-b * x) + c
    
    def func(x, a, b, c):
        y = np.empty(x.shape)
        y[0] = delta_func(0, a, b, c)
        for i in range(1, len(x)):
            y[i] = y[i-1] + delta_func(x[i], a, b, c)
        return y
    

    这是为了说明。你可以用 np.cumsum :

    def func(x, a, b, c):
        return np.cumsum(a * np.exp(-b * x) + c)