I'm trying to get some performance increase out of my own implicit differential equation solver using Compile[]. The uncompiled function is of the following form:
fun = Module[
{sol = ConstantArray[0, 10], init = 0.001},
f[xN_] = xN + xNP1^2 == 4;
Do[
sol[[i]] = xNP1 /. FindRoot[f[init], {xNP1, init}];
init = sol[[i]]
, {i, 10}
];
sol
]
which works correctly. Of course, for the real function I need many more than 10 iteration in the loop and was hoping to gain some performance increase with Compile[]. Here is the compile code:
cFun = Compile[{},
Module[
{sol = ConstantArray[0, 10], init = 0.001},
f[xN_] = xN + xNP1^2 == 4;
Do[
sol[[i]] = xNP1 /. FindRoot[f[init], {xNP1, init}];
init = sol[[i]]
, {i, 10}
];
sol
]
]
However, the compiled function fails with because f[init] is held unevaluated as passed into FindRoot, I believe. Is there a way around this or another solution I am not thinking of?
FindRootreturns a rule as an answer andCompileonly really handles functions that return numbers. Also, the slow part is almost certainlyFindRoot, soCompileis unlikely to help too much. – Pillsy Apr 05 '12 at 19:52