Refactored version of your code
% Ratio comparer%
Phi = ((1 + sqrt(5))/2);
GR = 1;
i = 1;
error =abs(Phi-GR) % Initialize the error vector
while (abs(Phi - GR) > 10^(-7))
i = i+1;
GR = Fibrat(i);
error = [error abs(Phi-GR)]; % Append the current error to the error vector
end
% View various variables in long format
format long
GR
i
error
% Create a vector from 1:i and plot error against it
plot(1:i-1,error)
% Local function used by above code
function r = Fibrat(n)
% Fibrat(n) gives the ratio of the n+1 th fibonacci number and the nth
% fibonacci number
F(1) = 1;
F(2) = 1;
for i = 3:n+1
F(i) = F(i-1)+F(i-2);
end
r = F(n+1)/F(n);
end
Note - Local functions need to be defined after the driver code