Solving equation for unknown and replacing it in second equation

I am new to Mathematica and struggling with its syntax. I have a set of equations, which I would like to solve for my unknowns. I would like to solve the first equation for the first unknown and substitute this unknown in a second equation with the result I have obtained from Solve. Sounds easy, but for me this seems to be undoable.

Here my MWE:

g1 = a + b + 2*c + 5 == 0
g2 = a + e + c - 5 == 0
sol = Solve[g1, c]
g2 = g2 /. c -> sol


The result is:

{{-5 + a + e + (c -> 1/2 (-5 - a - b))}} == 0


which is clearly not what I wanted. In the second step I want to solve g2 for a, assuming b and e are known.

• Try g2/.sol and don't overwrite g2=g2... Commented Dec 4, 2020 at 9:39

Clear["Global*"]

g1 = a + b + 2*c + 5 == 0;
g2 = a + e + c - 5 == 0;


With two equations you can solve for two unknowns.

sol = Solve[{g1, g2}, {a, c}]

(* {{a -> 15 + b - 2 e, c -> -10 - b + e}} *)


Or, if you only want to solve for a while eliminating c

sola = Solve[{g1, g2}, a, {c}]

(* {{a -> 15 + b - 2 e}} *)


For your larger system of equations, try structuring it as a minimization problem.

min = Minimize[Total[(Subtract @@@ {g1, g2})^2], {a, c}] // Simplify

(* {0, {a -> 15 + b - 2 e, c -> -10 - b + e}} *)


If an exact solution exists, the minimum is zero and the approaches are equivalent.

sol[[1]] === min[[2]]

(* True *)

• Thank you I will definitely try it. Is the chance for solving my larger problem with minimize bigger?
– Zorg
Commented Dec 6, 2020 at 12:28
• I have no way of knowing or even guessing. Since the approach that you are using doesn't seem to be working, it is at least an alternative. Commented Dec 6, 2020 at 15:55
• okay, I will try it. The three @ are strange to me, so I did some research. According to Wolfram this means "Verify that their differences are integers:", but since I am seeking the symbolic solutions is thisusefull or necessary?
– Zorg
Commented Dec 7, 2020 at 10:55
• Okay I have to admit that the longer I look at our minimize code the less I understand it. You subtract g2 from g1 and square the result, okay I understand this. But why 'Total', where does the sum come from?
– Zorg
Commented Dec 7, 2020 at 11:50
• In general the equations could be of the form lhs == rhs, i.e., Equal[lhs, rhs] where rhs is not necessarily zero. Highlight @@@ and press F1 for help. You will go to a link for Apply. The documentation there states "f@@@expr or Apply[f, expr, {1}] replaces heads at level 1 of expr by f." So Subtract @@@ eqns results in {lhs1-rhs1, lhs2-rhs2, ...}. To satisfy the equations, all of these numbers must be zero. A standard way of doing this is to minimize the sum of the squares (positive numbers), i.e., minimize Total[(Subtract @@@ eqns)^2] Commented Dec 7, 2020 at 16:10

You may use your approach after applying the correction proposed by @Ulrich Neumann. There is, however, a function specifically for such an operation:

g1 = a + b + 2*c + 5 == 0
g2 = a + e + c - 5 == 0

Eliminate[{g1, g2}, c]

(*    15 + b - 2 e == a  *)


The both approaches yield the same final equation.

Have fun!

• Thank you, my real problem is a little bit more complictaed than my MWE. I have 9 non-linear equations which I would like to solve for 9 unknowns. Numerically this is no problem but I need the symbolic solutions, if they exist. Simply Solve[{equations}{unkowns}] does not work, it runs for days without a result.
– Zorg
Commented Dec 4, 2020 at 9:57
• My question is, is it worth trying to use Eliminate?
– Zorg
Commented Dec 4, 2020 at 10:05
• My experience tells me that exactly in the situation you described in some cases I could not get a solution with Solve, but succeeded first using Eliminate and after that Solve. In other cases it did not help. Of course, 9 nonlinear equations is a lot, and I would not be very optimistic about finding the exact solution. However, everything depends on the form of equations. I wish you to be lucky. Commented Dec 4, 2020 at 11:17
• Nine nonlinear equations... If they are polynomials, Solve` is most likely to be hanging either in computing a Groebner basis, or in unravelling the roots. The complexity with depend on degrees and on presence/absence of symbolic parameters. If they are polynomials and the input is not mongo-large, I will suggest posting as a new MSE question. Commented Dec 4, 2020 at 14:48