# Plotting function containing NIntegrate: trace the values of convergence problems

Suppose you are plotting a function

Plot[f[x],{x,0,10}]


Where

f[x]=NIntegrate[g[x,t],{t,Tmin,Tmax}].


and when plotting, the warning message

NIntegrate::ncvbNIntegrate failed to converge to prescribed accuracy after N
recursive bisections in t near {t} = c. NIntegrate obtained *number*
and *other number* for the integral and error estimates


is printed (typically once). Unfortunately, it doesn't seem obvious for which value(s) of x the problem appears.

Is there a way to tell on which range of x your plot is reliable?

In case of a DensityPlot it can even be harder to estimate the validity domain.

It would be great if there were for example a possibility for Mesh->All to draw the badly convergent points in a different color.

• Note that you should use SetDelayed and not Set, i.e f[x_]:=NIntegrate.... See here: mathematica.stackexchange.com/questions/18393/… Jan 27 '17 at 14:44
• @yohbs you are absolutely right, was a typo Jan 27 '17 at 14:52

I'm not sure how you want to present the result, but you can cull the errors for each integration with IntegrationMonitor.

Clear[f];
f[x_Real] := Block[{ferror, fint},
fint = NIntegrate[BesselJ[2, x t], {t, 0, 40000},
IntegrationMonitor -> ((ferror = Total[Map[#1@"Error" &, #1]]) &)];
(*Sow[{x,ferror},"error"];*)    (* absolute error *)
Sow[{x, Abs[ferror/(10^-6*fint)]}, "scalederror"];   (* for  PrecisionGoal -> 6  *)
fint];

{plot, {se}} = Reap[Plot[f[t], {t, 0, 1}], "scalederror"];
se = Sort[se];

Graphics[{
Point[MapAt[RealExponent,
DeleteCases[se, {_, Indeterminate}], {All, 2}],
VertexColors -> ColorData["RedGreenSplit"] /@ UnitStep[1 - se[[All, 2]]]]
}, AspectRatio -> 0.6, Axes -> True] plot 