From 23b60aac660bd591320622e6eecf701fa015baa3 Mon Sep 17 00:00:00 2001 From: Christos Choutouridis Date: Wed, 6 Nov 2024 10:41:23 +0200 Subject: [PATCH] Overwrite fibonacci with Binet formula --- Work 1/scripts/min_fibonacci.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Work 1/scripts/min_fibonacci.m b/Work 1/scripts/min_fibonacci.m index 0265cdb..0ade515 100644 --- a/Work 1/scripts/min_fibonacci.m +++ b/Work 1/scripts/min_fibonacci.m @@ -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 % 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 if lambda <= 0 || epsilon <= 0 @@ -24,8 +24,8 @@ 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)); +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)); % All but the last calculation for k = 1:N-2 @@ -34,12 +34,12 @@ for k = 1:N-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)); + x_1 = a(k+1) + (fibonacci(N-k-2) / fibonacci(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)); + x_2 = a(k+1) + (fibonacci(N-k-1) / fibonacci(N-k)) * (b(k+1) - a(k+1)); end end