蟒蛇;解方程等于零

时间:2020-02-24 10:33:29

标签: python sympy

我如何将方程式等于​​零然后求解(目的是消除分母)。

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

在Matlab中有效:

solution= solve(y==0,x)

,但不是在python中。

2 个答案:

答案 0 :(得分:1)

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)

另一种解决方案:

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)

编辑(甚至更简单):

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)

答案 1 :(得分:0)

如果只想消除分母,则可以将其分为分子和分母。如果方程式已经以分数形式出现并且您想要分子,那么

>>> 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

但是,如果方程式是一个总和,则可以将其放在分母和因子上,以标识必须为零的分子因子才能求解方程式:

>>> 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]

但是,在尝试求解时,您不必分解分子。但是有时在处理特定方程式时,它会很有用。

如果您有一个方程式示例可以在其他地方快速求解,而在SymPy中却没有,请发布它。

相关问题