Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab code Recall that famous Fibonacci sequence is composed of elements create

ID: 3875193 • Letter: M

Question

Matlab code

Recall that famous Fibonacci sequence is composed of elements created by adding the two previous elements. One interesting property of a Fibonacci sequence is that the ratio of the values of adjacent members of the sequence approaches a number called "the golden ratio" or (phi). Create a program, which determines the minimum number of elements in the Fibonacci sequence needed to bring error in estimating below 0.001 (Hint: compute and compare values of for each consecutive pair of elements in the Fibonacci sequence. Once the "observable" variation in the computed values does not exceed 0.001 stop the process.)

Explanation / Answer

%Clear screen and memory
clear; clc; format compact

% Initialize the first two values,phi

f(1) = 1;
f(2) = 1;
phi = (1 + sqrt(5))/2;
error = inf;
i=3;

% Create the Fibonacci numbers untill error is below 0.001
while error >= 0.001
    % Perform the sum of terms accordingly
    f(i) = f(i-1) + f(i-2);
    % Calculate and display the ratio of 2 consecutive elements     % of the series
    golden_ratio = f(i)/f(i-1);
    error = abs(phi - golden_ratio);
    str = [num2str(f(i)) ' ' num2str(f(i-1)) ' ' ...
    num2str(golden_ratio, 10) '            error=' num2str(error)];
    disp(str)
  
    i = i+1;
end