% Unit 2 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%==================
% Fibonacci series
%==================
clear all
close all
clc

n = input('how many Fibonacci number? ');

% vector for Fibonacci numbers
f = zeros(n,1);
f(1) = 1;
f(2) = 1;

% vector for Binet's formula
b    = zeros(n,1);
b(1) = 1;
b(2) = 1;
phi  = (1+sqrt(5))/2;
psi  = (1-sqrt(5))/2;

% for-loop to calculate Fibonacci numbers
for i = 3:n
   f(i) = f(i-1) + f(i-2);
   b(i) = (phi^i-psi^i)/sqrt(5);
end

% show Fibonacci numbers
f(n)
b(n)

% show that Binet's formula is correct
f(n)-b(n)