Message Boards Message Boards

NIntegrate with parameters in Block: Avoid error messages?

GROUPS:

Hi!

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
POSTED BY: Vladimir Ivanov
Answer
12 days ago

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):

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

(* => 0.7965995992970549`  *)
POSTED BY: Alexey Popkov
Answer
12 days ago

This is unrelated to Block. Everything is explained here: http://support.wolfram.com/kb/12502

POSTED BY: Szabolcs Horvát
Answer
12 days ago

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.

POSTED BY: Vladimir Ivanov
Answer
12 days ago

Group Abstract Group Abstract