Fibonacci numbers are the numbers in a sequence in which the first two elements are 0 and 1, and the value of each subsequent element is the sum of the previous two elements: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... Write a MATLAB program that determines and displays the first 20 Fibonacci numbers

Answer :

Fibonacci numbers are the numbers in a sequence in which the first two elements are 0 and 1, and the value of each subsequent element is the sum of the previous two elements: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... MATLAB program that determines and displays the first 20 fibonacci numbers is-

%Clear screen and memory

clear; clc; format compact  

% Initialize the first two values

f(1) = 1;

f(2) = 1;  

% Create the first 20 Fibonacci numbers

for i = 3 : 20

   % 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);

   str = [num2str(f(i)) ' ' num2str(f(i-1)) ' ' ...

   num2str(golden_ratio, 10)];

   disp(str)

end

Other Questions