can someone please tell me why findroot performs poorly with jacobian ? in this example res1 is slower than res.
Clear[eqs, vars, function, jx, f, f1, res, res1]
eqs = {x^x + y^y - 20., x + y - 4.};
vars = {x, y};
function = Cases[Compile[##] &[vars, eqs], x_Function :> x] // First;
ClearAll[J];
J[HoldPattern[Function[vars : {__Symbol}, body_]]] :=
Block[vars, Function @@ {vars, D[body, {vars}]}];
jx = N@J[function];
With[{code = jx[x, y]},
cDf = Compile[{{x, _Real}, {y, _Real}}, code,
RuntimeAttributes -> {Listable}, Parallelization -> True,
RuntimeOptions -> "Speed"]];
f[x0_?NumericQ, y0_?NumericQ] :=
FindRoot[{eqs[[1]] == 0, eqs[[2]] == 0}, {x, x0}, {y, y0},
MaxIterations -> 5000,
Jacobian -> {"FiniteDifference", DifferenceOrder -> 1},
AccuracyGoal -> 10];
f1[x0_?NumericQ, y0_?NumericQ] :=
FindRoot[{eqs[[1]] == 0, eqs[[2]] == 0}, {x, x0}, {y, y0},
MaxIterations -> 5000, Jacobian :> cDf[x0, y0], AccuracyGoal -> 10];
res = f[3., 2.] // RepeatedTiming
res1 = f1[3., 2.] // RepeatedTiming
cDf
is not enough to ammortized the calling overhead. This problem is just too small. $\endgroup${x0, y0}
(i.e., useJacobian :> cDf[x, y]
). $\endgroup$