0
$\begingroup$

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!

$\endgroup$

2 Answers 2

2
$\begingroup$

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.}}

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – lumw
    Nov 19, 2021 at 23:15
0
$\begingroup$

@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.

$\endgroup$

Your Answer

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

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