Overwrite fibonacci with Binet formula
This commit is contained in:
parent
634f90b25b
commit
23b60aac66
@ -3,7 +3,7 @@ function [a, b, N] = min_fibonacci(fun_expression, alpha, beta, epsilon, lambda)
|
|||||||
|
|
||||||
% Use Binet's formula instead of matlab's recursive fibonacci
|
% Use Binet's formula instead of matlab's recursive fibonacci
|
||||||
% implementation
|
% implementation
|
||||||
fib = @(n) ( ((1 + sqrt(5))^n - (1 - sqrt(5))^n) / (2^n * sqrt(5)) );
|
fibonacci = @(n) ( ((1 + sqrt(5))^n - (1 - sqrt(5))^n) / (2^n * sqrt(5)) );
|
||||||
|
|
||||||
% Error checking
|
% Error checking
|
||||||
if lambda <= 0 || epsilon <= 0
|
if lambda <= 0 || epsilon <= 0
|
||||||
@ -24,8 +24,8 @@ end
|
|||||||
|
|
||||||
% calculate x1, x2 of the first iteration, since the following iteration
|
% calculate x1, x2 of the first iteration, since the following iteration
|
||||||
% will not require to calculate both
|
% will not require to calculate both
|
||||||
x_1 = a(1) + (fib(N-2) / fib(N)) * (b(1) - a(1));
|
x_1 = a(1) + (fibonacci(N-2) / fibonacci(N)) * (b(1) - a(1));
|
||||||
x_2 = a(1) + (fib(N-1) / fib(N)) * (b(1) - a(1));
|
x_2 = a(1) + (fibonacci(N-1) / fibonacci(N)) * (b(1) - a(1));
|
||||||
|
|
||||||
% All but the last calculation
|
% All but the last calculation
|
||||||
for k = 1:N-2
|
for k = 1:N-2
|
||||||
@ -34,12 +34,12 @@ for k = 1:N-2
|
|||||||
a(k+1) = a(k);
|
a(k+1) = a(k);
|
||||||
b(k+1) = x_2;
|
b(k+1) = x_2;
|
||||||
x_2 = x_1;
|
x_2 = x_1;
|
||||||
x_1 = a(k+1) + (fib(N-k-2) / fib(N-k)) * (b(k+1) - a(k+1));
|
x_1 = a(k+1) + (fibonacci(N-k-2) / fibonacci(N-k)) * (b(k+1) - a(k+1));
|
||||||
else
|
else
|
||||||
a(k+1) = x_1;
|
a(k+1) = x_1;
|
||||||
b(k+1) = b(k);
|
b(k+1) = b(k);
|
||||||
x_1 = x_2;
|
x_1 = x_2;
|
||||||
x_2 = a(k+1) + (fib(N-k-1) / fib(N-k)) * (b(k+1) - a(k+1));
|
x_2 = a(k+1) + (fibonacci(N-k-1) / fibonacci(N-k)) * (b(k+1) - a(k+1));
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user