If I want to precompile a function that I intend to put into FindRoot
many times, I could do it like this:
f[x_] := x^x + (4 - x)^(4 - x) - 10
g = Compile[{x}, Evaluate[f[x]]];
FindRoot[g, {3.99}]
Then if I want to run FindRoot
on this function multiple times it will be faster. Generally FindRoot
will compile the input function as the function will be called many times, and now if I want to run FindRoot
itself many times (for example with different initial conditions to find different roots of the function) the function is compiled only once.
Now suppose that I have a similar function which depends also on a parameter. I'd like to be able to compile the function only once (and not for every value of a
), and use FindRoot
for different values of the parameter like so:
f[x_, a_] := x^x + (a - x)^(a - x) - 10
FindRoot[f[x, 4], {x, 3.99}]
FindRoot[f[x, 5], {x, 4.99}]
I can't work out I would do this, can anybody help?