Super newbie question re: solving systems of equations

50 views
Skip to first unread message

Natalie Ulrich

unread,
Sep 21, 2017, 2:27:43 PM9/21/17
to sage-support
I'm using SageMathCell to solve chemical equilibrium problems, so at least one set of my solutions has to be real and positive. 

Here's my code: 

var('x, y, z')

xi=0

yi=0.150/2.0

zi=0.150/2.0

K=8.3e-4

eq1=K == y*z/ x

eq2=xi+yi==x+y

eq3=2*xi+2*zi==2*x+2*z

solve([eq1, eq2, eq3],[x, y, z])

 

And here are my solutions:

[[x == -1/200000*sqrt(2496889) + 15083/200000, y == 1/200000*sqrt(2496889) - 83/200000, z == 1/200000*sqrt(2496889) - 83/200000], [x == 1/200000*sqrt(2496889) + 15083/200000, y == -1/200000*sqrt(2496889) - 83/200000, z == -1/200000*sqrt(2496889) - 83/200000]]



Any thoughts? Thanks in advance.


Emmanuel Charpentier

unread,
Sep 22, 2017, 7:40:45 AM9/22/17
to sage-support
what's wrong with :

map(lambda S:map(lambda s:s.lhs()==s.rhs().n(), S), solve([eq1, eq2, eq3], [x, y, z]))

[[x == 0.0675142263037092, y == 0.00748577369629076, z == 0.00748577369629076],
 [x == 0.0833157736962908,  y == -0.00831577369629076,  z == -0.00831577369629076]]

Which shows that the first solution fulfills your constraints ?

HTH,

--
Emmanuel Charpentier

Natalie Ulrich

unread,
Sep 22, 2017, 7:44:50 AM9/22/17
to sage-support
So.. is all of that just converting all the crazy square roots into rational numbers? Thanks for your help!

Emmanuel Charpentier

unread,
Sep 22, 2017, 7:47:39 AM9/22/17
to sage-support
Some may find my first answer a bit Lispish. More Pythonish :

[[s.lhs()==s.rhs().n() for s in S] for S in solve([eq1, eq2, eq3],[x, y, z])]

[[x == 0.0675142263037092, y == 0.00748577369629076, z == 0.00748577369629076],
 [x == 0.0833157736962908, y == -0.00831577369629076, z == -0.00831577369629076]]


HTH,

--
Emmanuel Charpentier

Emmanuel Charpentier

unread,
Sep 22, 2017, 8:03:38 AM9/22/17
to sage-support
Even more Pythonish, and more direct (no conversions, Sage will take care of the (hairy !) exact arithmetics)...) :

[s for s in solve([eq1, eq2, eq3],[x, y, z], solution_dict=True) if all(map(lambda t:bool(t>0), s.values()))]

[{z: 1/200000*sqrt(2496889) - 83/200000,
  y: 1/200000*sqrt(2496889) - 83/200000,
  x: -1/200000*sqrt(2496889) + 15083/200000}]

HTH,

--
Emmanuel Charpentier

Natalie Ulrich

unread,
Sep 22, 2017, 9:42:40 AM9/22/17
to sage-support
That is extremely helpful. Many thanks, and have a beautiful day!

Emmanuel Charpentier

unread,
Sep 22, 2017, 9:51:38 AM9/22/17
to sage-support


Le vendredi 22 septembre 2017 15:42:40 UTC+2, Natalie Ulrich a écrit :
That is extremely helpful. Many thanks, and have a beautiful day!

Thanks !

Two notes :
  1. This kind of question, which is more oriented at learning how to use Sage than at reporting a problem with it or suggesting a new development, is probably more useful when asked on ask.sagemath.org
  2. Most potential answerers, being scholars/teachers/etc.., are wary of "homework" questions, and won't answer them (maybe providing helpful hints). Giving the context of the question and showing what parts of the work you did yourself may help them choosing the right answer...

HTH,

--
Emmanuel Charpentier

Dima Pasechnik

unread,
Sep 22, 2017, 10:38:43 AM9/22/17
to sage-support


On Friday, September 22, 2017 at 12:44:50 PM UTC+1, Natalie Ulrich wrote:
So.. is all of that just converting all the crazy square roots into rational numbers? Thanks for your help!

more precisely, it provided approximations of the crazy roots by floating point numbers.
(most square roots cannot be exactly represented by rational numbers, e.g. sqrt(2) already cannot...)

Natalie Ulrich

unread,
Sep 22, 2017, 1:36:09 PM9/22/17
to sage-support
Got it. I'm a college chemistry professor, just terrible at math solvers :) I've found where I can find answers to those types of questions now. Thanks again!

Natalie Ulrich

unread,
Sep 22, 2017, 1:36:16 PM9/22/17
to sage-support
Ah, I see. Thank you.
Reply all
Reply to author
Forward
0 new messages