Help with Fibonacci function in octave / matlab. The Fibonacci pattern is http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html
0 : 0
1 : 1
2 : 1
3 : 2
4 : 3
5 : 5
The function I have works but it skips a one
how can I fix this so the function correctly follows the list above? Below is what my function creates.
0 : 0
1 : 1
2 : 2
3 : 3
4 : 5
5 : 8
See function below
function f = rtfib(n)
if n<0
multi=-1; %if neg number store neg in multi variable
n=abs(n); %if neg make pos
else
multi=1;
end
if (n == 0)
f = 0;
elseif (n==1)
f=1;
elseif (n == 2)
f = 2;
else
fOld = 2;
fOlder = 1;
for i = 3 : n
f = fOld + fOlder;
fOlder = fOld;
fOld = f;
end
end
f=f*multi; %put sign back
end
Ps: I'm using octave 4.0 which is similar to matlab
Original code found from Create faster Fibonacci function for n > 100 in MATLAB / octave