Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Recall the definition of the Fibonacci numbers from last recitation. Without usi

ID: 3676286 • Letter: R

Question

Recall the definition of the Fibonacci numbers from last recitation. Without using recursion, please write a MATLAB function that accepts an input parameter n and returns (outputs) a vector with the first ’n’ numbers in the series.

Example usage:

x = fibonacci(10)

x=

a. Provide the code and sample output for a simplified solution that only needs to work for inputs in the range 2 < n < 100. You can name your function fibnonacciSimplified.

b. Provide the code and sample output for a full solution that also works for any 0 < n < 100. You can name this function fibonacciFull.

Explanation / Answer

Answers:)

Code for a).

N=input('Pick a number ');

if N<3 || N>99

return;
fib=zeros(1,N);
fib(1)=1;
fib(2)=1;
k=3;
while k <= N
    fib(k)=fib(k-2)+fib(k-1);
    k=k+1;
end

fprintf('The Fibonacci sequence to %d terms is ',N);
fprintf('%g ',fib);
fprintf(' ');

Sample Output:

Pick a number
14
The Fibonacci sequence to 14 terms is
1 1 2 3 5 8 13 21 34 55 89 144 233 377

Code for b).

N=input('Pick a number ');

if N == 1

return 1;

if N == 0 || N>99

return
fib=zeros(1,N);
fib(1)=1;
fib(2)=1;
k=3;
while k <= N
    fib(k)=fib(k-2)+fib(k-1);
    k=k+1;
end

fprintf('The Fibonacci sequence to %d terms is ',N);
fprintf('%g ',fib);
fprintf(' ');

Sample Output:

Pick a number
1
The Fibonacci sequence to 14 terms is
1

Pick a number
14
The Fibonacci sequence to 14 terms is
1 1 2 3 5 8 13 21 34 55 89 144 233 377