Browse Source

Small changes

tags/v1.0
Christos Choutouridis 1 month ago
parent
commit
93cf747abd
4 changed files with 28 additions and 38 deletions
  1. +5
    -7
      Work 1/scripts/GivenEnv.m
  2. +1
    -2
      Work 1/scripts/bisection/bisection.m
  3. +12
    -15
      Work 1/scripts/bisection/bisection_epsilonFixed.m
  4. +10
    -14
      Work 1/scripts/bisection/bisection_lambdaFixed.m

Work 1/scripts/funGivenEnv.m → Work 1/scripts/GivenEnv.m View File

@@ -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];
titles = [title_f1; title_f2; title_f3];

Work 1/scripts/bisection.m → Work 1/scripts/bisection/bisection.m View File

@@ -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


Work 1/scripts/bisection_epsilonFixed.m → Work 1/scripts/bisection/bisection_epsilonFixed.m View File

@@ -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


Work 1/scripts/bisection_lambdaFixed.m → Work 1/scripts/bisection/bisection_lambdaFixed.m View File

@@ -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')

Loading…
Cancel
Save