# Nested Integrals Using NIntegrate Not Converging

I have a mathematica script/code that I wrote that involves a few nested NIntegrate functions but its not converging. I let it run for about a week before calling it quits. I am a beginner with Mathematica and composed this script looking through the manual, so I am hoping there is a better way of solving this and that this problem is not just numerically impossible.

Can any help?

e = 1.60217733*10^-19;
re = 2.81794092*10^-15;
gamma = 1000/0.510999060000000;
n = (200*10^-12)/e;
sigmaZ = 10*10^-6;
lambda = n/sigmaZ;
Xt = 100*10^-6;
lq = 0.1;
k = 2;
phiM = lq*k*Xt;
rhoM = lq/phiM;
drift = 10;

EFieldKernel[Xi_?NumericQ, d_?NumericQ, s_?NumericQ, sprime_?NumericQ] :=
(Xi/Xt) (2 n*lambda)/(Xt - Xi -
d*((d - s + sprime - Sqrt[d^2 + (Xi - Xt)^2])/(-rhoM + (
d (rhoM + Xi - Xt))/Sqrt[d^2 + (Xi - Xt)^2])) -
1/2 rhoM*((
d - s + sprime - Sqrt[d^2 + (Xi - Xt)^2])/(-rhoM + (
d (rhoM + Xi - Xt))/Sqrt[d^2 + (Xi - Xt)^2]))^2);

EFieldLongitudinal[Xi_?NumericQ, d_?NumericQ, s_?NumericQ] :=
NIntegrate[EFieldKernel[Xi, d, s, sprime], {sprime, 0, sigmaZ}]

EFieldNegativeSum[d_?NumericQ, s_?NumericQ] :=
NIntegrate[-EFieldLongitudinal[Xi, d, s], {Xi,
Xt - rhoM (1 - Cos[phiM]) - d*Sin[phiM], Xt}]
EfieldPositiveSum[d_?NumericQ, s_?NumericQ] :=
NIntegrate[
EFieldLongitudinal[Xi, d, s], {Xi, -Xt,
Xt - rhoM (1 - Cos[phiM]) - d*Sin[phiM]}]

wake[d_?NumericQ,
s_?NumericQ] := (re/gamma*phiM) (EFieldNegativeSum[d, s]) + (re/
gamma*-phiM) (EfieldPositiveSum[d, s])
Etot[s_?NumericQ] := NIntegrate[wake[d, s], {d, 0, drift}]

Erms = Sqrt[
NIntegrate[lambda*Etot[s]^2, {s, 0, sigmaZ}] -
NIntegrate[lambda*Etot[s], {s, 0, sigmaZ}]^2]

• You have lots of undefined variables, e.g., lambda, n, re, gamma, phiM, etc. You should provide example values for them. Next, I believe the best way to compute a nested NIntegrate function is to rewrite it as a single NIntegrate, and let NIntegrate figure out what method to use. – Carl Woll Jun 21 '17 at 21:30
• You can do this Integrate[(Xi/Xt) (2 n*lambda)/(Xt - Xi - d*((d - s + sprime - Sqrt[d^2 + (Xi - Xt)^2])/(-rhoM + ( d (rhoM + Xi - Xt))/Sqrt[d^2 + (Xi - Xt)^2])) - 1/2 rhoM*(( d - s + sprime - Sqrt[d^2 + (Xi - Xt)^2])/(-rhoM + ( d (rhoM + Xi - Xt))/Sqrt[d^2 + (Xi - Xt)^2]))^2), {sprime, 0, sigmaZ}] operation analytically. Maybe this would help. – Rolf Mertig Jun 21 '17 at 21:35
• You are correct! So sorry I forgot the variables, this should be more helpful. l tried @RolfMertig suggestion, but it still does not converge. Any one else with some thoughts? – Donkey Kong Jun 22 '17 at 7:31
• What do you mean by "does not converge" Does the integration fail with errors? Or does it not finish? – Carl Woll Jun 22 '17 at 14:59
• @CarlWoll It does not finish. No errors. I think I let it run for about a week last time. But, even if it just takes a lot of time I was hoping there was a formalism or technique that could speed it up. – Donkey Kong Jun 22 '17 at 17:02

In general, it is much better to use a single NIntegrate instead of using multiple nested integrals. Here is a contrived example:

f[x_?NumericQ] := NIntegrate[g[x t], {t, 0, 10}];
g[x_?NumericQ] := NIntegrate[Cos[u], {u, 0, x}];

NIntegrate[f[x], {x,0,1}] //AbsoluteTiming


{4.70959, 2.92526}

The equivalent single NIntegrate version is:

NIntegrate[Cos[u], {x, 0, 1}, {t, 0, 10}, {u, 0, x t}] //AbsoluteTiming


{0.441031, 2.92526}

The timing discrepancy can easily be much worse, if the nested NIntegrate has integration convergence issues, which is what I think you're experiencing.

I took a look at your code, replacing all _?NumericQ patterns with just _, and replacing all NIntegrate heads with Inactive[NIntegrate]. Then it was a relatively simple exercise to convert the nested NIntegrate objects into multiple unnested NIntegrate objects. When I did this, the NIntegrate finished quickly, but had convergence issues. I think this solves your speed issue. You could ask about the convergence issues in a different question.