# Automating the solving of system of equations

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.

• Is there a way to automate this process? yes there is. But it will be easier to post the Mathematica code itself you used. Commented Apr 18, 2020 at 13:51
• 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? Commented Apr 18, 2020 at 14:02
• 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. Commented Apr 18, 2020 at 14:04
• {{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]}} Commented Apr 18, 2020 at 15:11
• 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. Commented Apr 18, 2020 at 15:17

## 1 Answer

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]


Also Solve works

 Solve[eqs, vars, Reals]
`
• 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! Commented Apr 18, 2020 at 15:41