OptimizationTechniques/Work 1/scripts/interval_over_iterations.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

89 lines
2.6 KiB
Matlab

function [] = interval_over_iterations(method)
% Calculate and plot the [a,b] interval over the iterations for different
% lambda values (min, mid, max)
%
% method: the minimum calculation method, one of:
% * 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: 3 points (3 lambda values min-mid-max)
N = 3;
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 the [a,b] interval over iterations for each 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).
%
disp(" ");
for i = 1:length(funs)
figure('Name', "interval_over_iterations_" + char(method) + "_fun" + i, 'NumberTitle', 'off');
set(gcf, 'Position', [100, 100, 1280, 720]); % Set the figure size to HD
for j = 1:N
[a, b, k(j), n(j)] = method(funs(i), a_0, b_0, epsilon, lambda(j));
fprintf('%20s(%34s ): [a, b]= [%f, %f], @lambda=%f, iterations= %d\n', ...
char(method), char(funs(i)), a(end), b(end), lambda(j), k(j) );
subplot(length(funs), 1, j);
plot(1:length(a), a, 'ob');
hold on
plot(1:length(b), b, '*r');
if j == 1
title(titles(i), 'Interpreter', 'latex', 'FontSize', 16);
end
xlabel("Iterations @lambda=" + lambda(j));
ylabel('[a_k, b_k]');
end
% Print and save the figure
%fig_epsc = fullfile(fig_dir, "interval_over_iterations_" + char(method) + "_fun" + i + ".epsc");
fig_png = fullfile(fig_dir, "interval_over_iterations_" + char(method) + "_fun" + i + ".png");
%print(gcf, fig_epsc, '-depsc', '-r300');
print(gcf, fig_png, '-dpng', '-r300');
close(gcf);
end