Michael E2 explains how to use parallel processing to speed up multiple FindRoot
calculation with the same function in https://mathematica.stackexchange.com/a/163295/40271.
An example of some code which does this is
f[x_] := x^x + (4 - x)^(4 - x) - 20;
df0[x_?VectorQ] = D[f[x], x];
df[x_?VectorQ] := DiagonalMatrix@SparseArray@df0[x];
params = RandomReal[{2, 3}, 10];
FindRoot[f[x], {x, params}, Jacobian :> df[x]]
I would now like to extend this to work in more than one variable. A syntax to do this is quite simple;
f[x_, y_] := {x^x + y^y - 20, x + y - 4}
inits = RandomReal[{2, 3}, {2, 10}];
FindRoot[f[x,y], {{x, inits[[1]]}, {y, inits[[2]]}}]
However I can't find the right syntax to add in an explicit Jacobian. My example code is
f[x_, y_] := {x^x + y^y - 20, x + y - 4}
df0[x_, y_] = Outer[D, f[x, y], {x,y}];
df[x_, y_] := df0[x, y]
inits = RandomReal[{2, 3}, {2, 10}];
Block[{x, y},
FindRoot[f[x, y], {{x, inits[[1]]}, {y, inits[[2]]}},
Jacobian :> df[x, y]]]
And I get an error that The Jacobian is not a matrix of numbers at {x,y} = inits
. So I try instead
f[x_, y_] := {x^x + y^y - 20, x + y - 4}
df0[x_, y_] = Outer[D, f[x, y], {x,y}];
SetAttributes[df, Listable];
df[x_, y_] := df0[x, y]
inits = RandomReal[{2, 3}, {2, 10}];
Block[{x, y},
FindRoot[f[x, y], {{x, inits[[1]]}, {y, inits[[2]]}},
Jacobian :> df[x, y]]]
Which produces the same error, however in this case if I evaluate df@@inits
, I do indeed get a list of matrices of numbers. So I am confused about this.
I have tried also with explicit ?VectorQ
, like so;
f[x_?VectorQ, y_?VectorQ] := {x^x + y^y - 20, x + y - 4}
df0[x_?VectorQ, y_?VectorQ] =
Outer[D, {x^x + y^y - 20, x + y - 4}, {x, y}];
df[x_?VectorQ, y_?VectorQ] := df0[x, y];
inits = RandomReal[{2, 3}, {2, 10}];
FindRoot[f[x, y], {{x, inits[[1]]}, {y, inits[[2]]}},
Jacobian :> df[x, y]]
And this doesn't work either.
Who knows how to do this?