Fitting to exp plus a constant

96 ビュー (過去 30 日間)
Jason
Jason 2025 年 12 月 17 日 12:18
編集済み: Matt J 2025 年 12 月 18 日 13:59
Hello, I am trying to fit some data to an exponential + constant function and have this approach that I came across:
% code is giving good results with template equation : % y = a.*(1-exp(b.*(x-c))) + d;
f = @(a,b,c,d,x) a.*(1-exp(b.*(x-c))) + d;
obj_fun = @(params) norm(f(params(1), params(2), params(3), params(4),x)-y);
sol = fminsearch(obj_fun, [y(end)-y(1),0,0,y(1)]);
a_sol = sol(1);
b_sol = sol(2);
c_sol = sol(3);
d_sol = sol(4);
y_fit = f(a_sol, b_sol,c_sol ,d_sol, x);
figure
plot(x,y,'-+b',x,y_fit,'-r'); grid on;
legend('signal','aexp(b(x-c))+d');
title(['a=',num2str(a_sol,'%0.2f'), ' b=',num2str(b_sol),' c=',num2str(c_sol),' d=',num2str(d_sol)],'FontSize',14,'Color','b')
Whilst the fit is good, the value of "a" seems wrong (at the expense of "d")
I've tried another approach that I came across from Steven Lord (as i don't really understand the code above)
D=[x y]
% It would be better to parametrize the function as
xmin=min(x);
fitfun = fittype( @(a,b,c,x) a*exp(b*(x-xmin))+c );
[fitted_curve,gof] = fit(D(:,1),D(:,2),fitfun,'StartPoint',[max(y) -2 min(y)])
plot(fitted_curve,'r-')
But I get this error:
Error using fit>iFit (line 362)
Inf computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients.
Error in fit (line 117)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in PlotAnalysis/FitExp1ContstantButtonPushed (line 1361)
[fitted_curve,gof] = fit(D(:,1),D(:,2),fitfun,'StartPoint',[max(y) -2 min(y)])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

回答 (1 件)

Matt J
Matt J 2025 年 12 月 17 日 12:42
編集済み: Matt J 2025 年 12 月 18 日 13:59
It looks like the fit was successful, but the model function is overparametrized, so there is no specific value for a (or c) that you can count on,
where . Whatever the decay rate b needed for the fit, there are infinite choices for a and c that will produce the same A (and thus the same curve).
  12 件のコメント
Jason
Jason 2025 年 12 月 17 日 17:06
Hi Matt, could I ask what the 0.005 is in this please, and where the constants a & c are
[b,ac]=fminspleas( {@(b,x) exp(-b*x),1} ,0.005, x,y,0); %Do fit
Matt J
Matt J 2025 年 12 月 17 日 18:05
編集済み: Matt J 2025 年 12 月 17 日 18:06
I think you would get a better handle on that if you familiarized yourself with the fminspleas documentation. Briefly, though,
(1) 0.005 is the initial guess for b
(2) a and c are the coefficients applied to the exp(-b*x) and the 1 respectively in the input {@(b,x) exp(-b*x),1}. Their fitted values are returned in "ac". They do not require initial guess values.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by