3

It occurs for instance here:

b = -1; c = 1; d = -1;
Plot[NIntegrate[1/(a*x^3 + b*x^2 + c*x + d), {x, 0, 10}], {a, 0, 2}]

but not with these values:

  b = 1; c = 1; d = 1;

The phenomenon seems to appear with negative coefficients.

Andreas
  • 3,297
  • 1
  • 5
  • 12
  • 3
    If you use RootSum[] instead, things are much nicer: With[{b = -1, c = 1, d = -1}, Plot[RootSum[d + c #1 + b #1^2 + a #1^3 &, (Log[10 - #1] - Log[-#1])/(c + 2 b #1 + 3 a #1^2) &], {a, 0, 2}, PlotRange -> All]] – J. M.'s missing motivation Dec 25 '20 at 14:38
  • 2
    Your integrand has a pole in the integration interval. This makes the numeric integral error prone. For the second set of parameters, the poles are outside the integration interval. – Daniel Huber Dec 25 '20 at 14:57

1 Answers1

1

A generation ago Plot used to let all (or many) messages escape, so that if you plotted a function like 1/x, you wouldn't be surprised at a divide-by-zero error message. You got used to typing Plot[1/x, {x, -1, 1}] // Quiet. Nowadays, Plot is very aggressive at suppressing message, too aggressive in this case, imo.

Modifying @Szabolcs's code, here's a way to see that Plot is hiding thousands of nonconvergence NIntegrate::ncvb errors from you:

b = -1; c = 1; d = -1;
messages = {};
clearMessages[] := messages = {};
collectMessages[m_] := 
  messages = {messages, m /. Hold[_[mm_, ___], _] :> HoldForm[mm]};
Internal`HandlerBlock[
  {"Message", collectMessages},
  Plot[NIntegrate[1/(a*x^3 + b*x^2 + c*x + d), {x, 0, 10}], {a, 0, 2}]
  ];
Tally[Flatten@messages]

Mathematica graphics

Here's another way I might do it, since I can never remember the handler stuff:

obj[a_?NumericQ] := Module[{res},
   Check[
    res = NIntegrate[1/(a*x^3 + b*x^2 + c*x + d), {x, 0, 10}],
    ++foo; res,
    NIntegrate::ncvb]
   ];
foo = 0;
Plot[obj[a], {a, 0, 2}]
foo
(*  plot omitted *)
(*  2986  *)

I ignore the slow convergence NIntegrate::slwcon warnings, which are hints why the integral does not converge. But I already know why it does not converge.

P.S. There is a method option "SuppressMessages", but it is undocumented and seems not to apply. It takes True or False for values, but it makes no difference here:

Plot[NIntegrate[1/(a*x^3 + b*x^2 + c*x + d), {x, 0, 10}], {a, 0, 2},
 MaxRecursion -> 0, (* for speed *)
 Method -> {"SuppressMessages" -> False}]

Try it with Method -> {"SuppressMessages" -> None} and you will be told to use True or False. So it is an option that is checked.

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