Message Boards Message Boards


NIntegrate with parameters in Block: Avoid error messages?

Posted 1 year ago
3 Replies
1 Total Likes


Could anybody explain me, how to avoid the error message "NIntegrate::inumr" in the sample below?

In[1]:= f[x_, y_] := NIntegrate[E^(-x y z), {z, 0, 1}]

In[2]:= Block[{y = 1}, NIntegrate[f[x, y], {x, 0, y}]]

During evaluation of In[2]:= NIntegrate::inumr: The integrand E^(-x z) has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>

Out[2]= 0.7966
3 Replies

This error message comes from symbolic pre-evaluation performed by the outer NIntegrate and has nothing related to Block. The best is to combine the two NIntegrate into one:

y = 1;
NIntegrate[E^(-x y z), {x, 0, 1}, {z, 0, 1}]

(* => 0.7965995998164899`  *)

If the latter isn't possible, a workaround is to disable symbolic preprocessing by turning the inner integral into a black-box function (but it can considerably slow down integration):

f[x_Real, y_] := NIntegrate[E^(-x y z), {z, 0, 1}]
Block[{y = 1}, NIntegrate[f[x, y], {x, 0, y}]]

(* => 0.7965995992970549`  *)

This is unrelated to Block. Everything is explained here:

Alexey Popkov and Szabolcs Horvát, thanks for answers.

In fact, I do not need a symbolic pre-evaluations of the integral, so this method works well for me.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract