浏览代码

Overwrite fibonacci with Binet formula

tags/v1.0
Christos Choutouridis 2 周前
父节点
当前提交
23b60aac66
共有 1 个文件被更改,包括 5 次插入5 次删除
  1. +5
    -5
      Work 1/scripts/min_fibonacci.m

+ 5
- 5
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


正在加载...
取消
保存