Re: NIntegrate - bug??

*To*: mathgroup at smc.vnet.net*Subject*: [mg85411] Re: NIntegrate - bug??*From*: Szabolcs Horvát <szhorvat at gmail.com>*Date*: Sat, 9 Feb 2008 04:20:32 -0500 (EST)*References*: <foeklg$i9b$1@smc.vnet.net> <fogi7f$26i$1@smc.vnet.net>

dh wrote: > 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. > Hi Daniel, It is not a bug. Even though NIntegrate is HoldAll, it evaluates its arguments before substituting in numerical values. It does this to achieve better performance, and to be able to compile the expression when possible. Making NIntegrate not evaluate its arguments would probably lead to an even greater confusion: think about cases like a = x^2 NIntegrate[a, {x, 0, 1}] (or using % instead of 'a') When the function in question is evaluated with the *symbolic* arguments expN[x, 1/5], the result is 1 + 0.2 x, and NIntegrate[1 + 0.2 x, {x, 10, 20}] is 40. It is easy to see why the function evaluates to 1 + 0.2 x if we keep in mind that While[] stops as soon as the condition evaluates to something else than True (not necessarily False). When in doubt, use Trace[], or On[] and Off[]: On[] expN[x, 1/5] Off[] Szabolcs