5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Change the method, or add an explicit NSumTerms within ten of desired term number. $\endgroup$ – ciao Jun 11 '14 at 9:53
2
$\begingroup$

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}]
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Echows Jun 12 '14 at 10:49
  • 1
    $\begingroup$ 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. $\endgroup$ – Echows Jun 12 '14 at 10:52
  • $\begingroup$ @Echows reference.wolfram.com/mathematica/tutorial/… $\endgroup$ – Apple Jun 12 '14 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.