0

I have trouble with FindRoot for vector variables, I have search MMA SE find a solution. in Szabolcs‘s code, Vector FindRoot realized by the Evaluated option of FindRoot.

Here is my code,Evaluated -> False not work.

deltat = 1*^-3;
integrator[x_, y_, {x0_, y0_}] := {x[1], y[1]} /. 
   FindRoot[{x[1] - x[0] == {1, 1, 1}, 
      y[1] - y[0] == {1, 1, 1}} /. {x[0] -> x0, y[0] -> y0}, {{x[1], 
      x0}, {y[1], y0}}, Evaluated -> False];
integrator[x, y, {{0, 0, 0}, {0, 0, 0}}]; 

I have test when the variables is scalar, but it fails in Vector variables.

Any comments will be very appreciate!

lumw
  • 593
  • 2
  • 8

2 Answers2

2

The issue is that something like x + {1, 1} automatically evaluates to {1+x, 1+x}, and now making x a vector causes issues. In your case that means that x[1] - x[0] becomes {x[1], x[1], x[1]} and FindRoot doesn't work. One idea is to put all the explicit vectors on one side of the equation and implicit vectors on the other:

integrator[x_, y_, {x0_, y0_}] := {x[1], y[1]} /. FindRoot[
    {x[1] == x0 + {1,1,1}, y[1] == y0 + {1,1,1}},
    {
        {x[1], x0},
        {y[1], y0}
    }
];

integrator[x,y,{{0,0,0},{0,0,0}}]

{{1., 1., 1.}, {1., 1., 1.}}

Another possibility is to add equations for x[0] and y[0]:

integrator[x_, y_, {x0_, y0_}] := {x[1], y[1]} /. FindRoot[
    {x[1] -x[0] == {1,1,1}, y[1] - y[0] == {1,1,1}, x[0] == x0, y[0] == y0},
    {
        {x[0], x0},
        {y[0], y0},
        {x[1], x0},
        {y[1], y0}
    }
];

integrator[x,y,{{0,0,0},{0,0,0}}]

{{1., 1., 1.}, {1., 1., 1.}}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • Thank you, follow your idea, I have test this code and find some issues and maybe fixed it(in this question). any advice will be appreciate. – lumw Nov 19 '21 at 23:15
0

@Carl Woll, thank you!

I think the answer should combine Carl and Szabolcs‘s code. In Carl's code, it seems not work when equation right-hand with x[1] which need to FindRoot.

Here is the example code, which return error.

integrator[x_, y_, {x0_, y0_}] := {x[1], y[1]} /. 
   FindRoot[{x[1] == x0 + x[1]\[Cross]{1, 1, 1}, 
     y[1] == y0 + {1, 1, 1}}, {{x[1], x0}, {y[1], y0}}];

integrator[x, y, {{0, 0, 0}, {0, 0, 0}}]

So ,if we put Evaluated option

integrator[x_, y_, {x0_, y0_}] := {x[1], y[1]} /. 
   FindRoot[{x[1] == x0 + x[1]\[Cross]{1, 1, 1}, 
     y[1] == y0 + {1, 1, 1}}, {{x[1], x0}, {y[1], y0}}, 
    Evaluated -> False];

integrator[x, y, {{0, 0, 0}, {0, 0, 0}}]

I didn't know why we should take Evaluated option, but it works for general form.

Thanks to Carl and Szabolcs again.

lumw
  • 593
  • 2
  • 8