OptimizationTechniques/Work 1/scripts/iterations_over_lambda.m
Christos Choutouridis d42725109a Report started
- Add output parameter to methods for objective function calls
 - Adjust the code to reduce objective function calls to some methods
2024-11-08 10:03:55 +02:00

86 lines
2.3 KiB
Matlab

function [] = iterations_over_lambda(method)
% Calculate and plot iteration needed for different lambda values.
%
% method: the minimum calculation method
% * bisections
% * golden_section
% * fibonacci
% * bisection_der
% return:
% none
%
% Load the functions and interval
GivenEnv;
fig_dir = 'figures';
if ~exist(fig_dir, 'dir')
mkdir(fig_dir);
end
% Setup
% ========================
%
% We need to test against the same lambda values for all the methods in
% order to compare them. And since epsilon (which is related to lambda)
% was given for the bisection method, we base our calculations to that.
%
%
% epsilon: e = 0.001
% lambda: l > 2e =>
% lambda_min: 0.0021
% lambda_max: 0.1
% N: 50 points (50 lambda values)
N = 50;
epsilon = 0.001;
lambda_min = 0.0021;
lambda_max = 0.1;
lambda = linspace(lambda_min, lambda_max, N);
k = zeros(1, N); % preallocate k
n = zeros(1, N); % preallocate n
%
% Call the minimum calculation method for each lambda value for each
% function and keep the number of iterations needed.
% Then plot and save the # of iterations k(lambda) for each function.
%
% note: In order to use the same method call for all methods, we force a
% common interface for minimum method functions. Thus some arguments
% will not be needed for some methods (epsilon is not needed for
% bisection _der for example).
%
figure('Name', "iterations_over_lambda_" + char(method), 'NumberTitle', 'off');
set(gcf, 'Position', [100, 100, 1280, 600]); % Set the figure size to HD
disp(" ");
for i = 1:length(funs)
for j = N:-1:1
[a, b, k(j), n(j)] = method(funs(i), a_0, b_0, epsilon, lambda(j));
end
fprintf('%20s(%34s ): [a, b]= [%f, %f], iters(min, max)= (%d, %d), calls(min, max)= (%d, %d)\n', ...
char(method), char(funs(i)), a(end), b(end), k(N), k(1), n(N), n(1) );
subplot(1, length(funs), i);
plot(lambda, n, '-b', 'LineWidth', 1.0);
title(titles(i), 'Interpreter', 'latex', 'FontSize', 16);
xlabel('lambda');
ylabel("Calls of f" + i);
end
%
% Print and save the figures
%
%fig_epsc = fullfile(fig_dir, "iter_over_lambda_" + char(method) + ".epsc");
fig_png = fullfile(fig_dir, "iter_over_lambda_" + char(method) + ".png");
%print(gcf, fig_epsc, '-depsc', '-r300');
print(gcf, fig_png, '-dpng', '-r300');
close(gcf);