• To: mathgroup at smc.vnet.net
• Subject: [mg89422] Re: A question about N[...]
• From: Bill Rowe <readnews at sbcglobal.net>
• Date: Mon, 9 Jun 2008 02:28:21 -0400 (EDT)

```On 6/8/08 at 2:32 AM, wyelen at gmail.com wrote:

>My platform is Mathematica 6.0 for Microsoft Windows (32-bit). When
>calculating the following integral, I got different results from
>Integrate & NIntegrate:

>In[1]:= Integrate[BesselJ[0, 2.405 * r]^2, {r, 0, 20}]

>Out[1]= 0.

>In[2]:= NIntegrate[BesselJ[0, 2.405 * r]^2, {r, 0, 20}]

>Out[2]= 0.864755

>Guessing a problem caused by numerical number 2.405, I rewrote it as
>an exact number:

>In[3]:= Integrate[BesselJ[0, (2 + 405/1000)*r]^2, {r, 0,
>20}]

>Out[3]= 20*HypergeometricPFQ[{1/2, 1/2}, {1, 1, 3/2}, -
>(231361/100)]

>then evaluated the numerical value, which was surprisingly still 0.:

>In[4]:= N[%]

>Out[4]= 0.

This really shouldn't be surprising. If you go to the help page
for N, click on the arrow by Scope then click on the arrow by
Machine and Adaptive Precision you will find a discussion that
explains what is going on. Basically, in the first expression
you enter the constant as a machine precision number which
causes Mathematica to do the entire computation with machine
precision. For the other case, you get an exact result which you
then tell Mathematica to convert all of the numbers to machine
precision then do the computation obtaining the same result.

>but evaluating with 6-digit precision gave the same result as
>NIntegrate:

>In[5]:= N[%%,6]

>Out[5]= 0.864755

Right. Now you've asked for arbitrary precision. So, Mathematica
uses its adaptive precision techniques rather than simply
substituting machine precision numbers for everything then doing
the computation.

>In help page for N it said "N[expr] is equivalent to
>N[expr,MachinePrecision]", but evaluating with a approximate
>precision didn't gave 0.:

Usually, this is true. But it is a simplification of what is
really going on and as a result isn't totally accurate for all cases.

>I wonder is this caused by the function N ,or whether I should just
>turn to another OS (say Linux) and things will go well.

A different OS will not resolve this issue.

When doing integration it is always better to use
NIntegrate[...] rather than N[Integrate[...]] when you want a
machine precision result.

```

• Prev by Date: Re: A question about N[...]
• Next by Date: Re: Dynamic GUI problems