# Strange NSum behavior for slightly larger number of terms

For some sums, NSum gives me the result nicely for some number of terms, but if I add one more term, it suddenly becomes much slower. I haven't found the minimum example for which this can be reproduced, but here is a problem I was just trying to compute:

NSum[Sin[x0*n*Pi]*Sin[d*n*Pi/2]*Exp[(n*Pi)^2 *t]*(2 - Cos[n*Pi] -
2*BesselJ[1, n*Pi/2]*Sin[n*Pi/2]), {n, 1, 24}]


Here t=-0.3, d=0.1 and x0=0.5. For 24 terms, this evaluates just fine (and gives me 0.015118), but for 25 terms, the calculation takes much longer. It seems to me that adding just one more term to the sum should not take much longer than the original calculation. What is going on here? I'm using Mathematica version 9.0.1.0.

• Change the method, or add an explicit NSumTerms within ten of desired term number.
– ciao
Jun 11, 2014 at 9:53

Way 1

t = -0.3; d = 0.1 ; x0 = 0.5;
NSum[Sin[x0*n*Pi]*Sin[d*n*Pi/2]*
Exp[(n*Pi)^2*t]*(2 - Cos[n*Pi] - 2*BesselJ[1, n*Pi/2]*Sin[n*Pi/2]), {n, 1, 100},
Method -> "WynnEpsilon"(*or AlternatingSigns*)]


NSum will use some algorithm.The default settings of the option Method is Automatica.But Automatica is not have better performance in any case,so we need to change this option.

Way 2

t = -0.3; d = 0.1 ; x0 = 0.5;
Sum[Sin[x0*n*Pi]*Sin[d*n*Pi/2]*
Exp[(n*Pi)^2*t]*(2 - Cos[n*Pi] - 2*BesselJ[1, n*Pi/2]*Sin[n*Pi/2]), {n, 1, 100}]

• Thank you. Using the WynnEpsilon method seems to do the trick. Apparently NSum, by default, changes the algorithm when some number of terms is reached and it seems that the heuristic that is used to determine this algorithm fails for some reason. Jun 12, 2014 at 10:49
• What is interesting is that even for a HUGE number of terms (I tried with 10^6), specifying the method gives the solution much faster than the default option. Jun 12, 2014 at 10:52
• Jun 12, 2014 at 11:35