I am trying to find the fib sequence using recursion but my function keeps giving me an error.
function y = r_nFib(seq, n)
y = zeros(1,n);
for m = 1:n
y(m) = r_nFib(m);
end
if seq == 0
y = [0 y];
else
y = [seq, seq, y];
function y = r_nFib(seq, n)
if n<3
y(1:n) = 1;
else
y(n) = r_nFib(n-2) + r_nFib(n-1);
end
y = y(n);
end
end
n is the length of the fib sequence and seq is the starting number. If seq is 0 then this is how the sequence is going to start
y = [0 1 1 2 3 5 8] % first two number will be the 0 and 1
if seq is any thing other than 0, then
if seq = 2;
y = [2 2 4 6 10] % first two number will be the seq
How do I correct my function to give me the right answer. I have never used recursion and I am new to it. Any help would be really appreciated.
y = r_nFib(4,10)
y = [4 4 8 12 20 32 52 84 136 220];
Thank you.