首页 > 解决方案 > Python; 求解等于零的方程

问题描述

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

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

在 Matlab 这工作:

solution= solve(y==0,x)

但不是在python中。

标签: pythonsympy

解决方案


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)

推荐阅读