梯度下降法求函数收敛值

def f(x,y): 
  return x-y+2*x*x+2*x*y+y*y 
 
def fx(x,y):
  return 1+4*x+2*y
 
def fy(x,y): 
  return -1+2*x+2*y

lr = 0.001 
x, y = 0, 0
n_x, n_y = x, y
error = False
while error == False:
  n_x -= lr*fx(x,y)
  n_y -= lr*fy(x,y)
  if f(x,y) - f(n_x,n_y) < 1e-9:
    error = True
  x, y = n_x, n_y
  print(x,y)

 

你可能感兴趣的:(python)