Python SymPy: Error while solving inequality
Asked Answered
M

1

10

Problem: I am trying to solve an inequality to obtain a variable coeff_rw, which is the value of the symbol rw satisfying the inequality. This value should be in terms of other symbols (variables) that are defined in the following code. I am first solving the equations and then the inequality (using the inequality solvers from this tutorial), however, I get a PolynomialError each time I use any solver to obtain coeff_rw as given in the tutorial.

import sympy as sym
#======= define variables as symbols
r, c1, c2, c3, c4, rh, rg, rw, cg, cw, a = sym.symbols('r, c1 c2 c3 c4 rh rg rw cg cw a') # cg = nablaP_g/(4*mu_g); cw = nablaP_w/(4*mu_w); a = mu_g/mu_w
#======= solve system of equations
coeffs = sym.solve((c1*(sym.log(rh)) + c2 + cg*(rh**2), \
c1*(sym.log(rg)) + c2 - c3*(sym.log(rg)) - c4 - (cw - cg)*(rg**2), \
(a*c1) - c3 - 2*(rg**2)*(cw - a*cg), \
c3*(sym.log(rw)) + c4 + cw*(rw**2)), c1, c2, c3, c4)
#======= solve qg and qw
qg = sym.integrate((cg*(r**2) + coeffs[c1]*(sym.log(r)) + coeffs[c2])*(2*sym.pi*r), (r, rh, rg))
qw = sym.integrate((cw*(r**2) + coeffs[c3]*(sym.log(r)) + coeffs[c4])*(2*sym.pi*r), (r, rg, rw))
#======= substitute rg=rh in qw
qwT = qw.subs(rg, rh)
#======= solve the inequality (qw >= qwT) to obtain rw
from sympy.solvers.inequalities import reduce_rational_inequalities
coeff_rw = reduce_rational_inequalities([[qw - qwT >= 0]], rw)]

Question: I would like to obtain the value of rw for the inequality qw >= qwT as defined in the above code.

Mommy answered 11/10, 2016 at 15:30 Comment(2)
Quantities qw and qwT are not polynomials w.r.t rw (they contain log(rw)).Kimmie
I understand that...I tried rational_inequality solver as well, but that also gives the PolynomialErrorMommy
L
1

A rational function is a ratio of two polynomials, so a log is still not allowed. Try

solve_univariate_inequality

but note some inequalities are hard to solve.

Lucila answered 21/10, 2016 at 18:35 Comment(4)
It ran for more than 10 minutes before giving the following error: File "C:\Anaconda\lib\site-packages\sympy\core\mul.py", line 975, in _eval_is_rational r = _fuzzy_group((a.is_rational for a in self.args), quick_exit=True) RuntimeError: maximum recursion depth exceeded while calling a Python objectMommy
That's what I mean about some inequalities are hard to solve. Submit to the SymPy team your example as a bug, but you should be prepared to hear from them that they can't solve your inequality. If you can shorten the domain (it's a parameter to the function) that could make it work. What I suggest is using a numerical solution, or solve a reduced problem. If you give the entire picture (edit your question) there may be suggestions. Also considering trying Mathematica as a symbolic solver, it's pretty powerful.Lucila
That will certainly not be solved by a sympy. I answered there.Lucila
Sorry, up now .Lucila

© 2022 - 2024 — McMap. All rights reserved.