Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10490

Re: Bug in NIntegrate[]?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!newspump.sol.net!posts.news.twtelecom.net!nnrp3.twtelecom.net!not-for-mail
From Bob Hanlon <hanlonr357@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Bug in NIntegrate[]?
Date Fri, 15 Jun 2012 19:38:53 +0000 (UTC)
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Sender steve@smc.vnet.net
Approved Steven M. Christensen <steve@smc.vnet.net>, Moderator
Message-ID <jrg30d$sof$1@smc.vnet.net> (permalink)
References <201206150741.DAA24405@smc.vnet.net>
Lines 34
NNTP-Posting-Date 15 Jun 2012 19:43:03 GMT
NNTP-Posting-Host 4261aca3.news.twtelecom.net
X-Trace DXC=T>@iSoSR?decJhQ<W;:kglC_A=>8kQj6mY;@_o827nGc<HiSiKeZEnhEFiONJ7[Gof1@ZHHPcg=Xf
X-Complaints-To abuse@twtelecom.net
X-Received-Bytes 1754
Xref csiph.com comp.soft-sys.math.mathematica:10490

Show key headers only | View raw


NIntegrate first tries to evaluate its argument symbolically and evaluates to 1 since your definition defines f as one for symbolic input.

You could change your default to

f[x_?NumericQ] = 1;

to preclude evaluation for symbolic input; however, it would be better to define f using Piecewise.

f[x_] := Piecewise[{{1, 0 <= x <= 1}}]


Bob Hanlon


On Jun 15, 2012, at 3:41 AM, GS <vokaputs@gmail.com> 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
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Bug in NIntegrate[]? Bob Hanlon <hanlonr357@gmail.com> - 2012-06-15 19:38 +0000

csiph-web