2.9 求下列方程组的符号解和数值解

点击查看代码
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("学号:3004")

posted on 2024-09-12 19:20  黄元元  阅读(17)  评论(0编辑  收藏  举报