Fibonacci numbers are the numbers in a sequence in which the

Fibonacci numbers are the numbers in a sequence in which the first two elements areo and 1, and the value of each subsequent element is the sum of the previous two elements: Write a MATLAB function the outputs the first N Fibonacci numbers, for any number N. The function should accept a single input N and returns an output array containing the sequence of N numbers. BONUS: Develop the MATLAB function that returns a Fibonacci sequence of numbers in which the last number in the sequence is less than or equal to the number K, specified as an input to the function.

Solution

function m = Fibonacci_series(N)
m(1) = 0;
m(2) = 1;

for i = 3:N
m(i) = m(i-1)+m(i-2);
end

end

OUTPUT:

Fibonacci_series(10)

ans =

Columns 1 through 9

0 1 1 2 3 5 8 13 21

Column 10

34

function m = Fibonacci_series_bonus(k)
m(1) = 0;
m(2) = 1;
i = 3;
n = 0;
while 1
if n>k
break
end
m(i) = m(i-1)+m(i-2);
n = m(i);
i = i+1;
end
m = m(1:i-2);
end

OUTPUT:

Fibonacci_series_bonus(1000)

ans =

Columns 1 through 9

0 1 1 2 3 5 8 13 21

Columns 10 through 17

34 55 89 144 233 377 610 987

 Fibonacci numbers are the numbers in a sequence in which the first two elements areo and 1, and the value of each subsequent element is the sum of the previous

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site