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.

Script_2_Steepest_descent.m 4.8 KiB

3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
2 days ago
3 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. % Define environment (functions, gradients etc...)
  2. GivenEnv
  3. % Define parameters
  4. max_iter = 300; % Maximum iterations
  5. tol = 1e-4; % Tolerance
  6. % Point x0 = (0, 0)
  7. % =========================================================================
  8. point = 1;
  9. x0 = [0, 0];
  10. f = fun(x0(1), x0(2));
  11. gf = grad_fun(x0(1), x0(2));
  12. hf = hessian_fun(x0(1), x0(2));
  13. fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Can NOT use method\n', x0, f, gf, hf);
  14. disp(' ');
  15. % Point x0 = (-1, 1)
  16. % =========================================================================
  17. point = 2;
  18. x0 = [-1, 1];
  19. point_str = "[" + x0(1) + ", " + x0(2) + "]";
  20. f = fun(-1, 1);
  21. gf = grad_fun(x0(1), x0(2));
  22. hf = hessian_fun(x0(1), x0(2));
  23. fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Can use method\n', x0, f, gf, hf);
  24. % Find the best fixed gamma
  25. k = zeros(100, 1);
  26. j = 1;
  27. n = linspace(0.1, 1.5, 100);
  28. for g = n
  29. gamma_fixed_step = g;
  30. [~, ~, k(j)] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
  31. j = j + 1;
  32. end
  33. plotItersOverGamma(n, k, "Iteration for different $\gamma$ values", "figures/StDes_Iter_o_gamma_" + point + ".png");
  34. [~, j] = min(k);
  35. gamma_fixed_step = n(j);
  36. [x_fixed, f_fixed, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
  37. fprintf('Fixed step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
  38. plotPointsOverContour(x_fixed, fun, [-2, 0], [-2, 2], 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "figures/StDes_fixed_" + point + ".png");
  39. % Minimized f
  40. [x_minimized, f_minimized, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'minimized');
  41. fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_minimized(end, :), f_minimized(end));
  42. plotPointsOverContour(x_minimized, fun, [-2, 0], [-2, 2], 100, point_str + ": Steepest descent minimized $f(x_k + \gamma_kd_k)$", "figures/StDes_minimized_" + point + ".png");
  43. % Armijo Rule
  44. % Methods tuning
  45. amijo_beta = 0.4; % typical range: [0.1, 0.8]
  46. amijo_sigma = 0.1; % typical range: [0.01, 0.3]
  47. [x_armijo, f_armijo, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'armijo');
  48. fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_armijo(end, :), f_armijo(end));
  49. plotPointsOverContour(x_armijo, fun, [-2, 0], [-2, 2], 100, point_str + ": Steepest descent Armijo method", "figures/StDes_armijo_" + point + ".png");
  50. disp(' ');
  51. % Compare methods
  52. plotConvCompare(x_fixed, "Fixed", x_minimized, "Minimized", x_armijo, "Armijo", Xmin, "Convergence compare", "figures/StDes_compare_" + point + ".png");
  53. % Point x0 = (1, -1)
  54. % =========================================================================
  55. point = 3;
  56. x0 = [1, -1];
  57. point_str = "[" + x0(1) + ", " + x0(2) + "]";
  58. f = fun(-1, 1);
  59. gf = grad_fun(x0(1), x0(2));
  60. hf = hessian_fun(x0(1), x0(2));
  61. fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]. Can use method\n', x0, f, gf, hf);
  62. % Find the best fixed gamma
  63. k = zeros(100, 1);
  64. j = 1;
  65. n = linspace(0.1, 1, 100);
  66. for g = n
  67. gamma_fixed_step = g;
  68. [~, ~, k(j)] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
  69. j = j + 1;
  70. end
  71. %if min(k) == max_iter
  72. % fprintf('Fixed step: Initial point (%d, %d). Can NOT use method\n', x0);
  73. %end
  74. %plotItersOverGamma(n, k, "Iteration for different $\gamma$ values", "figures/StDes_Iter_o_gamma_" + point + ".png");
  75. %gamma_fixed_step = 1;
  76. [x_fixed, f_fixed, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'fixed');
  77. fprintf('Fixed step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_fixed(end, :), f_fixed(end));
  78. plotPointsOverContour(x_fixed, fun, [-1, 2], [-2, 2], 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "figures/StDes_fixed_" + point + ".png");
  79. % Minimized f
  80. [x_minimized, f_minimized, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'minimized');
  81. fprintf('Minimized f(g): Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_minimized(end, :), f_minimized(end));
  82. plotPointsOverContour(x_minimized, fun, [-2, 2], [-3, 2], 100, point_str + ": Steepest descent minimized $f(x_k + \gamma_kd_k)$", "figures/StDes_minimized_" + point + ".png");
  83. % Armijo Rule
  84. % Methods tuning
  85. amijo_beta = 0.4; % typical range: [0.1, 0.8]
  86. amijo_sigma = 0.1; % typical range: [0.01, 0.3]
  87. [x_armijo, f_armijo, kk] = method_steepest_descent(fun, grad_fun, x0, tol, max_iter, 'armijo');
  88. fprintf('Armijo step: Initial point (%f, %f), steps:%d, Final (x,y)=(%f, %f), f(x,y)=%f\n', x0, kk, x_armijo(end, :), f_armijo(end));
  89. plotPointsOverContour(x_armijo, fun, [-1, 2], [-2, 2], 100, point_str + ": Steepest descent Armijo method", "figures/StDes_armijo_" + point + ".png");