99.3k views
2 votes
12. Fibonacci numbers are the numbers in a sequence in which the first three elements are 0, 1, and 1, and the value of each subsequent element is the sum of the previous three elements: 0, 1, 1, 2, 4, 7, 13, 24, ... Write a MATLAB program in a script file that determines and displays the first 25 Fibonacci numbers.

User Ivan Lesko
by
5.9k points

1 Answer

1 vote

Answer:

%first 3 Fibonacci numbers

f(1)=0;

f(2)=1;

f(3)=1;

%next 18 Fibonacci numbers

for i=1:22

f(i+3)=f(i)+f(i+1)+f(i+2);

end

%display the Fibonacci numbers

fprintf('First 25 Fiboncci numbers:\\')

fprintf('%d, ', f(1:24));

fprintf('%d\\', f(25));

Step-by-step explanation:

The MATLAB code above prints out the first 25 numbers in a fibonacci sequence. The sum of the previous three elements is equal to the next element.

User Shouko Nishimiya
by
5.6k points