Re: NIntegrate - bug??
- To: mathgroup at smc.vnet.net
- Subject: [mg85370] Re: NIntegrate - bug??
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Thu, 7 Feb 2008 07:07:25 -0500 (EST)
- References: <foeklg$i9b$1@smc.vnet.net>
Costa Bravo wrote:
> I have defined the function of Exp with series
>
> expN[x_, p1_] := Module[{min = 10^-20,s, u, j},
> s = 1; u = 1; j = 1;
> While[u *= N[p1*x/j, 20];
> s += u; j++;
> u > min*s];
> s]
>
>
> The differences between Exp & expN
>
> tp = 10; tk = 20;
> Table[{x, Exp[x/5] - expN[x, 1/5]}, {x, tp, tk, 1}]
>
> (* OK 0*10^-18 *)
>
> (* I have integration Exp[x/5] with 'NIntegrate' *)
>
> NIntegrate[Exp[x/5], {x, tp, tk}]
>
> 236.045 (* OK *)
>
> (* and expN[x,1/5] *)
>
> NIntegrate[expN[x, 1/5], {x, tp, tk}]
>
> 40. (* Bad !! *)
>
> (* primitive integration expN *)
>
> t1 = Table[expN[x, 1/5], {x, tp, tk, 1/10}];
> (Total[t1] - (t1[[1]] + t1[[-1]])/2)/10
>
> 236.0533... (* ~OK *)
>
> Why the integration of expN with NIntegrate is bad ?
>
> Tested with Mathematica 5.0 ,5.2 and 6
>
> PS
> If defined function expN1 ( without While !!)
>
> expN1[x_, p1_] := Module[{min = 10^-20, s, u, j},
> s = 1; u = 1;
> Do[u *= N[p1 x/j, 20]; s += u, {j, 30}];
> s]
>
> NIntegrate[expN1[x, 1/5], {x, tp, tk}]
>
> 236.045 ( OK !!!! )
>
Hi,
It is not a bug. Whenever you write function that takes numerical
arguments, such as expN[], make sure that they are evaluated *only* for
numerical data! Change the definition to look like this:
expN[x_?NumericQ, p1_?NumericQ] := ...
If you evaluate the original expN with symbolic parameters, you get:
In[2]:= expN[a, b]
Out[2]= 1 + a b
And NIntegrate[1 + a/5, {a, 10, 20}] happens to be:
In[3]:= NIntegrate[1 + a/5, {a, 10, 20}]
Out[3]= 40.
Szabolcs Horv=E1t