指数拟合-指数求和

时间:2018-07-09 15:55:34

标签: curve-fitting exponential

我要拟合以下数据:

x(time) y(data)    
0.75;   19.33    
1;      19.04   
1.25;   17.21   
1.5;    12.98   
1.75;   11.59   
2;  9.26   
2.25;   7.66   
2.5;    6.59    
2.75;   5.68    
3;  5.1    
3.25;   4.36   
3.5;    4.43     
3.75;   3.58    
4;  3.01    
4.25;   3.24     
4.5;    3.58     
4.75;   3.13     
5;  3.88     
5.25;   3.19     
5.5;    3.58     
5.75;   3.64   

使用以下代码:

#read text file
data = pd.read_table('episode_5_prova.txt', sep='\t')
#DataFrame
df = pd.DataFrame(data)
#Define your function
def func(x, a, b, c, d, e):
return a*np.exp(-b*x) + c*np.exp(-d*x) + e

#convert dataframe into numpy array
df0=df['time']
x=df0.as_matrix()
df1=df['bi']
y=df1.as_matrix()

# Using the python scipy function curve_fit with function and input variables
popt, pcov = curve_fit(func, x, y)
a, b, c, d, e= popt
fit = func(x, a, b, c, d, e)

fig, ax = plt.subplots()
ax.plot(x, fit, color='r', lw=3)
ax.plot(x, y,'g.')
observed_values=scipy.array(y)
expected_values=scipy.array(fit)
plt.xlim(0,25)
plt.ylim(0,20)

print(a,b,c,d,e)

print(scipy.stats.chisquare(observed_values, f_exp=expected_values, ddof=3))
plt.show()

我得到以下图: first fit 但是,出于我的工作目的,我需要将参数b和c固定为: b = 0.000431062,d = 0.000580525但我没有获得很好的拟合如下: second fit

有人有建议吗? 谢谢

1 个答案:

答案 0 :(得分:1)

这是 this question 的副本。

简而言之,您需要一个特征值求解器、一个最小二乘求解器并按照解决方案中列出的步骤进行操作。

您可以将 Matlab code 用作示例以在其他语言(例如 Python)中实现。

clear all;
clc;
% get data
dx = 0.02;
x  = (dx:dx:1.5)';
y  =  5*exp(0.5*x) + 4*exp(-3*x) + 2*exp(-2*x) - 3*exp(0.15*x);

% calculate integrals
iy1 = cumtrapz(x, y);
iy2 = cumtrapz(x, iy1);
iy3 = cumtrapz(x, iy2);
iy4 = cumtrapz(x, iy3);

% get exponentials lambdas
Y = [iy1, iy2, iy3, iy4, x.^3, x.^2, x, ones(size(x))];
A = pinv(Y)*y;
%lambdas =
%  -2.9991
%  -1.9997
%   0.5000
%   0.1500

lambdas = eig([A(1), A(2), A(3), A(4); 1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0]);
lambdas

% get exponentials multipliers
X = [exp(lambdas(1)*x), exp(lambdas(2)*x), exp(lambdas(3)*x), exp(lambdas(4)*x)];
P = pinv(X)*y;
P
%P =
%   4.0042
%   1.9955
%   4.9998
%  -2.9996