MATLAB - write a function called fib that accepts a single argument n which you
ID: 3680916 • Letter: M
Question
MATLAB - write a function called fib that accepts a single argument n which you may assume is a positive integer. Your function will return a row vector containing the first n elements of the Fibonacci sequence. The Fibonacci sequence is a sequence of numbers that starts with 1,1, and each successive element is the sum of the previous two. Therefore, if we denote the ith Fibonacci number as Fi then we have... Question 2 5 marks] The Fibonacci sequence is a sequence of numbers that starts with 1, 1, and each successive element is the sum of the previous two. Therefore, if we denote the ith Fibonacci number as F then we have Fi = Fi-1 +Fi-2 for i = 3,4,5, Write a function called fib that accepts a single argument n which you may assume is a positive integer. Your functio sequence n it to return a row vector containing the first n elements of the Fibonacci
Explanation / Answer
function F = fibonacci(n) Fn = [1 0]; %Fn(1) is F_{n-2}, Fn(2) is F_{n-1} F = 0; %F is F_{n} for i = (1:abs(n)) Fn(2) = F; F = sum(Fn); Fn(1) = Fn(2); end if n < 0 F = F*((-1)^(n+1)); end endRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.