THMMY's "Optimization Techniques" course assignments.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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