MATLAB: Efficiently combine anonymous functions

function

Hi
I have an algorithm which has several anonymous functions passed as parameters which are then combined depending on flags. Since the resulting function is called many times, this slows down my program quite significantly. Is there a way to "explicitely combine" two anonymous functions?
Mini Example:
% Single anonymous function
full1 = @(x)sin(x) + 3*x;
% split anonymous function
g1 = @(x)sin(x);
g2 = @(x)3*x;
full2 = @(x)g1(x) + g2(x);
%%Speed Test
N = 100000;
disp('Explicit combination')
tic
for i = 1:N
full1(rand);
end
toc
disp('Multilevel combination')
tic
for i = 1:N
full2(rand);
end
toc
returns
Explicit combination
Elapsed time is 0.234414 seconds.
Multilevel combination
Elapsed time is 0.434422 seconds.
What I would like is to somehow have g1(x) + g2(x) be "combined explicitely" such that I end up with a version as "full1", and not "full2". Is there a way to do that?
Thanks!

Best Answer

If you have the symbolic toolbox, then sometimes it works to call
syms x
full1 = matlabFunction( simplify(full2(x)), 'vars', x);
You can skip the simplify() for functions you do not expect will benefit from optimization such as finding common factors.
This will not always work as some functions cannot be called with symbolic parameters, or may have different results for symbolic parameters.
If you do not have the symbolic toolbox, then you can char() the individual anonymous functions, strip off the '@(x)', and combine remembering to put in parenthesis so you do not accidentally generate the wrong order of operations. Then put the @(x) back on and str2func()