Using Matlab
The Fibonacci sequence defined by
where the Nth term, is given by
As N increases, the ratio of two adjacent terms in the sequence approaches The Golden Ratio, ϕ:
Write a function that accepts a single input that is the tolerance, TolGR for the Golden Ratio calculation. Use a loop to generate Fibonacci numbers until the error in subsequent calculations of ϕ, defined by
is less than or equal to TolGR. Your function should output three scalar values, in order:
The final value of the Golden Ratio (a double precision number)
The largest Fibonacci number generated (32-bit unsigned integer datatype)
The number of terms, N, required to meet the tolerance (8-bit unsigned integer datatype).
Show transcribed image text
Expert Answer
Code:
%Define a function Gratio
function Gratio(TolGR)
%Declare variable
fib1 = 1;
%Declare variable
fib2 =1;
%Compute phi1
phi1 = fib2/fib1 ;
%Declare variable
n = 3 ;
%Loop infinite
while true
%Compute fib3
fib3 = fib1+fib2;
%Assign value
fib1=fib2;
%Assign value
fib2 = fib3;
%Compute ratio
phi2 = fib2/fib1
%compute error
err = abs(phi2-phi1)
%If error value is less than tolerance
if err<TolGR
%Break
break
%End
end
%Assign value
phi1 = phi2;
%Update value
n=n+1;
%End
end
%Display value
disp(phi2)
%Display result
fprintf(‘The Fibonacci sequence to %d terms isn’,n)
%Display result
fprintf(‘%g ‘,fib3)
%Display new line
fprintf(‘n’)
%End
end
%Call method
Gratio(0.05)