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?


FindRootnow only compiles once for each vector of inputs. 2) the calculations are sent to some special part of the processor which is optimised for numerical mathematical operations via this thing called MKL 3) the calculation is parallelised, which means different inputs from the vector are computed on different cores of the processor. Is that all correct? And did I miss anything? – Jojo Jan 09 '18 at 11:12FindRoot[f[x, #], {x, 2/3#}, Jacobian -> df[x, #]] &[params]I try extending this in the most obvious way I can think of toFindRoot[f[x, #1], {x, #2}, Jacobian -> df[x, #1]] &[params, inits], but this didn't work. Could you explain how to do this? – Jojo Jan 09 '18 at 11:15FindRootdoes not compile the function at all. Modern CPUs are optimized for arithmetic on arrays. Access in Mathematica is through what are called packed arrays. A number of libraries support this under the hood (MKL, BLAS, LAPACK), which you can google if you're interested. -- Your codeFindRoot[f[x, #1], {x, #2}, Jacobian -> df[x, #1]] &[params, inits]worked for me, but I don't know what you used forinits. – Michael E2 Jan 09 '18 at 14:16