1
$\begingroup$

I ran into a strange problem, where using ConstantArray inside FindRoot results in a FindRoot::jsing error. Here's a minimal example, with the first instance working and the second failing:

FindRoot[{0, 0} == {x^2 + y^2 - 1, x - y}, {x, 0.1}, {y, 0.2}]
(* {x -> 0.707107, y -> 0.707107} *)

FindRoot[ConstantArray[0, 2] == {x^2 + y^2 - 1, x - y}, {x, 0.1}, {y, 0.2}]
(* FindRoot::jsing: Encountered a singular Jacobian at the point {x,y} = {0.1,0.2}. Try perturbing the initial point(s). *)
(* {x -> 0.1, y -> 0.2} *)

Any idea why this occurs?

Anyhow, here's an easy workaround:

FindRoot[Table[0, 2] == {x^2 + y^2 - 1, x - y}, {x, 0.1}, {y, 0.2}]
(* {x -> 0.707107, y -> 0.707107} *)
$\endgroup$
6
  • 2
    $\begingroup$ Problem is probably due to FindRoot having the attribute HoldAll. Change to FindRoot[Thread[ConstantArray[0, 2] == {x^2 + y^2 - 1, x - y}], {x, 0.1}, {y, 0.2}] $\endgroup$
    – Bob Hanlon
    Commented Mar 29, 2018 at 14:24
  • 3
    $\begingroup$ Even easier workaround: FindRoot[{x^2 + y^2 - 1, x - y}, {x, 0.1}, {y, 0.2}] $\endgroup$
    – Michael E2
    Commented Mar 29, 2018 at 15:23
  • $\begingroup$ @BobHanlon Thanks, that helps. Do you have any insight into why the Table[0, 2] version works without Thread? $\endgroup$
    – Chris K
    Commented Mar 29, 2018 at 15:23
  • 3
    $\begingroup$ The problem with ConstantArray appears to be that it is packed. Needs["Developer`"]; FindRoot[FromPackedArray@ConstantArray[0, 2] == {x^2 + y^2 - 1, x - y}, {x, 0.1}, {y, 0.2}] works. $\endgroup$
    – Bob Hanlon
    Commented Mar 29, 2018 at 16:59
  • 1
    $\begingroup$ @Bob 's remark is further borne out by the failure of the "reversed" FindRoot[Developer`ToPackedArray@Table[0, 2] == {x^2 + y^2 - 1, x - y}, {x, 0.1}, {y, 0.2}] -- Still seems like a bug to me. $\endgroup$
    – Michael E2
    Commented Mar 30, 2018 at 1:56

0

Your Answer

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

Browse other questions tagged or ask your own question.