0
$\begingroup$

Sometimes I will use an equation, for example, imagine the old chemistry ideal gas law, PV=nRT. Depending on the situation I may want to use different variables as the independent variable. So, for example, in the case of the ideal gas law there are 4 different possible independent variables.

What is a good way to manage this situation and set it up in Mathematica so I can easily compute any particular variable in terms of the others in a literate way?

$\endgroup$
4
  • $\begingroup$ V /. Solve[P V == n R T, V][[1]]? $\endgroup$
    – Feyre
    Commented Jan 19, 2017 at 14:01
  • $\begingroup$ @Feyre I know I can repetitively solve the same equation over and over again. I am hoping there is a more elegant solution. $\endgroup$ Commented Jan 19, 2017 at 14:26
  • $\begingroup$ What kind of thing do you want? $\endgroup$
    – Feyre
    Commented Jan 19, 2017 at 14:33
  • $\begingroup$ It is not quite clear what you're asking. Please elaborate by a specific example. In the meanwhile I'm voting to close this question. $\endgroup$
    – yohbs
    Commented Jan 19, 2017 at 16:18

2 Answers 2

2
$\begingroup$
ClearAll[gasLaw];

gasLaw[var_Symbol, opts : OptionsPattern[]] := 
 Solve[P V == n R T, var, Reals, 
   FilterRules[{opts}, Options[Solve]]][[1]]

gasLaw[P]

(*  {P -> (n R T)/V}  *)

Flatten[gasLaw /@ Variables[Level[P V == n R T, {-1}]]]

(*  {n -> (P V)/(R T), P -> (n R T)/V, R -> (P V)/(n T), T -> (P V)/(n R),
  V -> (n R T)/P}  *)
$\endgroup$
3
  • $\begingroup$ What is opts : OptionsPattern[] and FilterRules[] doing in this? $\endgroup$
    – Sterling
    Commented Sep 21, 2020 at 14:17
  • $\begingroup$ @Sterling - If you evaluate Options[Solve] you will see that Solve can accept several options. This code enables gasLaw to accept these options and pass them to Solve. Not very important for this specific equation, but is useful as a general template. $\endgroup$
    – Bob Hanlon
    Commented Sep 21, 2020 at 14:30
  • $\begingroup$ Ah, great! Thank you. $\endgroup$
    – Sterling
    Commented Sep 21, 2020 at 14:47
0
$\begingroup$

Do you mean something like this ?

var = n;
Solve[P V == n R T, var]
$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.