python求解方程的根

python求解方程的根

本文将用sympy模块求解方程:

安装方法:

pip install sympy

单变量方程

例题:求解 x 2 + 3 x + 2 x^2+3x+2 x2+3x+2的根

from sympy import *
x=Symbol("x")
expr=x**2+3*x+2
solve(expr)
[-2, -1]

多变量方程

例1:
{ x + 2 y − 1 = 0 x − y + 1 = 0 \begin{cases} x+2y-1=0\\ x-y+1=0\\ \end{cases} {x+2y1=0xy+1=0

from sympy import *
x,y=symbols("x,y")
eq1=x+2*y-1
eq2=x-y+1
solve([eq1,eq2],[x,y],dict=True)#dict=True以字典形式返回结果
[{x: -1/3, y: 2/3}]

例2:
{ x 2 + y 2 + 8 y = 0 x 2 + y 2 − 4 x = 0 \begin{cases} x^2+y^2+8y=0\\ x^2+y^2-4x=0\\ \end{cases} {x2+y2+8y=0x2+y24x=0

from sympy import *
x,y=symbols("x,y")
eq1=x**2+y**2+8*y
eq2=x**2+y**2-4*x
solve([eq1,eq2],[x,y],dict=True)
[{x: 0, y: 0}, {x: 16/5, y: -8/5}]

你可能感兴趣的:(python,python,开发语言)