THMMY's "Optimization Techniques" course assignments.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

83 行
2.2 KiB

  1. function [] = iterations_over_lambda(method)
  2. % Plot iteration needed for different lambda values.
  3. %
  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: 50 points (50 lambda values)
  29. N = 50;
  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 and save the # of iterations k(lambda) for each function.
  39. %
  40. % note: In order to use the same method call for all methods, we force a
  41. % common interface for minimum method functions. Thus some arguments
  42. % will not be needed for some methods (epsilon is not needed for
  43. % bisection _der for example).
  44. %
  45. figure('Name', "iterations_over_lambda_" + char(method), 'NumberTitle', 'off');
  46. set(gcf, 'Position', [100, 100, 1280, 600]); % Set the figure size to HD
  47. disp(" ");
  48. for i = 1:length(funs)
  49. for j = N:-1:1
  50. [a, b, k(j)] = method(funs(i), a_0, b_0, epsilon, lambda(j));
  51. end
  52. fprintf('%20s(%34s ): [a, b]= [%f, %f], iterations(min, max)= (%d, %d)\n', ...
  53. char(method), char(funs(i)), a(end), b(end), k(N), k(1) );
  54. subplot(1, length(funs), i)
  55. plot(lambda, k, '-b', 'LineWidth', 1.0)
  56. title(titles(i), 'Interpreter', 'latex')
  57. xlabel('lambda')
  58. ylabel('Iterations')
  59. end
  60. %
  61. % Print and save the figures
  62. %
  63. %fig_epsc = fullfile(fig_dir, "iter_over_lambda_" + char(method) + ".epsc");
  64. fig_png = fullfile(fig_dir, "iter_over_lambda_" + char(method) + ".png");
  65. %print(gcf, fig_epsc, '-depsc', '-r300');
  66. print(gcf, fig_png, '-dpng', '-r300');