Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: FindRoot + Compile = incompatible (?)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111314] Re: FindRoot + Compile = incompatible (?)
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Tue, 27 Jul 2010 04:17:47 -0400 (EDT)

func = Compile[{{x, _Real}}, x^2];

FindRoot[func[y] == 1,
  {y, 0.5, 1.5}] // Quiet

{y -> 0.9999999999999993}

FindRoot[func[y] == 1,
    {y, #}] & /@
  {-.5, .5} // Quiet

{{y -> -1.000000000000001}, 
   {y -> 1.000000000000001}}

Solve[func[y] == 1, y] //
 Quiet

{{y -> -1}, {y -> 1}}

Reduce[func[y] == 1, y] //
 Quiet

y == -1 || y == 1


Bob Hanlon

---- "Scot T. Martin" <smartin at seas.harvard.edu> wrote: 

=============
Has anyone ever run into the problem of trying to use Compile'd functions 
within FindRoot?

The error message is:

"CompiledFunction::cfsa: Argument y at position 1 should be a machine-size 
real number."

[Yes, the function still evaluates but in doing so the purpose of compile 
& speed gets defeated.]

The explanation for this error messages is found in ref/FindRoot:

"FindRoot first localizes the values of all variables, then evaluates f 
with the variables being symbolic [1], and then repeatedly evaluates the 
result numerically." [1] That's why Compile[] fails.

Here is a specific example of code that shows the problem:

*****

func = Compile[{x}, x^2];

FindRoot[func[y] == 1, {y, 0.5, 1.5}]

*****

Anyone have thoughts on how to work around this aspect so that Compile'd 
functions can work within FindRoot and thus accelerate evaluation thereof? 
[Or is there an inherent reason why they shouldn't?]

[My actual function is not "func"; my actual func is 3x faster as a 
Compile'd, hence my motivation to get FindRoot to work.]

Thanks!



  • Prev by Date: Re: Scoping constructs Block, Module, ModuleBlock
  • Next by Date: Re: FindRoot + Compile = incompatible (?)
  • Previous by thread: Re: FindRoot + Compile = incompatible (?)
  • Next by thread: Re: FindRoot + Compile = incompatible (?)