1

I am trying to solve some basic physics problems using Maxima, but I am running into problems.

I want the code below to yield 600 (as the solution to T2), but it just gives an empty list ([]).

solve([
    (P1*V1)/T1 = (P2*V2)/T2,
    V1 = V2,
    P1 = 100000,
    T1 = 300,
    P2 = 200000
    ], [T2]);

What am I missing? (I also tried many other problems similar to this one, and they all seem to fail.)

I know that I could solve this particular one numerically, but I want the answers to be precise, and I also want to be able to solve problems like this:

solve([
    (P1*V1)/T1 = (P2*V2)/T2,
    V1 = V2
    ], [T2]);

(Where the solution should be (T1*P2)/P1.)

kuruczgyurci
  • 130
  • 8

3 Answers3

2

There are a few different ways to go about it. Let's start with:

(%i1) eqn : (P1*V1)/T1 = (P2*V2)/T2 $
(%i2) myvalues : [V1 = V2,P1 = 100000,T1 = 300,P2 = 200000] $

(1) Substitute values into the equation and then solve the equation.

(%i3) subst (myvalues, eqn);
                              1000 V2   200000 V2
(%o3)                         ------- = ---------
                                 3         T2
(%i4) solve (%, T2);
(%o4)                             [T2 = 600]

(2) Solve the equation in general and then substitute values into the solution.

(%i5) solve (eqn, T2);
                                      P2 T1 V2
(%o5)                           [T2 = --------]
                                       P1 V1
(%i6) subst (myvalues, %);
(%o6)                             [T2 = 600]

(3) Solve the equation with values temporarily assigned to variables.

(%i7) ev (solve (eqn, T2), myvalues);
(%o7)                             [T2 = 600]

or, equivalently (this formulation is seen pretty often):

(%i8) solve (eqn, T2), V1 = V2,P1 = 100000,T1 = 300,P2 = 200000;
(%o8)                             [T2 = 600]

(2) is perhaps the most general way to go about it. (1) and (3) are more or less equivalent in the sense that the variables already have values assigned by the time solve sees the equation. That can often make the equation easier for solve to solve it.

In addition to solve, take a look at to_poly_solve to solve equations.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • Thanks, I will take a look at the functions you suggested in more detail. It seems like `solve` just was not designed to take values as equations. – kuruczgyurci Jun 01 '16 at 13:49
1

Adding to Robert Dodier's answer, and ugly as it is, including enough "dummy" variable in solve() also works:

(%i2) solve([(P1*V1)/T1 = (P2*V2)/T2, V1 = V2, P1 = 100000, T1 = 300, P2 = 200000], [T2, P1, T1, P2, V1]);
(%o2) [[T2 = 600, P1 = 100000, T1 = 300, P2 = 200000, V1 = V2]]

and

(%i4) solve([(P1*V1)/T1 = (P2*V2)/T2, V1 = V2], [T2, V1]);
                                   P2 T1
(%o4)                       [[T2 = -----, V1 = V2]]
                                    P1

Certainly Robert Dodier's are better, I am not sure if there is any downside for this.

  • Thanks. This one is really interesting. Is there any reason why `solve` behaves this way? – kuruczgyurci Jun 01 '16 at 13:50
  • @kuruczgyurci, I don't know. But I note that the manual entry mentions 3 forms to `solve`: `solve (, )`, `solve ()`, and `solve ([, ..., ], [, ..., ])`. The 2nd form is simply the 1st when " may be omitted if contains only one variable". That is, in all cases it has as many variables as it has equations. The manual makes no such statement (I think) but the header implies that need. Also any example I can recall also does that - you may want to take a look at the very nice [Maxima by Example](http://web.csulb.edu/~woollett/) page – Rolazaro Azeveires Jun 02 '16 at 22:44
1

Played a little in one line:

subst ([V1 = V2,P1 = 100000`Pa,T1 = 300`K,P2 = 200000`Pa],solve ((P1*V1)/T1 = (P2*V2)/T2,T2));

or

subst (
[
V1 = V2,
P1 = 100000`Pa,
T1 = 300`K,
P2 = 200000`Pa
],
solve (
(P1*V1)/T1 = (P2*V2)/T2
,
T2
));