sympy求解符号方程
sympy.solve()
求解方程evalf
代入数值
import sympy
sympy.init_printing()
a, b, c, x = sympy.symbols('a, b, c, x')
func = a * x * x + b * x + c
print('符号解:', sympy.solve(func, x))
print('数值解:', sympy.solve(func.evalf(subs={a: 1, b: -3, c: 2}), x))
符号解: [(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
数值解: [1.00000000000000, 2.00000000000000]
也可以这样写
a, b, c, x = sympy.symbols('a, b, c, x')
func = a * x * x + b * x + c
res = sympy.solve(func, x)
print(res)
print(res[0].subs([(a, 1), (b, -3), (c, 2)]))
print(res[1].subs([(a, 1), (b, -3), (c, 2)]))
符号解: [(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
2
1