A first version of the assignment
|
Before Width: | Height: | Size: 115 KiB After Width: | Height: | Size: 115 KiB |
|
Before Width: | Height: | Size: 114 KiB After Width: | Height: | Size: 114 KiB |
|
Before Width: | Height: | Size: 117 KiB After Width: | Height: | Size: 117 KiB |
|
Before Width: | Height: | Size: 115 KiB After Width: | Height: | Size: 115 KiB |
|
Before Width: | Height: | Size: 115 KiB After Width: | Height: | Size: 115 KiB |
|
Before Width: | Height: | Size: 118 KiB After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 123 KiB After Width: | Height: | Size: 122 KiB |
|
Before Width: | Height: | Size: 122 KiB After Width: | Height: | Size: 122 KiB |
|
Before Width: | Height: | Size: 124 KiB After Width: | Height: | Size: 124 KiB |
|
Before Width: | Height: | Size: 121 KiB After Width: | Height: | Size: 121 KiB |
|
Before Width: | Height: | Size: 121 KiB After Width: | Height: | Size: 121 KiB |
|
Before Width: | Height: | Size: 123 KiB After Width: | Height: | Size: 123 KiB |
|
Before Width: | Height: | Size: 81 KiB After Width: | Height: | Size: 81 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 84 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 78 KiB |
|
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 78 KiB |
@@ -17,6 +17,8 @@ function [a, b, k, n] = min_bisection(fun_expr, alpha, beta, epsilon, lambda)
|
||||
% n: (number) The calls of objective function fun_expr
|
||||
%
|
||||
|
||||
|
||||
|
||||
% Error checking
|
||||
if alpha > beta || 2*epsilon >= lambda || lambda <= 0
|
||||
error ('Input criteria not met')
|
||||
@@ -25,10 +27,16 @@ end
|
||||
% Init
|
||||
a = alpha;
|
||||
b = beta;
|
||||
n = 0;
|
||||
fun = matlabFunction(fun_expr);
|
||||
|
||||
k=1;
|
||||
% wrapper call count function
|
||||
function r = count_fun(x)
|
||||
n = n + 1;
|
||||
r = fun(x);
|
||||
end
|
||||
|
||||
k=1;
|
||||
while b(k) - a(k) > lambda
|
||||
% bisect [a,b]
|
||||
mid = (a(k) + b(k)) / 2;
|
||||
@@ -37,7 +45,7 @@ while b(k) - a(k) > lambda
|
||||
|
||||
% set new search interval
|
||||
k = k + 1;
|
||||
if fun(x_1) < fun(x_2)
|
||||
if count_fun(x_1) < count_fun(x_2)
|
||||
a(k) = a(k-1);
|
||||
b(k) = x_2;
|
||||
else
|
||||
@@ -46,5 +54,4 @@ while b(k) - a(k) > lambda
|
||||
end
|
||||
end
|
||||
|
||||
% Set objective function calls
|
||||
n = 2*k;
|
||||
end
|
||||
|
||||
@@ -29,8 +29,16 @@ end
|
||||
% Init output vectors
|
||||
a = alpha;
|
||||
b = beta;
|
||||
n = 0;
|
||||
dfun = matlabFunction(diff(fun_expression));
|
||||
|
||||
% wrapper call count function
|
||||
% In this case the derivative of the objective function
|
||||
function r = count_dfun(x)
|
||||
n = n + 1;
|
||||
r = dfun(x);
|
||||
end
|
||||
|
||||
k=1;
|
||||
while b(k) - a(k) > lambda
|
||||
% bisect [a,b]
|
||||
@@ -38,7 +46,7 @@ while b(k) - a(k) > lambda
|
||||
|
||||
% set new search interval
|
||||
k = k + 1;
|
||||
df = dfun(x_mid);
|
||||
df = count_dfun(x_mid);
|
||||
if df < 0
|
||||
a(k) = x_mid;
|
||||
b(k) = b(k-1);
|
||||
@@ -52,6 +60,4 @@ while b(k) - a(k) > lambda
|
||||
end
|
||||
end
|
||||
|
||||
% Update the objctive function calls. In this case the derivative of the
|
||||
% function ;)
|
||||
n = k;
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
function [a, b, N, nn] = min_fibonacci(fun_expression, alpha, beta, epsilon, lambda)
|
||||
function [a, b, N, n] = min_fibonacci(fun_expression, alpha, beta, epsilon, lambda)
|
||||
% Fibonacci method for finding the local minimum of a function.
|
||||
%
|
||||
% fun_expr: (symbolic expression over x) The symbolic expression of the
|
||||
@@ -29,8 +29,15 @@ fibonacci = @(n) ( ((1 + sqrt(5))^n - (1 - sqrt(5))^n) / (2^n * sqrt(5)) );
|
||||
% Init variables
|
||||
a = alpha;
|
||||
b = beta;
|
||||
n = 0;
|
||||
fun = matlabFunction(fun_expression);
|
||||
|
||||
% wrapper call count function
|
||||
function r = count_fun(x)
|
||||
n = n + 1;
|
||||
r = fun(x);
|
||||
end
|
||||
|
||||
% calculate number of iterations
|
||||
N = 0;
|
||||
while fibonacci(N) < (b(1) - a(1)) / lambda
|
||||
@@ -42,26 +49,33 @@ end
|
||||
% will not require to calculate both
|
||||
x_1 = a(1) + (fibonacci(N-2) / fibonacci(N)) * (b(1) - a(1));
|
||||
x_2 = a(1) + (fibonacci(N-1) / fibonacci(N)) * (b(1) - a(1));
|
||||
f1 = count_fun(x_1);
|
||||
f2 = count_fun(x_2);
|
||||
|
||||
% All but the last calculation
|
||||
for k = 1:N-2
|
||||
% set new search interval
|
||||
if fun(x_1) < fun(x_2)
|
||||
if f1 <= f2
|
||||
a(k+1) = a(k);
|
||||
b(k+1) = x_2;
|
||||
x_2 = x_1;
|
||||
f2 = f1;
|
||||
x_1 = a(k+1) + (fibonacci(N-k-2) / fibonacci(N-k)) * (b(k+1) - a(k+1));
|
||||
f1 = count_fun(x_1);
|
||||
else
|
||||
a(k+1) = x_1;
|
||||
b(k+1) = b(k);
|
||||
x_1 = x_2;
|
||||
f1 = f2;
|
||||
x_2 = a(k+1) + (fibonacci(N-k-1) / fibonacci(N-k)) * (b(k+1) - a(k+1));
|
||||
f2 = count_fun(x_2);
|
||||
end
|
||||
end
|
||||
|
||||
% Last calculation
|
||||
x_2 = x_1 + epsilon;
|
||||
if fun(x_1) < fun(x_2)
|
||||
f2 = count_fun(x_2);
|
||||
if f1 <= f2
|
||||
a(N) = a(N-1);
|
||||
b(N) = x_1;
|
||||
else
|
||||
@@ -69,6 +83,5 @@ else
|
||||
b(N) = b(N-1);
|
||||
end
|
||||
|
||||
% Set objective function calls
|
||||
nn = 2*N -2;
|
||||
end
|
||||
|
||||
|
||||
@@ -29,36 +29,42 @@ end
|
||||
gamma = 0.618;
|
||||
a = alpha;
|
||||
b = beta;
|
||||
n = 0;
|
||||
fun = matlabFunction(fun_expression);
|
||||
|
||||
% wrapper call count function
|
||||
function r = count_fun(x)
|
||||
n = n + 1;
|
||||
r = fun(x);
|
||||
end
|
||||
|
||||
% 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));
|
||||
|
||||
f1 = fun(x_1);
|
||||
f2 = fun(x_2);
|
||||
f1 = count_fun(x_1);
|
||||
f2 = count_fun(x_2);
|
||||
while b(k) - a(k) > lambda
|
||||
% set new search interval
|
||||
k = k + 1;
|
||||
if f1 < f2
|
||||
if f1 <= f2
|
||||
a(k) = a(k-1);
|
||||
b(k) = x_2;
|
||||
x_2 = x_1;
|
||||
f2 = f1;
|
||||
x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
|
||||
f1 = fun(x_1);
|
||||
f1 = count_fun(x_1);
|
||||
else
|
||||
a(k) = x_1;
|
||||
b(k) = b(k-1);
|
||||
x_1 = x_2;
|
||||
f1 = f2;
|
||||
x_2 = a(k) + gamma*(b(k) - a(k));
|
||||
f2 = fun(x_2);
|
||||
f2 = count_fun(x_2);
|
||||
end
|
||||
end
|
||||
|
||||
% Set objective function calls
|
||||
n = k+1;
|
||||
end
|
||||
|
||||
|
||||