import sympy as sp
定义变量
x, y = sp.symbols('x y')
定义方程组
equation1 = sp.Eq(x**2 - y - x, 3)
equation2 = sp.Eq(x + 3*y, 2)
解方程组
solutions = sp.solve((equation1, equation2), (x, y), dict=True)
print("符号解:")
for sol in solutions:
print(sol)
from scipy.optimize import fsolve
import numpy as np
定义方程组函数
def equations(vars):
x, y = vars
return [x**2 - y - x - 3, x + 3*y - 2]
初始猜测值
initial_guess = [1, 0]
解方程组
solution = fsolve(equations, initial_guess)
print("数值解:")
print(f"x = {solution[0]}, y = {solution[1]}")
print("2023310143005")