MATLAB: Symsum is slow

performanceslowsumSymbolic Math Toolboxsymsumterms

Why is "symsum" so slow?
>> syms k
>> x2 = -.5:.5/1000:.5;
>> fun(k) = exp(-((((k-1)*x2).^2)/.01));
>> tic; p = symsum(fun(k),k,-1,1); toc; % almost 5 seconds
>> tic; p = fun(-1)+fun(0)+fun(1); toc; % about 1 second

Best Answer

"symsum" is the wrong function to use if you want to add up a few terms. It spends time to look for a general formula that is valid for arbitrarily many terms, and then applies that formula to the given number of terms.
"symsum" is much better when there are many terms.
For fewer terms, use the "sum" function:
>> tic; p = sum(subs(fun(k),k,(-1:1)')); toc; % about 1 second
Please see the following link for more information: