# How to apply NIntegrate two times?

I have following integration. $$I=\int_{0}^{\frac{\pi}{2}} \int_{0}^{\infty} \gamma e^{- \lambda \left(\gamma^2+2d\gamma\cos\theta -d^2 + \int_{-\frac{\pi}{2}}^{\frac{\pi}{2}} 2d\cos\Theta(d\cos\Theta - \sqrt{d^2\cos^2\Theta + 2d\gamma\cos\theta+\gamma^2}) d\Theta \right) } \,d\gamma d\theta$$ Since this may not have a closed-form solution, I tried to evaluate it numerically as below:

PoNum[λ_, d_] :=NIntegrate[
NIntegrate[
x Exp[-λ (x^2 + 2 d x Cos[θ] - d^2 +
NIntegrate[
2 d Cos[Θ] (d Cos[Θ] -
Sqrt[(d Cos[Θ])^2 + 2 d x  Cos[θ] +
x^2]), {Θ, -(π/2), π/2}])], {x,
0, ∞}], {θ, 0, π/2}];


Here $\lambda$ and $d$ are positive constant. E.g.

PoNum[2.3, 1.1]


However, my code may not give correct answer as I have seen series of warnings.

First the inner integral (exponent) is

int[d_?NumericQ, θ_?NumericQ, x_?NumericQ] :=NIntegrate[2 d Cos[Θ] (d Cos[Θ] -Sqrt[(d Cos[Θ])^2 + 2 d x Cos[θ] + x^2]), {Θ, -(π/2), π/2}]


With this function the complete integral you are asking for is

PoNum[λ_?NumericQ, d_?NumericQ] := NIntegrate[x Exp[-λ (x^2 + 2 d x Cos[θ] - d^2 + int[λ, θ, x])], {x, 0, ∞}, {θ,0, π/2}];

PoNum[2.3, 1.1]
(*5.46735*10^16*)


The inner integral int[] can be converted to

2 d^2Integrate[ Cos[Θ] ( Cos[Θ] -Sqrt[( Cos[Θ])^2 + (2 d x Cos[θ] + x^2)/d^2]),{Θ, -(π/2), π/2}]


and solved analytically

Simplify[Integrate[Cos[\[CurlyTheta]] (Cos[\[CurlyTheta]] - Sqrt[Cos[\[CurlyTheta]] + c^2]), {\[CurlyTheta], -Pi/2, Pi/2}],Re[c^2] > 0]
(* 1/2 (\[Pi] - (1/(3 Sqrt[1 + 1/c^2] Sqrt[c^2]))2 (2 (c^2 + c^4) EllipticE[2/(1 + c^2)] -2 (-1 + c^4) EllipticK[2/(1 + c^2)] +6 Sqrt[1 + 1/c^2]c^2 HypergeometricPFQ[{-(1/4), 1/4, 1}, {1/2, 3/2}, 1/c^4])) *)


Unfortunately this transformation doesn't lead to performance increase...

• thanks @Ulrich, it works smoothly.
– Frey
Sep 10, 2018 at 10:37
• @ Frey Thanks. Just one hint: The integral (exponent) could be solved analytically, but the evaluation of the complete integral doesn't finish... Sep 10, 2018 at 10:47
• Can you please share the solution of that exponent integral? Thanks!
– Frey
Sep 10, 2018 at 12:13
• My answer is edited. Sep 10, 2018 at 12:54
• thanks @Ulrich, that simplification helps me.
– Frey
Sep 11, 2018 at 3:33