THMMY's "Optimization Techniques" course assignments.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

method_SteepDesc.m 1.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839
  1. function [x_vals, f_vals, k] = method_SteepDesc(f, grad_f, xk, tol, max_iter, mode)
  2. % f: Objective function
  3. % grad_f: Gradient of the function
  4. % xk: Initial point [x0; y0]
  5. % tol: Tolerance for stopping criterion
  6. % max_iter: Maximum number of iterations
  7. % x_vals: Vector with the (x,y) values until minimum
  8. % f_vals: Vector with f(x,y) values until minimum
  9. % k: Number of iterations
  10. if strcmp(mode, 'armijo') == 1
  11. gamma_f = @(f, grad_f, dk, xk) gamma_armijo(f, grad_f, dk, xk);
  12. elseif strcmp(mode, 'minimized') == 1
  13. gamma_f = @(f, grad_f, dk, xk) gamma_minimized(f, grad_f, dk, xk);
  14. else % mode == 'fixed'
  15. gamma_f = @(f, grad_f, dk, xk) gamma_fixed(f, grad_f, dk, xk);
  16. end
  17. % Storage for iterations, begin with the first point
  18. x_vals = xk;
  19. f_vals = f(xk);
  20. for k = 1:max_iter
  21. % Check for convergence
  22. if norm(grad_f(xk)) < tol
  23. break;
  24. end
  25. dk = - grad_f(xk); % Steepset descent direction
  26. gk = gamma_f(f, grad_f, dk, xk); % Calculate gamma
  27. x_next = xk + gk * dk; % Update step
  28. f_next = f(x_next);
  29. xk = x_next; % Update point
  30. x_vals = [x_vals, x_next]; % Store values
  31. f_vals = [f_vals, f_next]; % Store function values
  32. end
  33. end