% Example Program for Fibonacci Sequence % Author: F. P. Schloerb % clear the workspace initially to allow rerunning with different % numbers of elements in the sequence clear % initialize the first two terms in the sequence % note that MATLAB indexing is one-off traditional indexing % for Fibonacci Sequence f(1) = 0; f(2) = 1; % initialize golden ratio "sequence" with zeros r(1) = 0.0; % r is undefined so set to zero r(2) = 0.0; % r is infinite (1/0) so set to zero % ask the user for number of terms to compute n = input('Enter number of terms of series to compute'); % now do the sequence through index of n (input above) for i=3:n f(i) = f(i-1)+f(i-2); r(i) = f(i)/f(i-1); end % print resulting arrays f r % plot successive approximations to golden ratio plot(1:n,r);