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

 Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Jul 2024
Edited: MathWorks Support Team on 29 Aug 2024
"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 run the below command in the command window of installed MATLAB R2019a version to get release specific documentation of using the "sum" function.
>> web(fullfile(docroot, 'symbolic/symbolic-summation.html'))
 Please follow the below link to search for the required information regarding the current release:

More Answers (0)

Categories

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!