Getting errors from NIntegrate when I try to plot my function

I am trying to 3D plot the function ϕ[σ, λ], but Mathematica keeps refusing to plot for the reason that the output involves complex numbers, even though by definition the function should not give any complex output. Here is the code I am using. It's a bit messy so please let me know if there's questions.

f[x_] = Exp[-(x - 5)^2]

G1[b_, σ_, λ_] = 0
G2[b_, σ_, λ_] = (1/π) Sqrt[b/σ] EllipticK[Abs[(λ^2 - 4 (σ - b)^2)/(16 σ*b)]];
G3[b_, σ_, λ_] = (4/π)*((b)/(Sqrt[λ^2 - 4 (σ - b)^2])) EllipticK[Abs[(16 σ*b)/(λ^2 - 4 (σ - b)^2)]];
G[b_, σ_, λ_] = Piecewise[{{G1[b, σ, λ], λ <= 2 Abs[σ - b]}, {G2[b, σ, λ], 2 Abs[σ - b] < λ < 2 Abs[σ + b]}, {G3[b, λ, σ], λ >= Abs[σ + b]}}];
ϕ[σ_, λ_] = 2*NIntegrate[f[b] G[b, σ, λ], {b, 0, ∞}]
Plot3D[ϕ[σ, λ], {σ, 0, 3}, {λ, 0, 12}, PlotRange -> {-1, 3}]


Basically, I just want to get it to plot the function successfully, so please let me know if you see anything out of place, or a small detail that might be useful to mess around with.

• At least, ϕ should probably be defined using SetDelayed (:=) rather than Set (=). But after that, there seem to be numerical issues with the integration. Have you worked those out yet? – march Jul 6 '16 at 22:37
• @march Any ideas on what might be causing the numerical issues. It is only plotting about half the graph (see edited/added image to the original post). It seems to find complex values, but all the square roots are definitely positive. Thanks for the continued help. – Dsmith Jul 6 '16 at 23:16
• I think your function is complex valued, check σ = 0.1 and λ = 11.1: N[G[23/5, 1/100, 111/10], 30] (* -0.459389664108971824295451307681 - 0.340789496534245725392527434380 I*) – Chip Hurst Jul 7 '16 at 0:04

Here is code that makes the plot of 1st ReandIm of the function without messages.

Clear[f, "G*", ϕ]

f[x_?NumberQ] := Exp[-(x - 5)^2]
G1[b_?NumberQ, σ_, λ_] := 0
G2[b_?NumberQ, σ_, λ_] := (1/π) Sqrt[
b/σ] EllipticK[
Abs[(λ^2 - 4 (σ - b)^2)/(16 σ*b)]];
G3[b_?NumberQ, σ_, λ_] := (4/π)*((b)/(Sqrt[\
λ^2 - 4 (σ - b)^2])) EllipticK[
Abs[(16 σ*b)/(λ^2 - 4 (σ - b)^2)]];
G[b_?NumberQ, σ_, λ_] :=
Piecewise[{{G1[b, σ, λ], λ <=
2 Abs[σ - b]}, {G2[b, σ, λ],
2 Abs[σ - b] < λ < 2 Abs[σ + b]}, {G3[
b, λ, σ], λ >= Abs[σ + b]}}];
ϕ[σ_, λ_] :=
2*NIntegrate[f[b] G[b, σ, λ], {b, 0, ∞},
AccuracyGoal -> 3, PrecisionGoal -> 3,
Method -> {"GlobalAdaptive", "SymbolicProcessing" -> 0}];

Grid[{{Plot3D[
Re[ϕ[σ, λ]], {σ, 0, 3}, {λ, 0,
12}, PlotRange -> All, ImageSize -> Medium],
Plot3D[Im[ϕ[σ, λ]], {σ, 0,
3}, {λ, 0, 12}, PlotRange -> All, ImageSize -> Medium]}}] • @Shutao Tang You definitely solved our issue, thank you. But I guess the real problem is that our function should not have any complex outputs. Any ideas as to what is going wrong and causing these complex outputs? – Dsmith Jul 7 '16 at 22:15
• @Dsmith Shutao just made a typographical edit of my answer. You are getting complex values because of Sqrt[λ^2 - 4 (σ - b)^2] inG3` -- in your integration λ is bounded in [0,12], and b goes to infinity. – Anton Antonov Jul 8 '16 at 0:39