symsum is slow

9 visualizaciones (últimos 30 días)
MathWorks Support Team
MathWorks Support Team el 23 de Abr. de 2018
Editada: MathWorks Support Team el 17 de Jun. de 2019
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

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 18 de Jun. de 2019
Editada: MathWorks Support Team el 17 de Jun. de 2019
"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:

Más respuestas (0)

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by