Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Compiled function with NIntegrate

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52581] Re: Compiled function with NIntegrate
  • From: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>
  • Date: Fri, 3 Dec 2004 03:53:35 -0500 (EST)
  • Organization: Uni Leipzig
  • References: <comgvp$9hg$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

you must restict the arguments of your function by a pattern.
That mean that you have to use a normal (uncompiled) function
that call the compiled function when the arguemtn is numeric

fc=Compile[{{x,_Real}},Sin[x]]
func[x_?NumericQ]:=fc[x]

and

NIntegrate[func[x], {x, 0, Pi}]

will work as expected.

Regards

  Jens




"Alexei Akolzin" <akolzine at uiuc.edu> schrieb im Newsbeitrag 
news:comgvp$9hg$1 at smc.vnet.net...
> Hello,
>
> I wonder why NIntegrate gives the following error with compiled functions:
>
> In:  f = Sin[x];
> In:  fc = Compile[{x},f];
>
> In:  fc[1]
> Out: 0.841471
>
> In:  NIntegrate[fc[x], {x, 0, 1}]
> Out: CompiledFunction::"cfsa" : Argument x at position 1 should be a
> machine-size real number.
> or
> Out: CompiledFunction::cfsa: Argument NIntegrate`Private`XX7 at position 1
> should be a machine-size real number.
>
> It is true that NIntegrate compiles functions by default. This was one of
> the suggestions found in the archive if not to deal, but to get around the
> problem. However, my particular function f takes around 10 minutes to
> evaluate for a single argument value. I never had patience to wait long
> enough (hours) for a result from NIntegrate to appear, with "Compiled"
> option set or not.
>
> To speed things up I used optimization package "optimize.m":
>
> In:  fc = Compile[{x},Optimize[f]].
>
> This helped a lot, with fc[1] ("1", for example) being evaluated in a 
> matter
> of mere seconds. But the problem now is that NIntegrate does not want to
> work with my function.
>
> So, the question is: is there any way to get my compiled function, as it 
> is,
> into NIntegrate?
>
> Thanks,
> Alexei.
> 



  • Prev by Date: Re: Re: Finding the Fourier transform of discrete functions
  • Next by Date: binomial fraction subharmic functions-3rd, and 4th
  • Previous by thread: Re: Compiled function with NIntegrate
  • Next by thread: How can I make FullSimplify work for user-defined functions?