From 93cf747abd8b6bed3af6c88cc8a52239005041a2 Mon Sep 17 00:00:00 2001 From: Christos Choutouridis Date: Mon, 4 Nov 2024 12:56:10 +0200 Subject: [PATCH] Small changes --- Work 1/scripts/{funGivenEnv.m => GivenEnv.m} | 12 ++++----- Work 1/scripts/{ => bisection}/bisection.m | 3 +-- .../{ => bisection}/bisection_epsilonFixed.m | 27 +++++++++---------- .../{ => bisection}/bisection_lambdaFixed.m | 24 +++++++---------- 4 files changed, 28 insertions(+), 38 deletions(-) rename Work 1/scripts/{funGivenEnv.m => GivenEnv.m} (62%) rename Work 1/scripts/{ => bisection}/bisection.m (92%) rename Work 1/scripts/{ => bisection}/bisection_epsilonFixed.m (69%) rename Work 1/scripts/{ => bisection}/bisection_lambdaFixed.m (69%) diff --git a/Work 1/scripts/funGivenEnv.m b/Work 1/scripts/GivenEnv.m similarity index 62% rename from Work 1/scripts/funGivenEnv.m rename to Work 1/scripts/GivenEnv.m index e744843..40f7b20 100644 --- a/Work 1/scripts/funGivenEnv.m +++ b/Work 1/scripts/GivenEnv.m @@ -4,15 +4,13 @@ a_0 = -1; b_0 = 3; % Setup the functions under test -syms x; - -f_1 = (x-2)^2 + x*log(x+3); -f_2 = exp(-2*x) + (x-2)^2; -f_3 = exp(x)*(x^3 - 1) + (x-1)*sin(x); -funs = [f_1, f_2, f_3]; +f_1 = @(x) (x-2)^2 + x*log(x+3); +f_2 = @(x) exp(-2*x) + (x-2)^2; +f_3 = @(x) exp(x)*(x^3 - 1) + (x-1)*sin(x); +funs = {f_1, f_2, f_3}; % Setup the function titles title_f1 = "$f_1(x) = (x - 2)^2 + x \cdot \ln(x + 3)$"; title_f2 = "$f_2(x) = e^{-2x} + (x - 2)^2$"; title_f3 = "$f_3(x) = e^x \cdot (x^3 - 1) + (x - 1) \cdot \sin(x)$"; -titles = [title_f1; title_f2; title_f3]; \ No newline at end of file +titles = [title_f1; title_f2; title_f3]; diff --git a/Work 1/scripts/bisection.m b/Work 1/scripts/bisection/bisection.m similarity index 92% rename from Work 1/scripts/bisection.m rename to Work 1/scripts/bisection/bisection.m index 735cd88..3c3c316 100644 --- a/Work 1/scripts/bisection.m +++ b/Work 1/scripts/bisection/bisection.m @@ -23,7 +23,7 @@ while b(k) - a(k) > lambda % set new search reange k = k + 1; - if subs(fun, x_1) < subs(fun, x_2) + if fun(x_1) < fun(x_2) a(k) = a(k-1); b(k) = x_2; else @@ -31,4 +31,3 @@ while b(k) - a(k) > lambda b(k) = b(k-1); end end - diff --git a/Work 1/scripts/bisection_epsilonFixed.m b/Work 1/scripts/bisection/bisection_epsilonFixed.m similarity index 69% rename from Work 1/scripts/bisection_epsilonFixed.m rename to Work 1/scripts/bisection/bisection_epsilonFixed.m index 2b93e8c..51c6ce8 100644 --- a/Work 1/scripts/bisection_epsilonFixed.m +++ b/Work 1/scripts/bisection/bisection_epsilonFixed.m @@ -6,22 +6,22 @@ % Clear workspace and load the functions and region clear - -funGivenEnv; +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 -% * size: 25 points +% * N: 50 points -size = 25; +N = 50; epsilon = 0.001; dl = 0.0001; lambda_max= 0.1; -lambda = linspace(2*epsilon + dl, lambda_max, size); -k = zeros(1,size); +lambda = linspace(2*epsilon + dl, lambda_max, N); +k = zeros(1, N); % preallocate k % @@ -29,19 +29,16 @@ k = zeros(1,size); % keep the number of iterations needed. % * Plot the iterations k(lambda) for each function % -i = 0; -for f = funs - i = i + 1; - j = 0; - for l = lambda - j = j + 1; - [a, b, k(j)] = bisection(f, a_0, b_0, epsilon, l); + +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, 3, i) + 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 + \ No newline at end of file diff --git a/Work 1/scripts/bisection_lambdaFixed.m b/Work 1/scripts/bisection/bisection_lambdaFixed.m similarity index 69% rename from Work 1/scripts/bisection_lambdaFixed.m rename to Work 1/scripts/bisection/bisection_lambdaFixed.m index 28d98ab..96d1346 100644 --- a/Work 1/scripts/bisection_lambdaFixed.m +++ b/Work 1/scripts/bisection/bisection_lambdaFixed.m @@ -6,20 +6,20 @@ % Clear workspace and load the functions and region clear - -funGivenEnv; +addpath('..'); +GivenEnv; % * lambda = 0.01 % * epsilon: e < lambda/2 = 0.005 % * de: A small step away from zero and lambda/2 % de = 0.0001 -% * size: 25 points +% * N: 50 points -size = 25; +N = 50; lambda = 0.01; de = 0.0001; -epsilon = linspace(de, (lambda/2)-de, size); -k = zeros(1,size); +epsilon = linspace(de, (lambda/2)-de, N); +k = zeros(1,N); % preallocate k % @@ -27,15 +27,11 @@ k = zeros(1,size); % keep the number of iterations needed. % * Plot the iterations k(epsilon) for each function % -i = 0; -for f = funs - i = i + 1; - j = 0; - for e = epsilon - j = j + 1; - [a, b, k(j)] = bisection(f, a_0, b_0, e, lambda); +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, 3, i) + subplot(1, length(funs), i) plot(epsilon, k, '-b', 'LineWidth', 1.0) title(titles(i), 'Interpreter', 'latex') xlabel('epsilon')