MathGroup Archive 1993

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: NIntegrate vs. N[Integrate]

Mangalam Vasudaven <TVASUDAVE at> writes
	> I was always under the impression that it is a lot more
	> efficient to use NIntegrate when one is trying to get
	> approximate values of definite integrals than surround
	> it by N[].  But while playing around with some gamma integrals
	> I found that NIntegrate took sixty times as much time as
	> the second method.  BTW, Mathematica Book, in page 683,
	> confirms my original impression.

	> p[x_]:= Integrate[(y^6)*(E^-y)/6!,{y,0,x}]
	> NIntegrate[ p[x]*(1-p[x]),{x,0,200}]
	> Now, the NIntegrate took 336.36 seconds.  And
	> N[Integrate[p[x]*(1-p[x]),{x,0,200},10]
	> took just 5.66 seconds, giving an answer accurate to more
	> decimal places than NIntegrate, incidently.  Why does this
	> happen?  Being a novice to Mathematica, I could not figure out
	> why.

There are two issues in the above example that are important:
   1) NIntegrate[ ] has the Attribute HoldAll.  This means that as the
      the algorithm proceeds and sample values of x are chosen they
      will be substituted into the integrand p[x]*(1-p[x]) and
      Integrate[ ] will be evaluated twice FOR EACH VALUE OF x.
      I am surprised that it only took 336.36 seconds!

      If instead you define p[x_] = Integrate[(y^6)*(E^-y)/6!,{y,0,x}]
      you evaluate the Integrate[ ] once and the timings are quite

	In[5]:= Timing[NIntegrate[ p[x]*(1-p[x]),{x,0,200}]]

	Out[5]= {10.7667 Second, 1.46631}

	In[6]:= Timing[NIntegrate[ p[x]*(1-p[x]),{x,0,Infinity}]]

	General::under: Underflow occurred in computation.

	Out[6]= {8.76667 Second, 1.46631}

   2) Timing the total time to evaluate Integrate[ ] three times and
      evalute the resulting function numerically has little
      meaning other than that the total time was whatever it was.
      It is not clear just what the problem is, but if it is to
      evaluate the integral for various limits of integration it is
      probably best to evaluate it once and for all symbollically,
      and then evaluate the symbolic result with various numerical

	In[8]:= q[x0_, x1_] = Integrate[p[x]*(1-p[x]),{x, x0, x1}];

	In[9]:= Timing[N[q[0, 200], 150]]

	Out[9]= {0.1 Second, 1.4663085937499999999999999999999999999\
	> 9999999999999999999999999999999999999986932189377841056111\
	> 6480220211033432215508557551981108896417994036563728038}

Jerry B. Keiper
keiper at
Worfram Research, Inc.

  • Prev by Date: Integers in Mma
  • Next by Date: beheading data objects
  • Previous by thread: NIntegrate vs. N[Integrate]
  • Next by thread: pattern matching