Small changes

This commit is contained in:
Christos Choutouridis
2024-11-04 12:56:10 +02:00
parent 59c61e5824
commit 93cf747abd
4 changed files with 28 additions and 38 deletions
+33
View File
@@ -0,0 +1,33 @@
function [a, b, k] = bisection(fun, alpha, beta, epsilon, lambda)
%
% Detailed explanation goes here
%
%
% Error checking
if 2*epsilon >= lambda
error ('Convergence criteria not met')
end
% Init output vectors
a = alpha;
b = beta;
k=1;
while b(k) - a(k) > lambda
% bisect [a,b]
mid = (a(k) + b(k)) / 2;
x_1 = mid - epsilon;
x_2 = mid + epsilon;
% set new search reange
k = k + 1;
if fun(x_1) < fun(x_2)
a(k) = a(k-1);
b(k) = x_2;
else
a(k) = x_1;
b(k) = b(k-1);
end
end
@@ -0,0 +1,44 @@
%
% Keeping epsilon fixed, test the iteration needed for different lambda
% values.
%
% Clear workspace and load the functions and region
clear
addpath('..');
GivenEnv;
% * epsilon: e = 0.001
% * lambda: l > 2e = 0.001
% * dl: A small step away from 2e
% dl = 0.0001
% * lambda_max: 0.1
% * N: 50 points
N = 50;
epsilon = 0.001;
dl = 0.0001;
lambda_max= 0.1;
lambda = linspace(2*epsilon + dl, lambda_max, N);
k = zeros(1, N); % preallocate k
%
% * Call the bisection method for each lambda value for each function and
% keep the number of iterations needed.
% * Plot the iterations k(lambda) for each function
%
for i = 1:length(funs)
for j = 1:N
[a, b, k(j)] = bisection(funs{i}, a_0, b_0, epsilon, lambda(j));
end
subplot(1, length(funs), i)
plot(lambda, k, '-b', 'LineWidth', 1.0)
title(titles(i), 'Interpreter', 'latex')
xlabel('lambda')
ylabel('Iterations')
end
@@ -0,0 +1,41 @@
%
% Keeping l (accuracy) fixed, test the iteration needed for different
% epsilon values.
%
% Clear workspace and load the functions and region
clear
addpath('..');
GivenEnv;
% * lambda = 0.01
% * epsilon: e < lambda/2 = 0.005
% * de: A small step away from zero and lambda/2
% de = 0.0001
% * N: 50 points
N = 50;
lambda = 0.01;
de = 0.0001;
epsilon = linspace(de, (lambda/2)-de, N);
k = zeros(1,N); % preallocate k
%
% * Call the bisection method for each epsilon value for each function and
% keep the number of iterations needed.
% * Plot the iterations k(epsilon) for each function
%
for i = 1:size(funs,2)
for j = 1:N
[a, b, k(j)] = bisection(funs{i}, a_0, b_0, epsilon(j), lambda);
end
subplot(1, length(funs), i)
plot(epsilon, k, '-b', 'LineWidth', 1.0)
title(titles(i), 'Interpreter', 'latex')
xlabel('epsilon')
ylabel('Iterations')
end