0
$\begingroup$

Here is an example equation:

Solve[{a*x + b*y == c}, {a}]

It gives me:

a -> (c - b*y)/x

To solve the same equation for b, I need to write:

Solve[{a*x + b*y == c}, {b}]

It will give me:

b -> (c - a*x)/y

Similarly, I will have to repeat the steps for all variables like x and y. Is there an easier to tell Mathematica to solve an equation for all variables one by one?

Thanks.

$\endgroup$
5
  • $\begingroup$ Something like: Solve[{ax + by == c}, {#}][[1]] & /@ {a, b, c} & $\endgroup$ Oct 26 '20 at 17:15
  • 2
    $\begingroup$ Flatten@Table[ Solve[a x + b y == c, {z}], {z, {a, b, c, x, y}}] is an easy-to-read method that uses two of the most basic list manipulation functions. $\endgroup$
    – LouisB
    Oct 26 '20 at 18:04
  • $\begingroup$ Thank you very much @LouisB. Is this feature mentioned somewhere in the documentation? $\endgroup$
    – Real Noob
    Oct 26 '20 at 18:42
  • 1
    $\begingroup$ @RealNoob: you might like to peruse these learning references: mathematica.stackexchange.com/questions/18/… $\endgroup$
    – Moo
    Oct 26 '20 at 21:41
  • 1
    $\begingroup$ Thanks @Moo. :) $\endgroup$
    – Real Noob
    Oct 27 '20 at 1:52
1
$\begingroup$

Generalizing the solution provided by LouisB

Clear["Global`*"]

solve[eqn_Equal] := Module[
  {$z, vars = Variables[Level[eqn, {-1}]]},
  Table[Solve[eqn, {$z}][[1]], {$z, vars}]]

eqn = a x + b y == c;

sol = solve[eqn]

(* {{a -> (c - b y)/x}, {b -> (c - a x)/y}, {c -> a x + b y}, {x -> (c - b y)/
   a}, {y -> (c - a x)/b}} *)

Verifying the solutions

And @@ (eqn /. sol)

(* True *)
$\endgroup$
2
  • $\begingroup$ Thanks @Bob. Could you please provide links to some resources where I can learn more about using Mathemtica in such an advanced manner? I am an absolute beginner in Mathemtica and it seems to be a very powerful tool. :) $\endgroup$
    – Real Noob
    Oct 26 '20 at 18:50
  • 2
    $\begingroup$ All the links are built into Mathematica. Highlight any function or operator and click F1 for help. On the documentation paqes look at the examples and look for the links to Tutorials and Related Guides. $\endgroup$
    – Bob Hanlon
    Oct 26 '20 at 19:00

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.