TransWikia.com

Solving equation for unknown and replacing it in second equation

Mathematica Asked by Zorg on August 9, 2021

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.

2 Answers

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 *)

Correct answer by Bob Hanlon on August 9, 2021

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!

Answered by Alexei Boulbitch on August 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP