Re: NIntegrate - bug??
- To: mathgroup at smc.vnet.net
- Subject: [mg85373] Re: NIntegrate - bug??
- From: dh <dh at metrohm.ch>
- Date: Thu, 7 Feb 2008 22:27:49 -0500 (EST)
- References: <foeklg$i9b$1@smc.vnet.net>
Ho Costa,
put a Print statement into your function that prints "x" and you will
see that the function is called with x not evaluated. Why this happens,
I can not explain. But you can prevent this by requesting that x must be
numeric:
expN[x_?NumericQ,p1_]
Nevertheless, Wolfram needs to explain why the nonsense result of 40 is
returned, I think its simply a bug.
hope this helps, Daniel
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 !!!! )
>
>
> Thanks in advance.
>
>
> Costa
>