THMMY's "Optimization Techniques" course assignments.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. function [] = interval_over_iterations(method)
  2. % Calculate and plot the [a,b] interval over the iterations for different
  3. % lambda values (min, mid, max)
  4. %
  5. % method: the minimum calculation method, one of:
  6. % * bisections
  7. % * golden_section
  8. % * fibonacci
  9. % * bisection_der
  10. % return:
  11. % none
  12. %
  13. % Load the functions and interval
  14. GivenEnv;
  15. fig_dir = 'figures';
  16. if ~exist(fig_dir, 'dir')
  17. mkdir(fig_dir);
  18. end
  19. % Setup
  20. % ========================
  21. %
  22. % We need to test against the same lambda values for all the methods in
  23. % order to compare them. And since epsilon (which is related to lambda)
  24. % was given for the bisection method, we base our calculations to that.
  25. %
  26. %
  27. % epsilon: e = 0.001
  28. % lambda: l > 2e =>
  29. % lambda_min: 0.0021
  30. % lambda_max: 0.1
  31. % N: 3 points (3 lambda values min-mid-max)
  32. N = 3;
  33. epsilon = 0.001;
  34. lambda_min = 0.0021;
  35. lambda_max = 0.1;
  36. lambda = linspace(lambda_min, lambda_max, N);
  37. k = zeros(1, N); % preallocate k
  38. n = zeros(1, N); % preallocate n
  39. %
  40. % Call the minimum calculation method for each lambda value for each
  41. % function and keep the number of iterations needed.
  42. % Then Plot the [a,b] interval over iterations for each lambda for each
  43. % function.
  44. %
  45. % note: In order to use the same method call for all methods, we force a
  46. % common interface for minimum method functions. Thus some arguments
  47. % will not be needed for some methods (epsilon is not needed for
  48. % bisection _der for example).
  49. %
  50. disp(" ");
  51. for i = 1:length(funs)
  52. figure('Name', "interval_over_iterations_" + char(method) + "_fun" + i, 'NumberTitle', 'off');
  53. set(gcf, 'Position', [100, 100, 1280, 720]); % Set the figure size to HD
  54. for j = 1:N
  55. [a, b, k(j), n(j)] = method(funs(i), a_0, b_0, epsilon, lambda(j));
  56. fprintf('%20s(%34s ): [a, b]= [%f, %f], @lambda=%f, iterations= %d\n', ...
  57. char(method), char(funs(i)), a(end), b(end), lambda(j), k(j) );
  58. subplot(length(funs), 1, j);
  59. plot(1:length(a), a, 'ob');
  60. hold on
  61. plot(1:length(b), b, '*r');
  62. if j == 1
  63. title(titles(i), 'Interpreter', 'latex', 'FontSize', 16);
  64. end
  65. xlabel("Iterations @lambda=" + lambda(j));
  66. ylabel('[a_k, b_k]');
  67. end
  68. % Print and save the figure
  69. %fig_epsc = fullfile(fig_dir, "interval_over_iterations_" + char(method) + "_fun" + i + ".epsc");
  70. fig_png = fullfile(fig_dir, "interval_over_iterations_" + char(method) + "_fun" + i + ".png");
  71. %print(gcf, fig_epsc, '-depsc', '-r300');
  72. print(gcf, fig_png, '-dpng', '-r300');
  73. close(gcf);
  74. end