
% Matlab code to generate and plot Fibonacci numbers.

clear f     % If you decrease the value of n, the plots still work.
n    = 70;  % The number of Fibonacci numbers to compute.
fi   = 1;   % Start with f0 = f1 = 1, as usual.
fim1 = 1;
f(1) = fi;  % Record f(1) = f1.
for i = 2:n
   fip1 = fi + fim1;  % f(i+1) = f(i) + f(i-1) is the recurrence
   fim1 = fi;         % relation that defines the Fibonacci numbers.
   fi   = fip1;
   f(i) = fi;         % Record f(i) for plotting.
end

figure(1)             % Plot all the numbers on a linear scale.
plot(f)
xlabel('i')           % The horizontal and vertical axes are 
ylabel('f')           % i and f respectively.
topTitle = sprintf('Fibonacci up to n = %d',n); % Put n into the title.
title(topTitle)
text(n/10, .9*f(n), 'Linear scale');
grid                  % Make it easier to read values in the plot.
set ( gcf, 'PaperPosition', [.25 2.5 3.2 2.5]); 
                      % Print a tiny image of the plot for the book.
print -dps FibLinear_se

figure(2)             % Plot all the numbers on a linear scale.
semilogy(f)
xlabel('i')
ylabel('f')
topTitle = sprintf('Fibonacci up to n = %d',n);
title(topTitle)
text(n/10, exp(.9*log(f(n))), 'Log scale');
grid
set ( gcf, 'PaperPosition', [.25 2.5 3.2 2.5]);
print -dps FibLog_se



