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_3_Newton.m 2.0 KiB

4 gün önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. % Define environment (functions, gradients etc...)
  2. GivenEnv
  3. % Define parameters
  4. max_iter = 300; % Maximum iterations
  5. tol = 1e-4; % Tolerance
  6. % Methods tuning
  7. amijo_beta = 0.5; % Armijo reduction factor
  8. amijo_sigma = 0.1; % Armijo condition constant
  9. % Point x0 = (0, 0)
  10. % x0 = [0, 0];
  11. % Point x0 = (0, 0)
  12. x0s = [0, 0; -1, 1 ; 1, -1]; % Initial points
  13. for i = 1:size(x0s, 1)
  14. x0 = x0s(i, :);
  15. point_str = "[" + x0(1) + ", " + x0(2) + "]";
  16. % Find the best fixed gamma
  17. k = zeros(100, 1);
  18. j = 1;
  19. n = linspace(0.1, 1.5, 100);
  20. for g = n
  21. gamma_fixed_step = g;
  22. [~, ~, k(j)] = newton(fun, grad_fun, hessian_fun, x0, tol, max_iter, 'fixed');
  23. j = j + 1;
  24. end
  25. plotIterationsOverGamma(n, k, "Iteration for different $\gamma$ values", "Newton_Iter_o_gamma_" + i + ".png");
  26. [~, j] = min(k);
  27. gamma_fixed_step = n(j);
  28. [x_fixed, f_fixed, kk] = newton(fun, grad_fun, hessian_fun, x0, tol, max_iter, 'fixed');
  29. 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));
  30. plotPointsOverContour(x_fixed, fun, [-2, 2], [-3, 3], 100, point_str + ": Newton $\gamma$ = " + gamma_fixed_step, "Newton_fixed_" + i + ".png");
  31. % Minimized f
  32. [x_minimized, f_minimized, kk] = newton(fun, grad_fun, hessian_fun, x0, tol, max_iter, 'minimized');
  33. 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));
  34. plotPointsOverContour(x_minimized, fun, [-2, 2], [-3, 3], 100, point_str + ": Newton minimized $f(x_k + \gamma_kd_k)$", "Newton_minimized_" + i + ".png");
  35. % Armijo Rule
  36. [x_armijo, f_armijo, kk] = newton(fun, grad_fun, hessian_fun, x0, tol, max_iter, 'armijo');
  37. 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));
  38. plotPointsOverContour(x_armijo, fun, [-2, 2], [-3, 3], 100, point_str + ": Newton Armijo method", "Newton_armijo_" + i + ".png");
  39. end