# NIntegrate inside NSum

Consider the following function with a numerical integration:

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}]
]


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, \[Infinity]}]


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, \[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}]
]


The problem remains, but I receive another error message:

NSum[BDMAF[n, 1, 1, 1], {n, 0, \[Infinity]}]


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. –  halirutan Nov 4 '12 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... –  Guess who it is. Nov 4 '12 at 14:20
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2)Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign –  chris Nov 4 '12 at 14:29
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. –  chris Nov 4 '12 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. –  Stanislav Kruchinin Nov 4 '12 at 15:55

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}]
`