|
- function [a, b, N] = min_fibonacci(fun_expression, alpha, beta, epsilon, lambda)
- %
-
- % Use Binet's formula instead of matlab's recursive fibonacci
- % implementation
- fib = @(n) ( ((1 + sqrt(5))^n - (1 - sqrt(5))^n) / (2^n * sqrt(5)) );
-
- % Error checking
- if lambda <= 0 || epsilon <= 0
- error ('Convergence criteria not met')
- end
-
- % Init variables
- a = alpha;
- b = beta;
- fun = matlabFunction(fun_expression);
-
- % calculate number of iterations
- N=0;
- while fibonacci(N) < (b(1) - a(1)) / lambda
- N = N + 1;
- end
-
-
- % calculate x1, x2 of the first iteration, since the following iteration
- % will not require to calculate both
- x_1 = a(1) + (fib(N-2) / fib(N)) * (b(1) - a(1));
- x_2 = a(1) + (fib(N-1) / fib(N)) * (b(1) - a(1));
-
- % All but the last calculation
- for k = 1:N-2
- % set new search interval
- if fun(x_1) < fun(x_2)
- a(k+1) = a(k);
- b(k+1) = x_2;
- x_2 = x_1;
- x_1 = a(k+1) + (fib(N-k-2) / fib(N-k)) * (b(k+1) - a(k+1));
- else
- a(k+1) = x_1;
- b(k+1) = b(k);
- x_1 = x_2;
- x_2 = a(k+1) + (fib(N-k-1) / fib(N-k)) * (b(k+1) - a(k+1));
- end
- end
-
- % Last calculation
- x_2 = x_1 + epsilon;
- if fun(x_1) < fun(x_2)
- a(N) = a(N-1);
- b(N) = x_1;
- else
- a(N) = x_1;
- b(N) = b(N-1);
- end
|