A Fibonacci sequence is composed of elements created by adding the two previous
ID: 3938484 • Letter: A
Question
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.Explanation / Answer
% 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
%}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.