Re: Bug in NIntegrate[]?

*To*: mathgroup at smc.vnet.net*Subject*: [mg126900] Re: Bug in NIntegrate[]?*From*: Sseziwa Mukasa <mukasa at gmail.com>*Date*: Fri, 15 Jun 2012 15:32:16 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <201206150741.DAA24405@smc.vnet.net>

You ran into the issue of calling f with a non-numeric argument, there are still issues due to the discontinuities but: (Debug) In[14]:= f[x_?NumericQ]:=0/;x<0||x>1 f[x_?NumericQ]:=1 (Debug) In[16]:= NIntegrate[f[x],{x,-1,2}] (Debug) During evaluation of In[16]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small. >> (Debug) During evaluation of In[16]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near {x} {1.00386}. NIntegrate obtained 0.9995253309911655` and 0.0007611273421692226` for the integral and error estimates. >> (Debug) Out[16]= 0.999525 Regards, Ssezi On Jun 15, 2012, at 3:41 AM, GS wrote: > I define the function f[x] as follows: > > f[x_] := 0 /; x < 0 || x > 1; > f[x_] := 1 > > It is zero outside of the interval [0,1]. This can be verified by plotting > Plot[f[x], {x, -1, 2}] > > Now I integrate it from -1 to 2: > In[270]:= NIntegrate[f[x], {x, -1, 2}] > Out[270]= 3. > > The result should be 1, but it is 3. Clearly Mathematica ignores the fact that f[x] is zero outside of [0,1]. > > This caused a lot of headache for me recently when I encountered such behavior in one of my research code. > GS >

**References**:**Bug in NIntegrate[]?***From:*GS <vokaputs@gmail.com>