THMMY's "Optimization Techniques" course assignments.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

Script_1_SteepDesc.m 1.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. % Define environment (functions, gradients etc...)
  2. GivenEnv
  3. % Define parameters
  4. max_iter = 1000; % Maximum iterations
  5. tol = 0.001; % Tolerance
  6. % Point x0 = (1, 1)
  7. % =========================================================================
  8. point = 1;
  9. x0 = [5, -5]';
  10. point_str = "[" + x0(1) + ", " + x0(2) + "]";
  11. f = fun(x0);
  12. gf = grad_fun(x0);
  13. hf = hessian_fun(x0);
  14. fprintf('Initial point (%d, %d), f = %f, grad = [%f;%f], hessian = [%f %f ; %f %f]=> Method applicable\n', x0, f, gf, hf);
  15. k = zeros(100, 1);
  16. j = 1;
  17. n = linspace(0.1, 1.5, 100);
  18. for g = n
  19. gamma_fixed_step = g;
  20. [~, ~, k(j)] = method_SteepDesc(fun, grad_fun, x0, tol, max_iter, 'fixed');
  21. j = j + 1;
  22. end
  23. plotItersOverGamma(n, k, "Iteration for different $\gamma$ values", "figures/StDes_Iter_o_gamma_" + point + ".png");
  24. for g=[0.1, 0.3, 3, 5]
  25. gamma_fixed_step = g;
  26. [x_fixed, f_fixed, kk] = method_SteepDesc(fun, grad_fun, x0, tol, max_iter, 'fixed');
  27. fprintf('Fixed step g=%f: Initial point (%f, %f), steps:%d, Final (x1,x2)=(%f, %f), f(x1,x2)=%f\n', g, x0, kk, x_fixed(:, end), f_fixed(end));
  28. if g <= 1
  29. plotPointsOverContour(x_fixed, fun, XSetLimmits(1, :), XSetLimmits(2, :), 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "figures/StDes_gamma_" + g + ".png");
  30. else
  31. plotPointsOverContour(x_fixed, fun, [-500, 500], [-500, 500], 100, point_str + ": Steepest descent $\gamma$ = " + gamma_fixed_step, "figures/StDes_gamma_" + g + ".png");
  32. end
  33. end