【问题标题】:exponential curve fitting with python用python拟合指数曲线
【发布时间】:2023-04-09 23:55:01
【问题描述】:

我正在尝试将一些用于曲线拟合我的数据的 Matlab 代码转换为 python 代码,但无法获得类似的答案。数据为:

x = array([   0.  ,   12.5 ,   24.5 ,   37.75,   54.  ,   70.25,   87.5 ,
    108.5 ,  129.5 ,  150.5 ,  171.5 ,  193.75,  233.75,  273.75])
y = array([-8.79182857, -5.56347794, -5.45683824, -4.30737662, -1.4394612 ,
   -1.58047016, -0.93225927, -0.6719836 , -0.45977157, -0.37622436,
   -0.56115757, -0.3038559 , -0.26594558, -0.26496367])

Matlab 代码为:

function [estimates, model] = curvefit(xdata, ydata)
% fits data to the curve y(x)=A-B*e(-lambda*x)

start_point = rand(1,3);

model =@efun;
options = optimset('Display','off','TolFun',1e-16,'TolX',1e-16);
estimates = fminsearch(model, start_point,options);
% expfun accepts curve parameters as inputs, and outputs sse,
% the sum of squares error for A -B* exp(-lambda * xdata) - ydata, 
% and the FittedCurve. 
    function [sse,FittedCurve] = efun(v)
        A=v(1);
        B=v(2);
        lambda=v(3);
        FittedCurve =A - B*exp(-lambda*xdata);
        ErrorVector=FittedCurve-ydata;
        sse = sum(ErrorVector .^2);
    end
end
err = Inf;
numattempts = 100;
for k=1:numattempts
[intermed,model]=curvefit(x, y));
[thiserr,thismodel]=model(intermed);
if thiserr<err
    err = thiserr;
    coeffs = intermed;
    ymodel = thismodel;
end

到目前为止,我在 Python 中拥有:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
from scipy.optimize import curve_fit
import pickle

def fitFunc(A, B, k, t):
    return A - B*np.exp(-k*t)
init_vals = np.random.rand(1,3)
fitParams, fitCovariances = curve_fit(fitFunc, y, x], p0=init_vals)

我想我必须在 p0 上运行 100 次尝试,但曲线只收敛大约 1/10 次,它收敛到一条直线,与我在 Matlab 中得到的值相差甚远。我见过的大多数关于曲线拟合的问题都使用 Bnp.exp(-kt) + A,但我上面的指数公式是我必须用于这些数据的公式。有什么想法吗?感谢您的宝贵时间!

【问题讨论】:

    标签: python matlab least-squares


    【解决方案1】:

    curve_fit(fitFunc, y, x], p0=init_vals) 应该是 curve_fit(fitFunc, x,y, p0=init_vals) 即, x 在 y 之前。 fitFunc(A, B, k, t) 应该是 fitFunc(t,A, B, k)。自变量首先出现。请看下面的代码:

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    
    x = np.array([   0.  ,   12.5 ,   24.5 ,   37.75,   54.  ,   70.25,   87.5 ,
        108.5 ,  129.5 ,  150.5 ,  171.5 ,  193.75,  233.75,  273.75])
    y = np.array([-8.79182857, -5.56347794, -5.45683824, -4.30737662, -1.4394612 ,
       -1.58047016, -0.93225927, -0.6719836 , -0.45977157, -0.37622436,
       -0.56115757, -0.3038559 , -0.26594558, -0.26496367])
    
    def fitFunc(t, A, B, k):
        return A - B*np.exp(-k*t)
    init_vals = np.random.rand(1,3)
    
    fitParams, fitCovariances = curve_fit(fitFunc, x, y, p0=init_vals)
    print fitParams
    plt.plot(x,y)
    plt.plot(x,fitFunc(x,*fitParams))
    plt.show()
    

    【讨论】:

    • 欢迎来到python!请阅读这些函数和示例的文档。它们非常有用。
    • 你能选择一个答案并接受它,这样人们就知道问题已经完成了吗?
    猜你喜欢
    • 1970-01-01
    • 2014-09-08
    • 2018-11-13
    • 2016-02-13
    • 1970-01-01
    • 2015-03-22
    • 2020-05-05
    • 2015-06-20
    • 2021-01-30
    相关资源
    最近更新 更多