2

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.

Wouter
  • 396
  • 1
  • 11
  • Note that you should use SetDelayed and not Set, i.e f[x_]:=NIntegrate.... See here: http://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/18487#18487 – yohbs Jan 27 '17 at 14:44
  • @yohbs you are absolutely right, was a typo – Wouter Jan 27 '17 at 14:52

1 Answers1

3

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[{f`error, f`int},
   f`int = NIntegrate[BesselJ[2, x t], {t, 0, 40000},
     IntegrationMonitor -> ((f`error = Total[Map[#1@"Error" &, #1]]) &)];
   (*Sow[{x,f`error},"error"];*)    (* absolute error *)
   Sow[{x, Abs[f`error/(10^-6*f`int)]}, "scalederror"];   (* for  PrecisionGoal -> 6  *)
   f`int];

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

Mathematica graphics

plot

Mathematica graphics

Michael E2
  • 235,386
  • 17
  • 334
  • 747