THMMY's "Optimization Techniques" course assignments.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

65 lines
1.7 KiB

  1. %
  2. % Calculate and plot the iteration needed for different epsilon values,
  3. % keeping lambda (accuracy) fixed.
  4. %
  5. % Load the functions and interval
  6. GivenEnv;
  7. fig_dir = 'figures';
  8. if ~exist(fig_dir, 'dir')
  9. mkdir(fig_dir);
  10. end
  11. % Setup
  12. % ========================
  13. % lambda = 0.01
  14. % epsilon: e < lambda/2 = 0.005
  15. % de: A small step away from zero and lambda/2
  16. % de = 0.0001
  17. % N: 50 points (50 epsilon values)
  18. N = 50;
  19. lambda = 0.01;
  20. de = 0.0001;
  21. epsilon = linspace(de, (lambda/2)-de, N);
  22. k = zeros(1,N); % preallocate k
  23. n = zeros(1,N); % preallocate n
  24. %
  25. % Call the min_bisection method for each epsilon value for each
  26. % function and keep the number of iterations needed.
  27. % Then plot and save the # of iterations k(epsilon) for each function.
  28. %
  29. figure('Name', 'iterations_over_epsilon_min_bisection', 'NumberTitle', 'off');
  30. set(gcf, 'Position', [100, 100, 1280, 600]); % Set the figure size to HD
  31. for i = 1:length(funs)
  32. for j = 1:N
  33. [a, b, k(j), n(j)] = min_bisection(funs(i), a_0, b_0, epsilon(j), lambda);
  34. end
  35. fprintf('%20s(%34s ): [a, b]= [%f, %f], iters(min, max)= (%d, %d), calls(min, max)= (%d, %d)\n', ...
  36. "min_bisection", char(funs(i)), a(end), b(end), k(1), k(N), n(1), n(N) );
  37. subplot(1, length(funs), i);
  38. plot(epsilon, n, '-b', 'LineWidth', 1.0);
  39. title(titles(i), 'Interpreter', 'latex', 'FontSize', 16);
  40. xlabel('epsilon');
  41. ylabel("Calls of f" + i);
  42. end
  43. %
  44. % Print and save the figures
  45. %
  46. %fig_epsc = fullfile(fig_dir, "iter_over_epsilon_min_bisection" + ".epsc");
  47. fig_png = fullfile(fig_dir, "iter_over_epsilon_min_bisection" + ".png");
  48. %print(gcf, fig_epsc, '-depsc', '-r300');
  49. print(gcf, fig_png, '-dpng', '-r300');
  50. close(gcf);