How to speed up function approximation?

4 ビュー (過去 30 日間)
Adrian T
Adrian T 2019 年 8 月 19 日
編集済み: Matt J 2019 年 8 月 19 日
I have a code in MATLAB2016:
...
tic
is_t1(:,i)=(is_t1(:,i)/mmax);
[f,r2]=fit(tr,is_t1(:,i),'1-b*exp(-((a*x)/(var_c)))','StartPoint',[0.8 0.8]);
T1(i)=-(var_c*log(-(0.6321 1)/f.b))/f.a;
toc
...
end
Its execution time: ~0,031s
Number of approximation points in a given task: ~7000-10000
How to ,speed up this operation?
Thanks for any help

採用された回答

Matt J
Matt J 2019 年 8 月 19 日
編集済み: Matt J 2019 年 8 月 19 日
There are a few inefficiencies that I see. Firstly, you shouldn't pick an arbitrary StartPoint like [0.8,0.8]. The problem can be log-transformed into a linear equation and solved algebraically for a much more informed initial guess,
xdata=tr;
ydata=1-is_t1(:,i);
z=[x(:), ones(size(xdata(:)))]\log(ydata);
a0=-z(1)*var_c;
b0=exp(z(2));
In fact, depending on your needs and the noisiness of your data, the above analytical solution might be accurate enough already. Maybe you don't need to use an iterative routine like fit() after all.
But if the data noise is significant and you wish to further refine a0 and b0 with iterative nonlinear fitting, I would recommend fminspleas
which will let you iterate over a only,
modelfun=@(a) exp((-a/var_c)*x);
[a,b] = fminspleas({modelfun}, a0, xdata,ydata);
Notice also that we've optimized the implementation of your modelfun() a bit. Here, it only executes 2 vectorized operations whereas your original implementation had 5.

その他の回答 (2 件)

Chris
Chris 2019 年 8 月 19 日
I dont have those toolboxes but often you can start an optimization problem with a randomly selected sub-set of data run for a short time to get a better initial guess before running the algorithm over all your data. YMMV. Take care with this and make sure you understand your data and how you are modeling it.
  1 件のコメント
Adrian T
Adrian T 2019 年 8 月 19 日
Hello
Thank you for your response.
1. Numeric data are well matched - R2> 0.9. I know the nature of the phenomenon.
2. random data - no result.

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


Yair Altman
Yair Altman 2019 年 8 月 19 日
It may be useful to replace the string with a function handle.
Also, setting the convergence tolerances to smaller values than the defaults might converge faster without a meaningful degradation of the result.
Lastly, read https://undocumentedmatlab.com/blog/speeding-up-builtin-matlab-functions-part-1 where I discuss different ways of significantly speeding-up the fitdist function. It's not the same as your fit but similar ideas can possibly also be useful in your case.
  1 件のコメント
Adrian T
Adrian T 2019 年 8 月 19 日
Thank you very much
I need to devote some time to this..

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by