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: RE: NIntegrate - problems with HoldAll property(?) - the

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73217] Re: RE: NIntegrate - problems with HoldAll property(?) - the
  • From: gotcha <david.shorthouse at credit-suisse.com>
  • Date: Thu, 8 Feb 2007 03:31:58 -0500 (EST)

Hi I have the solution to my problems. For reference I give them below

Problem:
========

I want to NIntegrate the equivalent of the functions:

f1[x_] := Module[{res,y},
  res = First[Position[{1, 3, 5, 7, 9, Infinity}, y_ /; y>= x, Infinity, 1]];
  res[[1]]
]
NIntegrate[f1[i], {i, 0,7}]

and 

f2[x_] := Module[{res,y},
  res = First[Position[{1, 3, 5, 7, 9, Infinity}, y_ /; y>= x, Infinity, 1]];
  { res[[1]],{a,b,c,d} }
]
NIntegrate[f2[i][[1]], {i, 0,7}]

Both of the NIntegrates fail because the "i" variable does not get resolved to a number at the time of the calls to f1 and f2

Solution to f1
==============
Quite a few people gave me the answer for f1[]. If I define the function so it only takes a numeric input, the NIntegrate function resolves the "i" variable to a number before the call. I'm not sure why this works but I am glad it does.

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

But this doesn't:(!!)
f2[(x_)?NumericQ] := Module[{res,y},
  res = First[Position[{1, 3, 5, 7, 9, Infinity}, y_ /; y>= x, Infinity, 1]];
  { res[[1]],{a,b,c,d} }
]
NIntegrate[f2[i][[1]], {i, 0,7}]

It gives a numeric result but the wrong one! Because the "i" variable is not resolved to a number this time, my definition of f2 does not even get called, so we are essentially doing:
NIntegrate[i, {i, 0,7}]

Solution to f2
==============
After a bit of fiddling I finally got f2 to work, as follows:
f2[(x_)?NumericQ] := Module[{res,y},
  res = First[Position[{1, 3, 5, 7, 9, Infinity}, y_ /; y>= x, Infinity, 1]];
  { res[[1]],{a,b,c,d} }
]
f3[(x_)?NumericQ] := f2[x][[1]]
NIntegrate[f3[i], {i, 0,7}]

Having a "pure" function in the integral seems to allow the NIntegrate to resolve the "i" variable to a number before the call to f3, so all is well again

I'm still a little confused but I am able to continue. Thanks for all the help

David


  • Prev by Date: Re: Passing a list as seperate parameters?
  • Next by Date: Re: Integrating SphericalHarmonicY
  • Previous by thread: Re: (in)dependent variables in DSolve: need explanation.
  • Next by thread: Unwrap list of angles