1
$\begingroup$

I checked on the website, and I didn't find a similar topic, so I decided to ask. I solved a couple of maximisation problems, and I obtained solutions for x, y, z and k. What I need to do is to solve them in a system of equations. Now, the peculiarity is that x, y and k have two solutions each, so I have 8 possible systems of equations. What I am supposed to do is to solve all of them, and then select the solutions I need. Is there a way to automate this process? I think writing by hand all the possibilities is cumbersome.

Let's suppose I obtained this output:

{{x -> 3 - y - Sqrt[4 + 7 z + k^2]}, {x -> 3 - y + Sqrt[4 + 7 z + k^2]}}

{{y -> 1/12 (5 + 2 z - Sqrt[12 + 2 k + x^2])}, {y -> 1/12 (5 + 2 z + Sqrt[12 + 2 k + x^2])}}

{{z -> (-2 x + 3 y - 12 k)/(6 k z)}}

{{k -> 1/9 (-2 + 2 x - Sqrt[4 + 2 y^2])}, {k -> 1/9 (-2 + 2 x + Sqrt[4 + 2 y^2])}}

I want to solve the systems:

Solve[x == 3 - y - Sqrt[4 + 7 z + k^2] && y == 1/12 (5 + 2 z - Sqrt[12 + 2 k + x^2]) && z == (-2 x + 3 y - 12 k)/(6 k z) && k == 1/9 (-2 + 2 x - Sqrt[4 + 2 y^2]), {x, y, z, k}]

The only way I know is to manually write one Solve for each system, but it is really cumbersome.

$\endgroup$
  • $\begingroup$ Is there a way to automate this process? yes there is. But it will be easier to post the Mathematica code itself you used. $\endgroup$ – Nasser Apr 18 at 13:51
  • $\begingroup$ Thanks for the answer. Unluckily, the code is related to my thesis, so I am a bit worried about posting it online. Maybe I can write a script resembling my situation? $\endgroup$ – Plastic Man Apr 18 at 14:02
  • 4
    $\begingroup$ Yes, you can make a MWE (small example) that illustrates the problem you want automated. People here like to work with code example which makes it easier to help you and answer the question. $\endgroup$ – Nasser Apr 18 at 14:04
  • $\begingroup$ {{x -> 3 - y - Sqrt[4 + 7 z + k^2]}{x -> 3 - y + Sqrt[4 + 7 z + k^2]}} Is there supposed to be a comma between these 2 lists? i.e. like this {{x -> 3 - y - Sqrt[4 + 7 z + k^2]},{x -> 3 - y + Sqrt[4 + 7 z + k^2]}} $\endgroup$ – Nasser Apr 18 at 15:11
  • $\begingroup$ Yep, sorry, I ma have deleted it by mistake. They are lists, so x, y and k have two possible solutions. I am going to immediately edit the post. $\endgroup$ – Plastic Man Apr 18 at 15:17
3
$\begingroup$

One way

ClearAll["Global`*"]
(*this is the INPUT *)
list = {{{{x -> 3 - y - Sqrt[4 + 7 z + k^2]}, {x -> 
       3 - y + Sqrt[4 + 7 z + k^2]}},
    {{y -> 1/12 (5 + 2 z - Sqrt[12 + 2 k + x^2])}, {y -> 
       1/12 (5 + 2 z + Sqrt[12 + 2 k + x^2])}},
    {{z -> (-2 x + 3 y - 12 k)/(6 k z)}},
    {{k -> 1/9 (-2 + 2 x - Sqrt[4 + 2 y^2])}, {k -> 
       1/9 (-2 + 2 x + Sqrt[4 + 2 y^2])}}}
   };

(*This is the AUTOMATION part *)

list = list[[1, All, 1]];
vars = Union@Cases[list, {Rule[x_, any__]} :> x];
eqs = Cases[list[[All, 1]], Rule[x_, any_] :> (x == any)];
NSolve[eqs, vars, Reals]

Mathematica graphics

Also Solve works

 Solve[eqs, vars, Reals]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks a lot! Your code is wonderful. Of course it is not working with my functions, but I have learnt a lot the same. Also, thanks for the patience and the support! $\endgroup$ – Plastic Man Apr 18 at 15:41

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.