       Re: newbie problem with NIntegrate

• To: mathgroup at smc.vnet.net
• Subject: [mg49841] Re: newbie problem with NIntegrate
• From: "David W. Cantrell" <DWCantrell at sigmaxi.org>
• Date: Tue, 3 Aug 2004 01:11:13 -0400 (EDT)
• References: <cei8o5\$2he\$1@smc.vnet.net> <cejs90\$cpk\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```On Aug. 2, 2004, Garry Helzer wrote:
> On Aug 1, 2004, David W. Cantrell wrote:
> > "Xiaoxun" <dbjunxiao at hotmail.com> wrote:
> >> Hi, all,
> >>
> >>
> >> Geom = (200 >=x>= 100 && 50 >= y >= -50 && -20 <= z <= 20);
> >> gr[x_, y_, z_] := 2 /; Geom;
> >> gr[x_, y_, z_] := 0 /; ! (Geom);
> >> N[Integrate[gr[x, y, z], {x, 0, 300}, {y, -100, 100}, {z, -30, 30}]
> >> This gives a result of 799995.
> >> However, if changing the x range to {x,0,600} results 799993.
> >>                                 and {x,0,900} results 0;
> >>
> >> So what could we do to ensure the right answer?
> >
> > First, let me note that in problems like this, it's often good to
> > write the function using UnitStep. For example, in your problem, it
> > can be written as
> >
> > gr[x_, y_, z_] := 2(UnitStep[x - 100] - UnitStep[x - 200])
> >                    (UnitStep[y + 50] - UnitStep[y - 50])
> >                    (UnitStep[z + 20] - UnitStep[z - 20])
> >
> > However, doing that in your problem does not happen to be necessary.
> > Just get rid of the N[...]. (Did you notice the warning messages you
> > were getting?) In other words, the following works fine (regardless of
> > whether the upper limit of integration for x is 300, as shown, or 600
> > or 900):
> >
> > In:=
> > Geom = (200 >= x >= 100 && 50 >= y >= -50 && -20 <= z <= 20);
> > gr[x_, y_, z_] := 2 /; Geom; gr[x_, y_, z_] := 0 /; ! (Geom);
> > Integrate[gr[x, y, z], {x, 0, 300}, {y, -100, 100}, {z, -30, 30}]
> >
> > Out=
> > 800000

> What version are you using? The last expression is returned unevaluated
> on my system ( version 5.0.1, Mac OS 10.3.4).

The fact that I'm using version 5.0.0 is immaterial. I made a mistake.
Sorry! Doing what it _looks_ like I did, the expression is returned
unevaluated on my system too.

I had actually done the work in the midst of a long session. [The
In and Out numbers were really in the thousands.] I had first defined gr
in terms of UnitStep, as noted above, and had seen that the symbolic
integral worked as desired in several cases. (So far, so good.) I then
decided to see if the OP's definition of gr could be used successfully
instead. In doing so, I uncritically copied the OP's code, and then
deleted the N[]. It seemed to work as desired. What I unfortunately failed
to notice was that the OP's code did not in fact change the definition of
gr! It was still defined in terms of UnitStep. I apologize for my
oversight.