フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

I wrote 2 snippets as below, they both try to find minimum value, but one repetitively uses function handles and the other uses a sub function. It turns out the speed are very different? Why?

1 回表示 (過去 30 日間)
Shawn Miller
Shawn Miller 2016 年 2 月 25 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Two snippets are as follow (they are not my original codes and are used for illustration only), the input y is just an array, for example, you can run method1(rand(500,1)) and method2(rand(500,1)), respectively. Interestingly, although both do the same job, when y is very large, method1 would take unbearable long time but method2 works pretty fine. What makes this huge difference?
Code 1
function out=method1(y)
f=@(x)0;F=@(x)0;
for i=1:length(y)
f=@(x)x(1)*f(x)^2+x(2)*y(i)^2;
F=@(x)x(1)*F(x)+x(2)*log(f(x)^2+1)-10*y(i)^3;
end
options=optimoptions(@fminunc,'Algorithm','quasi-newton');
out=fminunc(F,[0,0],options);
end
Code 2
function out=method2(y)
options=optimoptions(@fminunc,'Algorithm','quasi-newton');
out=fminunc(@(x)subfun(x,y),[0,0],options);
end
function F=subfun(x,y)
f=0;F=0;
for i=1:length(y)
f=x(1)*f^2+x(2)*y(i)^2;
F=x(1)*F+x(2)*log(f^2+1)-10*y(i)^3;
end
end
  4 件のコメント
Shawn Miller
Shawn Miller 2016 年 2 月 25 日
編集済み: Shawn Miller 2016 年 2 月 25 日
I got this question when I tried to recode MATLAB's built in estimate(mdl,y) function used in GARCH model. As I said, my code shown here is just for illustration, it has no sense. However, it may has some sense in some way. Please note that in the loop, f is redefined but did use previous f(x) value, and F is redefined and also used previous F(x) and f(x) value. In other words, one can't write final F(x) directly. The final F(x) is derived step by step -- it's a recurrence relation. In terms of x, because it's to be estimated using fminunc(), so I need to write it using a function handle.
I guess the slow speed in method1 may be due to function handle. But I am not sure and wonder if function handle performs poor in speed and needs to be avoided whenever it's possible.
Shawn Miller
Shawn Miller 2016 年 2 月 25 日
編集済み: Shawn Miller 2016 年 2 月 25 日
Also think in this question, I have a column of number a1,a2,...,an; and a column of functions b1(x),..., bn(x). How do I get these functions? Well, I used relations as below b1(x)=f(a1,x),b2(x)=f(a1,b1(x)),b3(x)=f(a2,b2(x)),...,bn(x)=f(an-1,bn-1(x)). Now I'd like to find x such that g(b1(x),...,bn(x)) reaches minimum, g is just a function on these b functions. In brief, one has to use a loop to first get every b function, and then plug in g() and treat x as unknown and find minimum.

回答 (1 件)

Philip Borghesani
Philip Borghesani 2016 年 2 月 25 日
Profile your code, think about what the code is doing. Method2 makes one function call to subfun per iteration. Method1 makes many thousands of function calls using the many anonymous functions you have created per iteration. There is no comparison in the amount of work being done. Recursive solutions are rarely the most efficient solution to a problem.
I do find that Method1 runs significantly faster in R2015b then in previous versions but it is still much slower then Method2.
  1 件のコメント
Shawn Miller
Shawn Miller 2016 年 2 月 26 日
How do I profile a function? Also, when you said "Method2 makes one function call to subfun per iteration", what do you mean by "per iteration"? Do you mean every time fminunc() tries a point to see if it's minimum?

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by