x1+x2=4 X1-X2=2的画图呢?
import matplotlib.pyplot as plt
import numpy as np
# 设置 x1 的取值范围
x1 = np.linspace(-2, 8, 200)
# 计算对应的 x2 值
x2_line1 = 4 - x1 # 从方程 x1 + x2 = 4 推出 x2 = 4 - x1
x2_line2 = x1 - 2 # 从方程 x1 - x2 = 2 推出 x2 = x1 - 2
# 求两条直线的交点
# 解方程组: x1 + x2 = 4, x1 - x2 = 2
# 可通过代数解出:
# 加法得 2x1 = 6 → x1 = 3, 再代入得 x2 = 1
intersection = (3, 1)
# 开始画图
plt.figure(figsize=(6, 6))
plt.plot(x1, x2_line1, label=r'$x_1 + x_2 = 4$', color='blue')
plt.plot(x1, x2_line2, label=r'$x_1 - x_2 = 2$', color='green')
# 标出交点
plt.plot(*intersection, 'ro')
plt.text(intersection[0] + 0.2, intersection[1], f'({intersection[0]}, {intersection[1]})', color='red')
# 添加坐标轴等
plt.axhline(0, color='black', linewidth=1)
plt.axvline(0, color='black', linewidth=1)
plt.grid(True)
plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')
plt.title('Graphs of $x_1 + x_2 = 4$ and $x_1 - x_2 = 2$')
plt.legend()
plt.axis('equal')
plt.show()