golden_sector method added
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+3
-3
@@ -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
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
%
|
||||
|
||||
|
||||
% Clear workspace and load the functions and region
|
||||
% Clear workspace and load the functions and interval
|
||||
clear
|
||||
addpath('..');
|
||||
GivenEnv;
|
||||
Reference in New Issue
Block a user