0

I have a pre-compiled function and I need to find its roots. The function seems to be working much faster that before compiling, so I want the compiled version of it.

FindRoot[g1[y], {y, -0.2}]

"Argument y at position 3 should be a \!\(\"machine-size real \
number\"\)"

Is there a way to use FindRoot instead of going for a standard numerical method?

The functions:

g2 = Compile[{{f, _Real}, {\[Theta], _Real}, {y, _Real}, {Hur1, \
_Real}, {Hur2, _Real}, {ke, _Real}, {kmin, _Real}, {kmax, _Real}, {\
\[Gamma], _Real}},


  Hur2 Sum[
 1/k^(\[Gamma] - 
   1) (((1 - 2 f) 1/2 (1 - Erf[Sqrt[2 k] (-y)]) + 
      f)/(((1 - 2 f) 1/2 (1 - Erf[Sqrt[2 k] (-y)]) + 
        f) + ((1 - f) - (1 - 2 f) 1/
         2 (1 + Erf[Sqrt[2 k] (y)])))), {k, Round[kmin], 
  Round[ke Hur1/Hur2] - 1}] + 
   Hur2 Sum[
 1/k^(\[Gamma] - 
   1) (((1 - 2 f) 1/
       2 (1 - Erf[Sqrt[2 k] (\[Theta]/(2 (1 - \[Theta])) - y)]) + 
      f)/(((1 - 2 f) 1/
         2 (1 - Erf[
            Sqrt[2 k] (\[Theta]/(2 (1 - \[Theta])) - y)]) + 
        f) + ((1 - f) - (1 - 2 f) 1/
         2 (1 + Erf[
            Sqrt[2 k] (\[Theta]/(2 (1 - \[Theta])) + y)])))), {k, 
  Round[ke Hur1/Hur2], kmax}] - (y + 1/2)]

g1[y_] := g2[fmin, \[Theta], y, Hur1, Hur2, ke, kmin, kmax, \[Gamma]]
Capivara Cometa
  • 433
  • 2
  • 6
  • Problems with code usually require the code tor an answer. – Michael E2 May 11 '17 at 00:28
  • This is fairly general, but I'll edit and add the function. – Capivara Cometa May 11 '17 at 00:30
  • Well, the short answer is no, it's program and has only finitely many choices for algorithms, each of which could be considered "standard." – Michael E2 May 11 '17 at 00:32
  • Thanks for the answer, but I couldn't understand the argument. Why won't FindRoot work for this kind of functions? It is a fairly simple function. Actually this problem occurs for any compiled function – Capivara Cometa May 11 '17 at 00:34
  • 2
    It works for me. The message is not really an error. Try Clear[g1]; g1[y_?NumericQ] := g2[fmin, \[Theta], y, Hur1, Hur2, ke, kmin, kmax, \[Gamma]]. – Michael E2 May 11 '17 at 00:38

0 Answers0