NIntegrate inside NSum

Consider the following function with a numerical integration:

BDMAF[n_, γ_, x_, c_] :=
Module[{K1 = EllipticK[1/Sqrt[1 + γ^2]],
E1 = EllipticE[1/Sqrt[1 + γ^2]], y0},
y0 = ((π^2 (IntegerPart[x + 1] - x + n))/(2 K1 E1))^(1/2);
Exp[-y0^2] NIntegrate[
Exp[y^2] Sin[(n + IntegerPart[x + 1])/2 π + c y]^2, {y, 0, y0}]
]


that works perfectly if all parameters are explicitly defined:

BDMAF[1, 1, 1, 1]


0.0391914

I need to calculate the infinite sum over n. Naive approach gives

NSum[BDMAF[n, 1, 1, 1], {n, 0, ∞}]


NIntegrate::nlim: y = 1.38268 Sqrt[1. +n] is not a valid limit of integration. >>

Following the answer to the question about nested NIntegrate, I tried to redefine BDMAF as follows:

BDMAF[n_?NumericQ, γ_, x_, c_] := BDMAF[n, γ, x, c] =
Module[{K1 = EllipticK[1/Sqrt[1 + γ^2]],
E1 = EllipticE[1/Sqrt[1 + γ^2]], y0},
y0 = ((π^2 (IntegerPart[x + 1] - x + n))/(2 K1 E1))^(1/2);
Exp[-y0^2] NIntegrate[
Exp[y^2] Sin[(n + IntegerPart[x + 1])/2 π + c y]^2, {y, 0, y0}]
]


The problem remains, but I receive another error message:

NSum[BDMAF[n, 1, 1, 1], {n, 0, ∞}]


NIntegrate::inumr: The integrand BDMAF(n,1,1,1) has evaluated to non-numerical values for all sampling points in the region with boundaries (15. 4.64782*10^14). >>

How BDMAF function should be defined?

• The problem is your integral. Try to evaluate BDMAF[4.64782*10^14, 1, 1, 1] and see, why it results in a non-numerical value. Nov 4, 2012 at 13:56
• You're sure this sum of yours converges? Some experimentation with various settings of NSum[] and NIntegrate[] are yielding somewhat conflicting results on my end... Nov 4, 2012 at 14:20
• Table[BDMAF[n, 1, 1, 1], {n, 0, 120}] // ListLinePlot produces this i.stack.imgur.com/P0sRg.png which as @J.M. points out suggests slow convergence if any. Nov 4, 2012 at 15:32
• Thanks. I rechecked the derivations and figured out that instead of Sin^2 there should be Cos^2. In this case the sum converges faster and everything works. It is strange that Mathematica did not show the message about the sum convergence. Nov 4, 2012 at 15:55
• The problem was in the invalid testing series, that did not converge. Another sample series NSum[Exp[-Pi n] BDMAF[n, 1, 1, 1], {n, 0, Infinity}] which is closer to the original expression that I actually need converges perfectly, and works well with NIntegrate inside NSum. Case closed. Thank you for your attention. Nov 7, 2012 at 13:42

Problem was in the non-convergent sum. This example with the new sum works good:

BDMAF[n_?NumericQ, \[Gamma]_, x_, c_] := BDMAF[n, \[Gamma], x, c] =
Module[{K1 = EllipticK[1/Sqrt[1 + \[Gamma]^2]],
E1 = EllipticE[1/Sqrt[1 + \[Gamma]^2]], y0},
y0 = ((\[Pi]^2 (IntegerPart[x + 1] - x + n))/(2 K1 E1))^(1/2);
Exp[-y0^2] NIntegrate[
Exp[y^2] Sin[(n + IntegerPart[x + 1])/2 \[Pi] + c y]^2, {y, 0, y0}]
]

NSum[Exp[-Pi n] BDMAF[n, 1, 1, 1], {n, 0, Infinity}]