costly anonymous function declaration for solving a equation system with parameters

Hi i have to solve a equation system (which is a matlab functiion)for many parameters values, so i run a loop over the parameter values, then use an anonymous function to pass the parameters to the equation system, then use fsolve to solve this anonymous function. now i understand that solving takes some time, but setting up the anonymous function takes almost the same time. why? can this be done faster?
The script looks like this
for i=1:ni
parameters= par_fun(i);
my_anon_fun = @my_fun(x)(x,parameters);
[sol fval exitflag]= fsolve(my_anon_fun,starval);
end
where my_fun is the eqauation system and par_fun give the parameter vector for the equation system. The profiler shows a lot of time spent in the line my_an_fun = @myfun(x)(x,parameters);
Thanks Dominik

1 件のコメント

Does your line perhaps instead look like
my_anon_fun = @(x) my_fun(x,parameters);

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

 採用された回答

Alan Weiss
Alan Weiss 2013 年 12 月 5 日

1 投票

Perhaps it would be faster for you to use nested functions rather than anonymous functions. I think (thought I haven't tested this idea) that this would avoid the overhead of creating anonymous functions.
Alan Weiss
MATLAB mathematical toolbox documentation

1 件のコメント

dominik
dominik 2013 年 12 月 9 日
Just tried it, seems to speed things up indeed! Thanks a lot!!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

質問済み:

2013 年 12 月 5 日

コメント済み:

2013 年 12 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by