A Fibonacci sequence is composed of elements created by adding the two previous
ID: 3605948 • Letter: A
Question
A Fibonacci sequence is composed of elements created by adding the two previous
elements. The simplest Fibonacci sequence starts with 1,1 and proceed
s as follows:
1, 1, 2, 3, 5, 8, 13, ...
However, a Fibonacci sequence can be created with any two starting numbers.
Create a MATLAB function that creates a Fibonacci sequence. There should be
three
inputs. The first and second number
of
the
desired
Fibona
cci sequence and the total
number of elements desired for the Fibonacci sequence.
For example, if you use your completed function to solve for the Fibonacci sequence
where the first two numbers are 3 & 4 and your total number of elements is 5, your
functi
on should return:
[3, 4, 7, 11, 18]
Explanation / Answer
function fib = fibonaci(a,b,n)
%empty array
fib = [];
%while n is greater than zero
while(n>0)
% add element to fib array
fib = [fib a];
%update elements
temp = a;
a = b;
b = b+temp;
%reduce n
n = n-1;
end
end
%call function
disp(fibonaci(3,4,5));
% sample output
% 3 4 7 11 18
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.