python求解等于零的方程

2024-04-24 02:48:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我如何将一个方程等于零,然后求解它(目的是消除分母)

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

在Matlab中,这项工作:

solution= solve(y==0,x)

但不是用python


Tags: 目的方程solutionmatlabsolve分母
2条回答
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)

如果你只想消除分母,那么你可以把它分成分子和分母。如果方程已经显示为分数,而你想要分子,那么

>>> 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,请把它贴出来

相关问题 更多 >