4
$\begingroup$

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
$\endgroup$
3
  • 4
    $\begingroup$ I gues, the amount of numbercrunching within cDf is not enough to ammortized the calling overhead. This problem is just too small. $\endgroup$ Jun 18, 2018 at 12:34
  • 1
    $\begingroup$ You get some improvement by updating the Jacobian at each step, instead of using the constant Jac. at the starting point {x0, y0} (i.e., use Jacobian :> cDf[x, y]). $\endgroup$
    – Michael E2
    Jun 18, 2018 at 17:32
  • $\begingroup$ @ Michael E2 it actually makes a difference and you are right i dont even know why i was evaluating Jacobian at initial points. now i have another question. if function and Jacobian being updated at each loop then what is the best way to tackle this problem? you can also post the answer and i will accept it . $\endgroup$
    – user49047
    Jun 18, 2018 at 18:09

1 Answer 1

2
$\begingroup$

Turning a comment into an answer (and correcting some typos).

I guess, the amount of number crunching within cDf is not enough to ammortize the calling overhead. This problem is just too small.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.