How to write a custom non linear function for data fitting?

3 ビュー (過去 30 日間)
Jacopo Tabaglio
Jacopo Tabaglio 2020 年 3 月 23 日
コメント済み: Jacopo Tabaglio 2020 年 3 月 23 日
I wrote this script to fit some data with a custom nonlinear function, but I'm getting an almost flat line instead than an exponential.
myfittype=fittype('(N/(1 + exp((-N)*(b)*(t - tf))))','dependent',{'n'},'independent',{'t'},'coefficients',{'N','b','tf'});
h=fit(t,n,myfittype)
plot(h,t,n)

回答 (1 件)

the cyclist
the cyclist 2020 年 3 月 23 日
編集済み: the cyclist 2020 年 3 月 23 日
I don't have the Curve Fitting Toolbox, so I can't really comment on your current code. But, if you also have the Statistics and Machine Learning Toolbox, you could try the fitnlm function.
% Some pretend data
t_data = (-2 : 0.1 : 10)';
f_data = 8 ./ (1 + exp(-2*(t_data - 5))) + 0.2*randn(size(t_data));
% Fitting function
f = @(F,t) F(1)./(1 + exp(-F(2).*(t - F(3))));
% Initial guess at parameters
beta0 = [1 1 1];
% Fit the model
mdl = fitnlm(t_data,f_data,f,beta0);
% Plot the fit against the data
figure
hold on
plot(t_data,f_data,'.')
plot(t_data,predict(mdl,t_data))
  3 件のコメント
John D'Errico
John D'Errico 2020 年 3 月 23 日
Note that nonlinear fits often require an intelligent choice of starting values. The curvefitting toolbox uses random choice of initial values for general models if you give it nothing.
Jacopo Tabaglio
Jacopo Tabaglio 2020 年 3 月 23 日
Thanks, understood

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

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by