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