%RFIB                    Random Fibonacci sequence.

rand('state',100)        % Set random number state.
m = 10000;                % Number of iterations.

tic
x = [1 2];               % Initial conditions.
for n = 2:m-1            % Main loop.
     x(n+1) = x(n) + sign(rand-0.5)*x(n-1);
end
toc

disp('redo the calculation with preallocation of memory')
tic
x=zeros(m,1);   % preallocate memory for speed
x(1:2)=[1;2];
for n = 2:m-1            % Main loop.
     x(n+1) = x(n) + sign(rand-0.5)*x(n-1);
end
toc

semilogy(1:m,abs(x))
c = 1.13198824;          % Viswanath's constant.
hold on
semilogy(1:m,c.^(1:m))
hold off
