From df7a86fae83637af96502486ba416b3d63b73fc5 Mon Sep 17 00:00:00 2001 From: Christos Choutouridis Date: Mon, 4 Nov 2024 20:59:32 +0200 Subject: [PATCH] golden_sector method added --- Work 1/scripts/GivenEnv.m | 2 +- Work 1/scripts/bisection/bisection.m | 4 +- Work 1/scripts/bisection/bisection_interval.m | 48 +++++++++++++++++++ ...lambdaFixed.m => bisection_over_epsilon.m} | 6 +-- ...epsilonFixed.m => bisection_over_lambda.m} | 2 +- .../scripts/golden_section/golden_section.m | 37 ++++++++++++++ .../golden_section/golden_section_interval.m | 45 +++++++++++++++++ .../golden_section_over_lambda.m | 39 +++++++++++++++ 8 files changed, 176 insertions(+), 7 deletions(-) create mode 100644 Work 1/scripts/bisection/bisection_interval.m rename Work 1/scripts/bisection/{bisection_lambdaFixed.m => bisection_over_epsilon.m} (82%) rename Work 1/scripts/bisection/{bisection_epsilonFixed.m => bisection_over_lambda.m} (93%) create mode 100644 Work 1/scripts/golden_section/golden_section.m create mode 100644 Work 1/scripts/golden_section/golden_section_interval.m create mode 100644 Work 1/scripts/golden_section/golden_section_over_lambda.m diff --git a/Work 1/scripts/GivenEnv.m b/Work 1/scripts/GivenEnv.m index 40f7b20..9fb08f3 100644 --- a/Work 1/scripts/GivenEnv.m +++ b/Work 1/scripts/GivenEnv.m @@ -1,5 +1,5 @@ % -% Select the given region: [-1,3] +% Select the given interval: [-1,3] a_0 = -1; b_0 = 3; diff --git a/Work 1/scripts/bisection/bisection.m b/Work 1/scripts/bisection/bisection.m index 3c3c316..6644f14 100644 --- a/Work 1/scripts/bisection/bisection.m +++ b/Work 1/scripts/bisection/bisection.m @@ -5,7 +5,7 @@ function [a, b, k] = bisection(fun, alpha, beta, epsilon, lambda) % % Error checking -if 2*epsilon >= lambda +if 2*epsilon >= lambda || lambda <= 0 error ('Convergence criteria not met') end @@ -21,7 +21,7 @@ while b(k) - a(k) > lambda x_1 = mid - epsilon; x_2 = mid + epsilon; - % set new search reange + % set new search interval k = k + 1; if fun(x_1) < fun(x_2) a(k) = a(k-1); diff --git a/Work 1/scripts/bisection/bisection_interval.m b/Work 1/scripts/bisection/bisection_interval.m new file mode 100644 index 0000000..b52e7ee --- /dev/null +++ b/Work 1/scripts/bisection/bisection_interval.m @@ -0,0 +1,48 @@ +% +% Keeping epsilon fixed, plot the [a,b] interval over the iterations for +% different lambda values (min, mid, max)) +% + + +% Clear workspace and load the functions and interval +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: 3 lambda values + +N = 3; +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 +% * Plot the [a,b] interval over iterations for each lambda for each function +% + +for i = 1:length(funs) + figure; + for j = 1:N + [a, b, k(j)] = bisection(funs{i}, a_0, b_0, epsilon, lambda(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') + end + xlabel("Iterations @lambda=" + lambda(j)) + ylabel('[a_k, b_k]') + end +end + + \ No newline at end of file diff --git a/Work 1/scripts/bisection/bisection_lambdaFixed.m b/Work 1/scripts/bisection/bisection_over_epsilon.m similarity index 82% rename from Work 1/scripts/bisection/bisection_lambdaFixed.m rename to Work 1/scripts/bisection/bisection_over_epsilon.m index 96d1346..ee20c99 100644 --- a/Work 1/scripts/bisection/bisection_lambdaFixed.m +++ b/Work 1/scripts/bisection/bisection_over_epsilon.m @@ -1,10 +1,10 @@ % -% Keeping l (accuracy) fixed, test the iteration needed for different +% Keeping lambda (accuracy) fixed, test the iteration needed for different % epsilon values. % -% Clear workspace and load the functions and region +% Clear workspace and load the functions and interval clear addpath('..'); GivenEnv; @@ -27,7 +27,7 @@ k = zeros(1,N); % preallocate k % keep the number of iterations needed. % * Plot the iterations k(epsilon) for each function % -for i = 1:size(funs,2) +for i = 1:length(funs) for j = 1:N [a, b, k(j)] = bisection(funs{i}, a_0, b_0, epsilon(j), lambda); end diff --git a/Work 1/scripts/bisection/bisection_epsilonFixed.m b/Work 1/scripts/bisection/bisection_over_lambda.m similarity index 93% rename from Work 1/scripts/bisection/bisection_epsilonFixed.m rename to Work 1/scripts/bisection/bisection_over_lambda.m index 51c6ce8..a415636 100644 --- a/Work 1/scripts/bisection/bisection_epsilonFixed.m +++ b/Work 1/scripts/bisection/bisection_over_lambda.m @@ -4,7 +4,7 @@ % -% Clear workspace and load the functions and region +% Clear workspace and load the functions and interval clear addpath('..'); GivenEnv; diff --git a/Work 1/scripts/golden_section/golden_section.m b/Work 1/scripts/golden_section/golden_section.m new file mode 100644 index 0000000..681089c --- /dev/null +++ b/Work 1/scripts/golden_section/golden_section.m @@ -0,0 +1,37 @@ +function [a, b, k] = golden_section(fun, alpha, beta, lambda) +% + + +% Error checking +if lambda <= 0 + error ('Convergence criteria not met') +end + +% Init variables +gamma = 0.618; +a = alpha; +b = beta; + + +% calculate x1, x2 of the first iteration, since the following iteration +% will not require to calculate both +k=1; +x_1 = a(k) + (1 - gamma)*(b(k) - a(k)); +x_2 = a(k) + gamma*(b(k) - a(k)); + +while b(k) - a(k) > lambda + % set new search interval + k = k + 1; + if fun(x_1) < fun(x_2) + a(k) = a(k-1); + b(k) = x_2; + x_2 = x_1; + x_1 = a(k) + (1 - gamma)*(b(k) - a(k)); + else + a(k) = x_1; + b(k) = b(k-1); + x_1 = x_2; + x_2 = a(k) + gamma*(b(k) - a(k)); + end +end + diff --git a/Work 1/scripts/golden_section/golden_section_interval.m b/Work 1/scripts/golden_section/golden_section_interval.m new file mode 100644 index 0000000..d56e1aa --- /dev/null +++ b/Work 1/scripts/golden_section/golden_section_interval.m @@ -0,0 +1,45 @@ +% +% Plot the [a,b] interval over the iterations for different lambda +% values (min, mid, max)) +% + + +% Clear workspace and load the functions and interval +clear +addpath('..'); +GivenEnv; + +% * lambda_min: 0.0001 +% * lambda_max: 0.1 +% * N: 3 lambda values + +N = 3; +lambda_min = 0.0001; +lambda_max = 0.1; +lambda = linspace(lambda_min, lambda_max, N); +k = zeros(1, N); % preallocate k + + +% +% * Call the golden_sector method for each lambda value for each function and +% keep the number of iterations needed. +% * Plot the [a,b] interval over iterations for each lambda for each function +% + +for i = 1:length(funs) + figure; + for j = 1:N + [a, b, k(j)] = golden_section(funs{i}, a_0, b_0, lambda(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') + end + xlabel("Iterations @lambda=" + lambda(j)) + ylabel('[a_k, b_k]') + end +end + + \ No newline at end of file diff --git a/Work 1/scripts/golden_section/golden_section_over_lambda.m b/Work 1/scripts/golden_section/golden_section_over_lambda.m new file mode 100644 index 0000000..fbfeaa8 --- /dev/null +++ b/Work 1/scripts/golden_section/golden_section_over_lambda.m @@ -0,0 +1,39 @@ +% +% Test the iteration needed for different lambda values. +% + + +% Clear workspace and load the functions and interval +clear +addpath('..'); +GivenEnv; + +% * lambda_min: 0.0001 +% * lambda_max: 0.1 +% * N: 50 points + +N = 50; +lambda_min = 0.0001; +lambda_max = 0.1; +lambda = linspace(lambda_min, lambda_max, N); +k = zeros(1, N); % preallocate k + + +% +% * Call the golden_sector 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)] = golden_section(funs{i}, a_0, b_0, 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 + + \ No newline at end of file