1
votes

I have been looking for an answer now and I can't find a solution. I am trying to use sympy.solve in Python 3 and I only want to see the real roots and all positive answers.

Sympy is able to solve what equation I want, but It shows a bunch of nonsense as the other answers. I have tried some of the other methods I found like solveset but they seem to still show the complex solutions

Image of Sympy solution

This is my code if that helps:

h, rho_c_s, T_c_s, k, pi, M, M_sun, m_H, m_e, mu_e = symbols(
    r"h,\rho_\odot,T_\odot,k,\pi,M,M_\odot,m_H,m_e,\mu_e",positive=True,real=True)

rho_c = rho_c_s * (M / M_sun)**(-2 / 7)
T_c = T_c_s * (M / M_sun)**(4 / 7)

expr1 = (h**2) / (20 * m_e) * (3 / pi)**(2 / 3) * (rho_c /
                                                   (mu_e * m_H))**(5 / 3)
expr2 = (rho_c * k * T_c) / (mu_e * m_H)

expr = expr1 - expr2
eqn = Eq(expr, 0)
soln = solve(eqn, M, simplify=True,real=True,positive=True,)
init_printing()

soln

I appreciate your help. Thanks

1

1 Answers

0
votes

Well I figured it out.

You want to do:

soln[0]

And it will only give you: Solution