Search code examples
pythonsympysolver

how to solve for a ratio of symbols


I am having trouble getting the sympy.py solve function to produce an answer to this problem. The answer should be Q/q = 2*sqrt(2).

import sympy as sy

q,Q,a, K = sy.symbols('q Q a K')

F21 = K * q * Q / a**2 * (-sy.I)
F41i = K * Q * Q / (2*a**2) * 1/sy.sqrt(2) * (sy.I)

Eqn = (F21 + F41i)
print(Eqn)
ans = sy.solve(Eqn,Q/q)
print(ans)

Solution

  • If the ratio does not appear as a literal expression, you must make it do so before attempting to solve for it. A common way to do this is to replace the numerator in the ratio with a dummy variable multiplied by the denominator, e.g.

    >>> solve(Eq(x, Q/q), Q)  # x = Q/q -> Q = q*x
    [q*x]
    >>> solve(Eqn.subs(Q, _[0]), x)
    [0, 2*sqrt(2)]