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

83 行
2.4 KiB

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