import matplotlib.pyplot as plt

# 以下の連立方程式を解く
# x + 2y = 0
# 2x - 3y = 7

# とりあえず適当な値で初期化
x = 0
y = 0

eps = 1.0e-6
step = []
xResult = []
yResult = []

for i in range(100):
    xp = x
    yp = y

    x = -2 * yp
    y = (2 * xp - 7) / 3
    
    step.append(i)
    xResult.append(x)
    yResult.append(y)

    # 前回とほぼ同じ値だったら、それが解なのでループ脱出
    if abs(x - xp) + abs(y - yp) <= eps:
        break

plt.plot(step, xResult, label="x")
plt.plot(step, yResult, label="y")
plt.xlabel('x - step')
plt.ylabel('y - solution')
plt.title('Jacobi method')
plt.grid(axis='both')
plt.legend() # これがないとラベル出ない
plt.show()

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: