A Fibonacci sequence is composed of elements created by addi
A Fibonacci sequence is composed of elements created by adding the two previous elements. Below is the simplest Fibonacci sequence. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. Create a program to print above sequence in the command window. Use \'For\' loop to implement the logic given in the first line. Total numbers in the above sequence are 10. You should start script with saving first two numbers in a vector i.e. a(l)=l; a(2)=1; a(3) to a(10) should be calculated using For loop. Sequence numbers are saved in vector a.
Solution
% matlab code fibonacci sequence
% empty vector
a = [];
a(1) = 1;
a(2) = 1;
% determining subsequent numbers
for i=3:10
a(i) = a(i-1) + a(i-2);
end
disp(\"Fibonacci Sequence: \");
disp(a);
%{
output:
Fibonacci Sequence:
1 1 2 3 5 8 13 21 34 55
%}
