Message Boards Message Boards

0
|
3783 Views
|
8 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Use NIntegrate with Boole and IntervalMemberQ?

Hi,

I am illustrating my problem in this toy example below. I would like to integrate (say the value 1 in this toy example) over a sub-domain {0,1} of the full domain {0,2}. So I try this:

NIntegrate[  Boole[ IntervalMemberQ[ Interval[{0, 1}], x]], {x, 0, 2}]

Mathematica gives me 0 as the answer, while I expect 1. Am I doing something wrong?

I am using Mathematica 11.0.1.0 on Linux x86 (64-bit).

Thanks.

8 Replies
In[9]:= NIntegrate[1, x \[Element] Interval[{0, 1}]]

Out[9]= 1.
POSTED BY: Frank Kampas

Thanks, Frank. Your suggestion works for what I want to do. Although your suggested code is more direct and is fine, I would like to understand why the initial code-fragment I have above doesn't work, in case there is some lesson in there on how not to do things. Any comments on this?

I believe the issue is the order of evaluation. IntevalMemberQ function is evaluated first and gives False since x is not a number until later in the evaluation.

POSTED BY: Frank Kampas

Hi Frank, it seems you hit the point. Look at

iq[x_?NumericQ] := IntervalMemberQ[Interval[{0, 1}], x]

and

In[36]:= NIntegrate[Boole[iq[x]], {x, 0, 2}]

Out[36]= 1.
POSTED BY: Hans Dolhaine

Yes, I agree. But is that the way it's supposed to be? I would have thought it would assign x to a number first and then check later. I wonder if there is a general statement on the syntax in such evaluations on how Mathematica handles it: check before the assignment or check after the assignment.

For instance, the following behaves as I expect (i.e. check after assignment):

Plot[  Boole[ IntervalMemberQ[ Interval[{0, 1}], x]], {x, 0, 2}]

So isn't there an inconsistency here between how NIntegrate[...] handles it and how Plot[...] handles it?

Even worse:
In[1]:= f[x_] := Boole[IntervalMemberQ[Interval[{0, 1}], x]]

In[2]:= Table[f[x], {x, 0, 2, .1}]

Out[2]= {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

In[3]:= NIntegrate[f[x], {x, 0, 2}, "MinRecursion" -> 9]

During evaluation of In[3]:= NIntegrate::izero: Integral and error estimates are 0 on all integration subregions. Try increasing the value of the MinRecursion option. If value of integral may be 0, specify a finite value for the AccuracyGoal option.

Out[3]= 0.

Trace shows that NIntegrate evaluates the function before performing the integration.

POSTED BY: Frank Kampas
In[3]:= IntervalMemberQ[Interval[{0, 1}], x]

Out[3]= False
POSTED BY: Frank Kampas

But

In[13]:= Table[IntervalMemberQ[Interval[{0, 1}], x], {x, 0, 2, .1}]


Out[13]= {True, True, True, True, True, True, True, True, True, True, \
True, False, False, False, False, False, False, False, False, False, \
False}
POSTED BY: Hans Dolhaine
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract