Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.2K Views
|
8 Replies
|
1 Total Like
View groups...
Share
Share this post:

Use NIntegrate with Boole and IntervalMemberQ?

8 Replies
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?

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