5
$\begingroup$

In investigating Slow integration for simple functions involving `UnitBox`, I defined

f[x_] = UnitBox[1 - Mod[x, 3]]
intn[x_] := NIntegrate[f[u], {u, -x, x}]

I evaluated

Block[{$MaxPiecewiseCases = ∞}, intn[20]]

and received the following errors/warnings, and the correct answer. (I realise that in this case, modifying $MaxPiecewiseCases is not needed to evaluate the intn.)

enter image description here

I see similar behaviour in both V10.4 and V11.0.0 (Linux Mint 17.3).

Does anyone have any clues as to what is happening?

$\endgroup$
1
  • 2
    $\begingroup$ You may wish to flag this as a bug and report it to Wolfram, Inc. $\endgroup$
    – bbgodfrey
    Sep 4 '16 at 21:19
2
$\begingroup$

This is an extended comment plus a work-around (at least, for the specific functions in the question). According to its documentation, the value of $MaxPiecewiseCases can impact such functions as PiecewiseExpand, FunctionExpand, Reduce, and Integrate. The documentation makes no mention of NIntegrate, although clearly it has an impact on it. With

$Version
(* 11.0.0 for Microsoft Windows (64-bit) (July 28, 2016) *)

Block[{$MaxPiecewiseCases = Infinity}, intn[20]]

produces the same results presented in the question. Unexpectedly,

Block[{$MaxPiecewiseCases = Infinity}, PiecewiseExpand[intn[20]]]

produces the set of error messages twice. (I would have expected only one set of error messages.) Limiting the arguments of f and intn to be numerical has no impact on either case.

On the other hand,

Block[{$MaxPiecewiseCases = Infinity}, PiecewiseExpand[f[x], -40 < x < 40]]
Block[{$MaxPiecewiseCases = Infinity}, Reduce[f[x] == x, x]]
Block[{$MaxPiecewiseCases = Infinity}, FunctionExpand[f[100]^2]]

and

intn[x_] := Integrate[f[u], {u, -x, x}]
Block[{$MaxPiecewiseCases = Infinity}, intn[20]]
(* 27/2 *)

work well. Even

intn[x_] := NIntegrate[f[u], {u, -x, x}, Method -> "MonteCarlo"]
Block[{$MaxPiecewiseCases = Infinity}, intn[20]]
(* 13.5168 *)

and Monte Carlo variants work, although not very accurately. So, it appears that only NIntegrate with certain Methods generates error messages before returning the correct answer.

Evidently, if Integrate can handle the integrand, it should be used.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.