2
$\begingroup$

Goal is to use NSolve table of solutions for a set of linear equations as initial input values for FindRoot. Here is example of 3 Eqns:

Nsol = Table[NSolve[{
0 == x1 + 2*y1 + 3*z1,
0 == 2*x1 - 3*y1 + z1,
a == 1 + x1 + y1/z1 }, {x1, y1, z1}], {a, 1, 3, 1}]

This provides unique solutions for each value of a = 1,2,3. I'd like to now use these solutions as input to a FindRoot of 3 Eqns (one of which is nonlinear) for same values of a:

FRsol = Table[FindRoot[{
0 == x2 + 2*y2 + 3*z2,
0 == 2*x2 - 3*y2 + z2,
a == E^x2 + y2/z2}, {{x2,?},{y2,?},{z2,?}}], {a, 1, 3, 1}]

I've tried replacing "?" with several delimiters involving Nsol, but without success. While this example FRsol can be solved by merely plugging in some example initial values for x2,y2,z2, the real system I'm looking at has solutions that differ by >15 orders of magnitude and a much larger table of solutions which I will ultimately export. The approach is to use a linear set of equations to generate input values reasonably close to solutions involving system with nonlinear equations.

$\endgroup$
  • $\begingroup$ @JackLaVigne It is on the lhs of the third eqn. $\endgroup$ – Daniel Lichtblau Apr 18 '18 at 16:57
3
$\begingroup$

Could do it like so.

Setup:

vars = {x1, y1, z1};
solns1 = Table[{a, 
   First[NSolve[{0 == x1 + 2*y1 + 3*z1, 0 == 2*x1 - 3*y1 + z1, 
      a == 1 + x1 + y1/z1}, vars]]}, {a, 1, 3, 1}]

(* Out[306]= {{1, {x1 -> 0.714285714286, y1 -> 0.324675324675, 
   z1 -> -0.454545454545}}, {2, {x1 -> 1.71428571429, 
   y1 -> 0.779220779221, 
   z1 -> -1.09090909091}}, {3, {x1 -> 2.71428571429, 
   y1 -> 1.23376623377, z1 -> -1.72727272727}}} *)

Now plug in as initial values for the harder system.

solns2 = 
 Map[FindRoot[{0 == x1 + 2*y1 + 3*z1, 
     0 == 2*x1 - 3*y1 + z1, #[[1]] == E^x1 + y1/z1}, 
    Transpose[{vars, vars /. #[[2]]}]] &, solns1]

(* Out[304]= {{x1 -> 0.538996500733, y1 -> 0.244998409424, 
  z1 -> -0.342997773194}, {x1 -> 0.998528830111, y1 -> 0.45387674096, 
  z1 -> -0.635427437343}, {x1 -> 1.31218638897, y1 -> 0.596448358621, 
  z1 -> -0.835027702069}} *)

An alternative is to reinitialize each iteration in the second run with the results from the prior run.

$\endgroup$
2
$\begingroup$
Nsol = Table[{a, x1, y1, z1} /. 
   NSolve[{0 == x1 + 2*y1 + 3*z1, 0 == 2*x1 - 3*y1 + z1, 
      a == 1 + x1 + y1/z1}, {x1, y1, z1}][[1]], {a, 1, 3, 1}]

(* {{1, 0.714286, 0.324675, -0.454545}, {2, 1.71429, 
  0.779221, -1.09091}, {3, 2.71429, 1.23377, -1.72727}} *)

FindRoot[{0 == x2 + 2*y2 + 3*z2, 
    0 == 2*x2 - 3*y2 + z2, #[[1]] == 
     E^x2 + y2/
       z2}, {{x2, #[[2]]}, {y2, #[[3]]}, {z2, #[[4]]}}] & /@ Nsol

{* {{x2 -> 0.538997, y2 -> 0.244998, z2 -> -0.342998}, {x2 -> 0.998529, 
  y2 -> 0.453877, z2 -> -0.635427}, {x2 -> 1.31219, y2 -> 0.596448, 
  z2 -> -0.835028}} *)

EDIT: However, note that the exact solutions can be obtained with Solve

eqns = {0 == x2 + 2*y2 + 3*z2, 0 == 2*x2 - 3*y2 + z2, a == E^x2 + y2/z2};

Table[{sol[a] = Solve[eqns, {x2, y2, z2}][[1]], And @@ (eqns /. sol[a])}, 
    {a, 1, 3}] // Quiet // Column

enter image description here

% // N

enter image description here

$\endgroup$
  • $\begingroup$ Bob, your FindRoot solution worked perfect for my system. Note, the real system I'm applying this to has a nonlinear equation that's more complex than simply E^x, and solve does not work. It turns out there's one more level of complexity. The solutions of x2,y2,z2 now need to be used as constants to solve another set of equations for the same values of "a". I'm working on this currently. May seek help again with new post if unsuccessful. $\endgroup$ – itedin Apr 19 '18 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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