THMMY's "Optimization Techniques" course assignments.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

61 Zeilen
2.0 KiB

  1. function [x_vals, f_vals, k] = method_lev_mar(f, grad_f, hessian_f, e, xk, tol, max_iter, mode)
  2. % f: Objective function
  3. % grad_f: Gradient of the function
  4. % hessian_f: Hessian of the function
  5. % e: Offset for hessian damping Hk' = Hk + mI
  6. % - when: Hk not positive defined
  7. % - Where: m = abs(min(eig(Hk))) + e
  8. % xk: Initial point [xk, yk]
  9. % tol: Tolerance for stopping criterion
  10. % max_iter: Maximum number of iterations
  11. % x_vals: Vector with the (x,y) values until minimum
  12. % f_vals: Vector with f(x,y) values until minimum
  13. % k: Number of iterations
  14. if strcmp(mode, 'armijo') == 1
  15. gamma_f = @(f, grad_f, dk, xk) gamma_armijo(f, grad_f, dk, xk);
  16. elseif strcmp(mode, 'minimized') == 1
  17. gamma_f = @(f, grad_f, dk, xk) gamma_minimized(f, grad_f, dk, xk);
  18. else % mode == 'fixed'
  19. gamma_f = @(f, grad_f, dk, xk) gamma_fixed(f, grad_f, dk, xk);
  20. end
  21. x_vals = xk; % Store iterations
  22. f_vals = f(xk(1), xk(2));
  23. for k = 1:max_iter
  24. grad = grad_f(xk(1), xk(2));
  25. % Check for convergence
  26. if norm(grad) < tol
  27. break;
  28. end
  29. hess = hessian_f(xk(1), xk(2));
  30. % Check if hessian is not positive defined
  31. lmin = min(eig(hess));
  32. if lmin <= 0
  33. % Select m with offset to stear hess to positive eigenvalues
  34. m = abs(lmin) + e;
  35. mI = m * eye(size(hess));
  36. if min(eig(hess + mI)) <= 0 % Fail-check
  37. warning('Can not normalize hessian matrix.');
  38. end
  39. end
  40. % Solve for search direction using Newton's step
  41. dk = - inv(hess + mI) * grad;
  42. % Calculate gamma
  43. gk = gamma_f(f, grad_f, dk, xk);
  44. x_next = xk + gk * dk'; % Update step
  45. f_next = f(x_next(1), x_next(2));
  46. xk = x_next; % Update point
  47. x_vals = [x_vals; x_next]; % Store values
  48. f_vals = [f_vals; f_next]; % Store function values
  49. end
  50. end