KKK
KKK

Reputation: 125

Solving equation equal to zero

How I can equate an equation to zero then solve it (the purpose is to eliminate the denominator).

y=(x**2-2)/3*x

In Matlab this works:

solution= solve(y==0,x)

but not in python.

Upvotes: 0

Views: 13623

Answers (2)

smichr
smichr

Reputation: 19093

If you want only to eliminate the denominator, then you can split it into numerator and denominator. If the equation is already appear as a fraction and you want the numerator then

>>> y=(x**2-2)/(3*x); y  # note parentheses around denom, is that what you meant?
(x**2 - 2)/(3*x)
>>> numer(_)
x**2 - 2

But if the equation appears as a sum then you can put it over a denominator and perhaps factor to identify numerator factors that must be zero in order to solve the equation:

>>> y + x/(x**2+2)
x/(x**2 + 2) + (x**2 - 2)/(3*x)
>>> n, d = _.as_numer_denom(); (n, d)
(3*x**2 + (x**2 - 2)*(x**2 + 2), 3*x*(x**2 + 2))
>>> factor(n)
(x - 1)*(x + 1)*(x**2 + 4)
>>> solve(_)
[-1, 1, -2*I, 2*I]

You don't have to factor the numerator before attempting to solve, however. But I sometimes find it useful when working with a specific equation.

If you have an example of an equation that is solved quickly elsewhere but not in SymPy, please post it.

Upvotes: 0

DenverCoder1
DenverCoder1

Reputation: 2501

from sympy import *

x, y = symbols('x y') 

y=(x**2-2)/3*x

# set the expression, y, equal to 0 and solve
result = solve(Eq(y, 0))

print(result)

Another solution:

from sympy import *

x, y = symbols('x y')

equation = Eq(y, (x**2-2)/3*x)

# Use sympy.subs() method
result = solve(equation.subs(y, 0))

print(result)

Edit (even simpler):

from sympy import *

x, y = symbols('x y') 

y=(x**2-2)/3*x

# solve the expression y (by default set equal to 0)
result = solve(y)

print(result)

Upvotes: 3

Related Questions