THMMY's "Optimization Techniques" course assignments.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

39 行
1.4 KiB

  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