@@ -4,15 +4,13 @@ a_0 = -1; | |||||
b_0 = 3; | b_0 = 3; | ||||
% Setup the functions under test | % 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 | % Setup the function titles | ||||
title_f1 = "$f_1(x) = (x - 2)^2 + x \cdot \ln(x + 3)$"; | title_f1 = "$f_1(x) = (x - 2)^2 + x \cdot \ln(x + 3)$"; | ||||
title_f2 = "$f_2(x) = e^{-2x} + (x - 2)^2$"; | 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)$"; | 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]; |
@@ -23,7 +23,7 @@ while b(k) - a(k) > lambda | |||||
% set new search reange | % set new search reange | ||||
k = k + 1; | k = k + 1; | ||||
if subs(fun, x_1) < subs(fun, x_2) | |||||
if fun(x_1) < fun(x_2) | |||||
a(k) = a(k-1); | a(k) = a(k-1); | ||||
b(k) = x_2; | b(k) = x_2; | ||||
else | else | ||||
@@ -31,4 +31,3 @@ while b(k) - a(k) > lambda | |||||
b(k) = b(k-1); | b(k) = b(k-1); | ||||
end | end | ||||
end | end | ||||
@@ -6,22 +6,22 @@ | |||||
% Clear workspace and load the functions and region | % Clear workspace and load the functions and region | ||||
clear | clear | ||||
funGivenEnv; | |||||
addpath('..'); | |||||
GivenEnv; | |||||
% * epsilon: e = 0.001 | % * epsilon: e = 0.001 | ||||
% * lambda: l > 2e = 0.001 | % * lambda: l > 2e = 0.001 | ||||
% * dl: A small step away from 2e | % * dl: A small step away from 2e | ||||
% dl = 0.0001 | % dl = 0.0001 | ||||
% * lambda_max: 0.1 | % * lambda_max: 0.1 | ||||
% * size: 25 points | |||||
% * N: 50 points | |||||
size = 25; | |||||
N = 50; | |||||
epsilon = 0.001; | epsilon = 0.001; | ||||
dl = 0.0001; | dl = 0.0001; | ||||
lambda_max= 0.1; | 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. | % keep the number of iterations needed. | ||||
% * Plot the iterations k(lambda) for each function | % * 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 | end | ||||
subplot(1, 3, i) | |||||
subplot(1, length(funs), i) | |||||
plot(lambda, k, '-b', 'LineWidth', 1.0) | plot(lambda, k, '-b', 'LineWidth', 1.0) | ||||
title(titles(i), 'Interpreter', 'latex') | title(titles(i), 'Interpreter', 'latex') | ||||
xlabel('lambda') | xlabel('lambda') | ||||
ylabel('Iterations') | ylabel('Iterations') | ||||
end | end | ||||
@@ -6,20 +6,20 @@ | |||||
% Clear workspace and load the functions and region | % Clear workspace and load the functions and region | ||||
clear | clear | ||||
funGivenEnv; | |||||
addpath('..'); | |||||
GivenEnv; | |||||
% * lambda = 0.01 | % * lambda = 0.01 | ||||
% * epsilon: e < lambda/2 = 0.005 | % * epsilon: e < lambda/2 = 0.005 | ||||
% * de: A small step away from zero and lambda/2 | % * de: A small step away from zero and lambda/2 | ||||
% de = 0.0001 | % de = 0.0001 | ||||
% * size: 25 points | |||||
% * N: 50 points | |||||
size = 25; | |||||
N = 50; | |||||
lambda = 0.01; | lambda = 0.01; | ||||
de = 0.0001; | 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. | % keep the number of iterations needed. | ||||
% * Plot the iterations k(epsilon) for each function | % * 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 | end | ||||
subplot(1, 3, i) | |||||
subplot(1, length(funs), i) | |||||
plot(epsilon, k, '-b', 'LineWidth', 1.0) | plot(epsilon, k, '-b', 'LineWidth', 1.0) | ||||
title(titles(i), 'Interpreter', 'latex') | title(titles(i), 'Interpreter', 'latex') | ||||
xlabel('epsilon') | xlabel('epsilon') |