Re: Bug in NIntegrate[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg126889] Re: Bug in NIntegrate[]?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 15 Jun 2012 15:28:28 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 6/15/12 at 3:41 AM, vokaputs at gmail.com (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 This does appear to be a bug. Instead of using logical operators to define your function why not define it in terms of HeavisideTheta, i.e. g[x_] := HeavisideTheta[x] - HeavisideTheta[x - 1] The advantage of this approach is Mathematica know how to integrate and differentiate HeavisideTheta. That is: In[6]:= Integrate[g[x], {x, -2, 1}] Out[6]= 1 Note, NIntegrate does have problems with this definition. That is In[7]:= NIntegrate[g[x], {x, -2, 1}] Out[7]= 1.00024 with a warning about failure to converge with 9 recursive bisections