Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

RE: NIntegrate - problems with HoldAll property(?)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73183] RE: [mg73113] NIntegrate - problems with HoldAll property(?)
  • From: "Shorthouse, David" <david.shorthouse at credit-suisse.com>
  • Date: Tue, 6 Feb 2007 03:55:10 -0500 (EST)

thanks Carl,

please see my reply to the post by Bob Hanlon (when it passes moderation...)

I can see that this does fix the problem I presented for functions that return a single number, although I'm not sure why this forces NIntegrate to resolve the "i" to a number before calling f1[i] (?)

For my real problem, my function f1[] returns a list of useful results, and I am trying to integrate over the first element of the list, akin to NIntegrate[f1[i][[1]], {i, 0,7}]. When I try your solution on this problem, I still get the same problems - in fact if u look at my new post, I actually get a numerical & unexpectedly wrong result, because NIntegrate cannot enter into my function now.

any thoughts?

i.e. this still does not work:
f1[x_?NumericQ, ANOtherInput_] := Module[{res,y},
	res=First[Position[{1, 3, 5, 7, 9, Infinity}, y_ /; y>= x, Infinity, 1]][[1,1]];
	{res,Length[ANOtherInput]}
]
NIntegrate[f1[i][[1]], {i, 0,7}]
gives the wrong answer i.e. <> 19!
It actually gives the integral of f1[x,m]=x i.e. 24.5

any thoughts?

rgds
David


-----Original Message-----
From: Carl Woll [mailto:carlw at wolfram.com] 
Sent: 03 February 2007 14:27
To: gotcha
Cc: mathgroup at smc.vnet.net
Subject: [mg73183] Re: [mg73113] NIntegrate - problems with HoldAll property(?)

gotcha wrote:

>While playing with my new Workbench toy, I think I have found the cause of a problem I am running into with NIntegrate. If so, any way around this one?
>
>  
>
>>f1[x_] := First[Position[{1, 3, 5, 7, 9, Infinity}, y_ /; y>= x, 
>>Infinity, 1]][[1]] NIntegrate[f1[i], {i, 0,7}]
>>    
>>
>
>This fails with the message:
>First::first: {} has a length of zero and no first element
>NIntegrate::inum: Integrand {} is not numerical at {i} = {3.5`}
>
>i.e. I am chucked out on the first Nintegrate call (when i=3.5)
>
>When I track this through the debugger, I notice that "i" is not resolved to "3.5" when it is passed to the f1 function, and so the function cannot execute. i.e. the variable is still set to "i", rather than "3.5" when it hits the function. This is ok for a wide range of functions, but not this one.
>
>I think this is the key to the problems I am having with a more complex problem. If so, can anyone see a way around it? i.e. can I use NIntegrate, and get it to call the functions with numerical values?
>
>Digging deeper, I can see that NIntegrate has the "HoldAll" property (I'm sure there is a good reason for this), but I still can't get round this - I tried putting in Evaluate[] in a couple of places.
>
>Any help out there?
>
>rgds
>David
>  
>
To prevent f1[x] from ever being called when x is not numerical, use the
definition:

f1[x_?NumericQ] := First[Position[{1, 3, 5, 7, 9, Infinity}, y_ /; y>= x, Infinity, 1]][[1]]

instead. Then, your integral will still produce error messages, but will return a valid result:

In[11]:=
f1[x_?NumericQ] := First[Position[{1, 3, 5, 7, 9, Infinity}, y_ /; y ? x, Infinity, 1]][[1]] NIntegrate[f1[i], {i, 0, 7}]

(* error messages suppressed *)

Out[12]=
19.0012

Another possibility for this particular example is to use Interpolation and Integrate instead. For example:

In[13]:=
f2 = Interpolation[{{-100, 0}, {1, 1}, {3, 2}, {5, 3}, {7, 4}, {9, 5}, {100, 6}}, InterpolationOrder -> 0]; Integrate[f2[x], {x, 0, 7}]

Out[14]=
19

Carl Woll
Wolfram Research

==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer: 

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==============================================================================


  • Prev by Date: dot minus operator
  • Next by Date: RE: NIntegrate - problems with HoldAll property(?)
  • Previous by thread: Re: NIntegrate - problems with HoldAll property(?)
  • Next by thread: RE: NIntegrate - problems with HoldAll property(?)