from sympy import symbols, Eq, solve
定义未知数
x, y = symbols('x y')
定义非线性方程组
equations = (Eq(x**2 - y - x - 3, 0), Eq(x + 3*y - 2, 0))
求解符号解
symbolic_solution = solve(equations, (x, y))
print("符号解:", symbolic_solution)
数值解需要使用数值方法求解
from scipy.optimize import fsolve
定义非线性方程组的函数
def equations(variables):
x, y = variables
return [x**2 - y - x - 3, x + 3*y - 2]
初始猜测值
initial_guess = [1, 1]
求解数值解
numeric_solution = fsolve(equations, initial_guess)
print("数值解:", numeric_solution)