import numpy as np import matplotlib.pyplot as plt def f(x1, x2): return x1**2+2*x1+x2**2+x2 def gradf(x1, x2): gradx1 = 2*x1+2 gradx2 = 2*x2+1 return gradx1,gradx2 x1, x2 = 1, 1 y = f(x1, x2) eta = 0.2 for step in range(1000): gradf1, gradf2 = gradf(x1, x2) x1t = x1 - eta*gradf1 x2t = x2 - eta*gradf2 yt = f(x1t, x2t) if (yt < y): x1 = x1t x2 = x2t y = yt else: continue print(step, x1, x2, y)