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: [mg73145] Re: NIntegrate - problems with HoldAll property(?)
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 4 Feb 2007 08:07:29 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <eq1j69$34h$1@smc.vnet.net>

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
> 

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

See the FAQ (Frequently Asked Questions), "How do I write a function 
which evaluates only when the argument is a number?" at
http://support.wolfram.com/mathematica/kernel/features/evalwhennumber.html

Regards,
Jean-Marc


  • Prev by Date: Re: How to find all folder within a specified directory?
  • Next by Date: Re: Text Function with Graphic
  • Previous by thread: Re: NIntegrate - problems with HoldAll property(?)
  • Next by thread: Re: NIntegrate - problems with HoldAll property(?)