'Python: Problem updating solution vector and step size of Gradient Descent method

I have a system of three equations that simplifies to the sum of their squares, Z.

Z = (3*x-cos(y*z)-(1/2))**2 + ((x**2)-625*(y**2))**2 + (exp(-x*y) + 20*z + (10*pi - 3)/3)**2

I have already managed to calculate the gradient, its evaluation and the search direction. But for some reason, only the first iteration returns the desired result, and in subsequent iterations the same vector of solutions and two different step sizes are repeated over and over again. I already tried to change the way they are updated but I don't get any change.

def grad_descent(f,v,u,er,it):
  g = grad(f,v)
  print('  0 {:8.5f} {:8.5f} {:8.5f}'.format(u[0],u[1],u[2]))
  for k in range(it): 
    c = ev_grad(g,v,u)
    fm = ev_sol(f,v,u)
    s = cal_step(f,g,v,u)
    uk = []
    for i in range(len(c)):
      uk += [float(u[i]) - s*float(c[i])]
    u = uk.copy()
    print('{:3d} {:8.5f} {:8.5f} {:8.5f} {:12.5e} {:8.5f}'.format(k+1,u[0],u[1],u[2],float(fm),s))

This are my results.

0  0.00000  0.00000  0.00000

1  0.01125  0.00000 -0.52360  1.11912e+02  0.00125

2  0.01125  0.00000 -0.52360  2.14989e+00  0.00000

3  0.01125  0.00000 -0.52360  2.14989e+00  0.00000

4  0.01125  0.00000 -0.52360  2.14989e+00 -0.00000

5  0.01125  0.00000 -0.52360  2.14989e+00  0.00000

6  0.01125  0.00000 -0.52360  2.14989e+00 -0.00000

.....

Where I most suspect there is an error is in the part where I get the step size.

def cal_step(f,g,v,u):
  c = ev_grad(g,v,u)
  t = Symbol('t')
  xt = []
  for i in range(len(v)):
    xt += [float(u[i]) - t*float(c[i])]
  fs = f.subs(v[0],u[0])
  for i in range(1,len(v)):
    fs = fs.subs(v[i],xt[i])
  df = diff(fs,t)
  ddf = diff(df,t)
  s = 1
  for i in range(5):
    s = s - float(df.subs(t,s))/float(ddf.subs(t,s))
  return(s)

Link of the program in Colab in case it is necessary to review it:

https://colab.research.google.com/drive/1bNUNMmuZ_9sMg5HIFggYcF9j5OLwmIiX?usp=sharing



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source