Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. def newton_method(x,f_x,gradient_f_x,hessian_f_x):
  2. try:
  3. inverse = np.linalg.inv(hessian_f_x(x))
  4. except np.linalg.LinAlgError:
  5. print('Hessian is non invertible')
  6. pass
  7. else:
  8. iters = 0
  9. f_values = []
  10. gradient_f = gradient_f_x(x)
  11. f = f_x(x)
  12. f_values.append(f)
  13. while (np.sqrt(gradient_f.dot(gradient_f))/(1+abs(f))) >= epsilon and iters<=itmax: # condition to achieve optimality
  14. iters+=1
  15. p = -np.dot(inverse,gradient_f_x(x)) # search direction
  16. x = x+p # update of solution point
  17. gradient_f = gradient_f_x(x)
  18. f = f_x(x)
  19. f_values.append(f)
  20. return (x,iters,f_values)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement